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