1
1
#[ macro_use]
2
2
mod macros;
3
3
4
+ use std:: { time:: { Duration , Instant } , collections:: HashMap , sync:: Mutex } ;
4
5
use self :: macros:: MetricFromOpts ;
5
6
use crate :: db:: Pool ;
6
7
use crate :: BuildQueue ;
@@ -10,6 +11,7 @@ use prometheus::proto::MetricFamily;
10
11
load_metric_type ! ( IntGauge as single) ;
11
12
load_metric_type ! ( IntCounter as single) ;
12
13
load_metric_type ! ( IntCounterVec as vec) ;
14
+ load_metric_type ! ( IntGaugeVec as vec) ;
13
15
load_metric_type ! ( HistogramVec as vec) ;
14
16
15
17
metrics ! {
@@ -44,6 +46,13 @@ metrics! {
44
46
/// The time it takes to render a rustdoc page
45
47
pub ( crate ) rustdoc_rendering_times: HistogramVec [ "step" ] ,
46
48
49
+ /// Count of recently accessed crates
50
+ pub ( crate ) recent_krates: IntGaugeVec [ "duration" ] ,
51
+ /// Count of recently accessed versions of crates
52
+ pub ( crate ) recent_versions: IntGaugeVec [ "duration" ] ,
53
+ /// Count of recently accessed platforms of versions of crates
54
+ pub ( crate ) recent_platforms: IntGaugeVec [ "duration" ] ,
55
+
47
56
/// Number of crates built
48
57
pub ( crate ) total_builds: IntCounter ,
49
58
/// Number of builds that successfully generated docs
@@ -67,6 +76,69 @@ metrics! {
67
76
namespace: "docsrs" ,
68
77
}
69
78
79
+ #[ derive( Debug ) ]
80
+ pub ( crate ) struct RecentReleases {
81
+ krates : Mutex < HashMap < String , Instant > > ,
82
+ versions : Mutex < HashMap < String , Instant > > ,
83
+ platforms : Mutex < HashMap < String , Instant > > ,
84
+ }
85
+
86
+ impl RecentReleases {
87
+ pub ( crate ) fn new ( ) -> Self {
88
+ Self {
89
+ krates : Mutex :: new ( HashMap :: new ( ) ) ,
90
+ versions : Mutex :: new ( HashMap :: new ( ) ) ,
91
+ platforms : Mutex :: new ( HashMap :: new ( ) ) ,
92
+ }
93
+ }
94
+
95
+ pub ( crate ) fn record ( & self , krate : & str , version : & str , target : & str ) {
96
+ self . krates . lock ( ) . unwrap ( ) . insert ( krate. to_owned ( ) , Instant :: now ( ) ) ;
97
+ self . versions . lock ( ) . unwrap ( ) . insert ( format ! ( "{}/{}" , krate, version) , Instant :: now ( ) ) ;
98
+ self . platforms . lock ( ) . unwrap ( ) . insert ( format ! ( "{}/{}/{}" , krate, version, target) , Instant :: now ( ) ) ;
99
+ }
100
+
101
+ pub ( crate ) fn gather ( & self , metrics : & Metrics ) {
102
+ fn inner ( map : & mut HashMap < String , Instant > , metric : & IntGaugeVec ) {
103
+ let mut hour_count = 0 ;
104
+ let mut half_hour_count = 0 ;
105
+ let mut five_minute_count = 0 ;
106
+ map. retain ( |_, instant| {
107
+ let elapsed = instant. elapsed ( ) ;
108
+ if elapsed > Duration :: from_secs ( 60 * 60 ) {
109
+ return false ;
110
+ }
111
+ hour_count += 1 ;
112
+ if elapsed > Duration :: from_secs ( 30 * 60 ) {
113
+ return true ;
114
+ }
115
+ half_hour_count += 1 ;
116
+ if elapsed > Duration :: from_secs ( 5 * 60 ) {
117
+ return true ;
118
+ }
119
+ five_minute_count += 1 ;
120
+ true
121
+ } ) ;
122
+
123
+ metric
124
+ . with_label_values ( & [ "one hour" ] )
125
+ . set ( hour_count) ;
126
+
127
+ metric
128
+ . with_label_values ( & [ "half hour" ] )
129
+ . set ( half_hour_count) ;
130
+
131
+ metric
132
+ . with_label_values ( & [ "five minutes" ] )
133
+ . set ( five_minute_count) ;
134
+ }
135
+
136
+ inner ( & mut * self . krates . lock ( ) . unwrap ( ) , & metrics. recent_krates ) ;
137
+ inner ( & mut * self . versions . lock ( ) . unwrap ( ) , & metrics. recent_versions ) ;
138
+ inner ( & mut * self . platforms . lock ( ) . unwrap ( ) , & metrics. recent_platforms ) ;
139
+ }
140
+ }
141
+
70
142
impl Metrics {
71
143
pub ( crate ) fn gather (
72
144
& self ,
@@ -82,6 +154,7 @@ impl Metrics {
82
154
. set ( queue. prioritized_count ( ) ? as i64 ) ;
83
155
self . failed_crates_count . set ( queue. failed_count ( ) ? as i64 ) ;
84
156
157
+ self . recent_releases . gather ( self ) ;
85
158
self . gather_system_performance ( ) ;
86
159
Ok ( self . registry . gather ( ) )
87
160
}
0 commit comments