@@ -35,22 +35,23 @@ pub const CDN_INVALIDATION_HISTOGRAM_BUCKETS: &[f64; 11] = &[
3535 24000.0 , // 240
3636] ;
3737
38- metrics ! {
39- pub struct Metrics {
40- /// Number of crates in the build queue
41- queued_crates_count: IntGauge ,
42- /// Number of crates in the build queue that have a positive priority
43- prioritized_crates_count: IntGauge ,
44- /// Number of crates that failed to build
45- failed_crates_count: IntGauge ,
46- /// Whether the build queue is locked
47- queue_is_locked: IntGauge ,
48- /// queued crates by priority
49- queued_crates_count_by_priority: IntGaugeVec [ "priority" ] ,
50-
51- /// queued CDN invalidations
52- queued_cdn_invalidations_by_distribution: IntGaugeVec [ "distribution" ] ,
38+ #[ derive( Debug ) ]
39+ pub struct Metrics {
40+ pub instance : InstanceMetrics ,
41+ pub service : ServiceMetrics ,
42+ }
43+
44+ impl Metrics {
45+ pub fn new ( ) -> Result < Self , prometheus:: Error > {
46+ Ok ( Self {
47+ instance : InstanceMetrics :: new ( ) ?,
48+ service : ServiceMetrics :: new ( ) ?,
49+ } )
50+ }
51+ }
5352
53+ metrics ! {
54+ pub struct InstanceMetrics {
5455 /// The number of idle database connections
5556 idle_db_connections: IntGauge ,
5657 /// The number of used database connections
@@ -143,7 +144,7 @@ impl RecentlyAccessedReleases {
143144 . insert ( ( version, TargetAtom :: from ( target) ) , now) ;
144145 }
145146
146- pub ( crate ) fn gather ( & self , metrics : & Metrics ) {
147+ pub ( crate ) fn gather ( & self , metrics : & InstanceMetrics ) {
147148 fn inner < K : std:: hash:: Hash + Eq > ( map : & DashMap < K , Instant > , metric : & IntGaugeVec ) {
148149 let mut hour_count = 0 ;
149150 let mut half_hour_count = 0 ;
@@ -182,18 +183,112 @@ impl RecentlyAccessedReleases {
182183 }
183184}
184185
185- impl Metrics {
186+ impl InstanceMetrics {
187+ pub ( crate ) fn gather ( & self , pool : & Pool ) -> Result < Vec < MetricFamily > , Error > {
188+ self . idle_db_connections . set ( pool. idle_connections ( ) as i64 ) ;
189+ self . used_db_connections . set ( pool. used_connections ( ) as i64 ) ;
190+ self . max_db_connections . set ( pool. max_size ( ) as i64 ) ;
191+
192+ self . recently_accessed_releases . gather ( self ) ;
193+ self . gather_system_performance ( ) ;
194+ Ok ( self . registry . gather ( ) )
195+ }
196+
197+ #[ cfg( not( target_os = "linux" ) ) ]
198+ fn gather_system_performance ( & self ) { }
199+
200+ #[ cfg( target_os = "linux" ) ]
201+ fn gather_system_performance ( & self ) {
202+ use procfs:: process:: Process ;
203+
204+ let process = Process :: myself ( ) . unwrap ( ) ;
205+ self . open_file_descriptors
206+ . set ( process. fd_count ( ) . unwrap ( ) as i64 ) ;
207+ self . running_threads
208+ . set ( process. stat ( ) . unwrap ( ) . num_threads ) ;
209+ }
210+ }
211+
212+ fn metric_from_opts < T : MetricFromOpts + Clone + prometheus:: core:: Collector + ' static > (
213+ registry : & prometheus:: Registry ,
214+ metric : & str ,
215+ help : & str ,
216+ variable_label : Option < & str > ,
217+ ) -> Result < T , prometheus:: Error > {
218+ let mut opts = prometheus:: Opts :: new ( metric, help) . namespace ( "docsrs" ) ;
219+
220+ if let Some ( label) = variable_label {
221+ opts = opts. variable_label ( label) ;
222+ }
223+
224+ let metric = T :: from_opts ( opts) ?;
225+ registry. register ( Box :: new ( metric. clone ( ) ) ) ?;
226+ Ok ( metric)
227+ }
228+
229+ #[ derive( Debug ) ]
230+ pub struct ServiceMetrics {
231+ pub queued_crates_count : IntGauge ,
232+ pub prioritized_crates_count : IntGauge ,
233+ pub failed_crates_count : IntGauge ,
234+ pub queue_is_locked : IntGauge ,
235+ pub queued_crates_count_by_priority : IntGaugeVec ,
236+ pub queued_cdn_invalidations_by_distribution : IntGaugeVec ,
237+
238+ registry : prometheus:: Registry ,
239+ }
240+
241+ impl ServiceMetrics {
242+ pub fn new ( ) -> Result < Self , prometheus:: Error > {
243+ let registry = prometheus:: Registry :: new ( ) ;
244+ Ok ( Self {
245+ registry : registry. clone ( ) ,
246+ queued_crates_count : metric_from_opts (
247+ & registry,
248+ "queued_crates_count" ,
249+ "Number of crates in the build queue" ,
250+ None ,
251+ ) ?,
252+ prioritized_crates_count : metric_from_opts (
253+ & registry,
254+ "prioritized_crates_count" ,
255+ "Number of crates in the build queue that have a positive priority" ,
256+ None ,
257+ ) ?,
258+ failed_crates_count : metric_from_opts (
259+ & registry,
260+ "failed_crates_count" ,
261+ "Number of crates that failed to build" ,
262+ None ,
263+ ) ?,
264+ queue_is_locked : metric_from_opts (
265+ & registry,
266+ "queue_is_locked" ,
267+ "Whether the build queue is locked" ,
268+ None ,
269+ ) ?,
270+ queued_crates_count_by_priority : metric_from_opts (
271+ & registry,
272+ "queued_crates_count_by_priority" ,
273+ "queued crates by priority" ,
274+ Some ( "priority" ) ,
275+ ) ?,
276+ queued_cdn_invalidations_by_distribution : metric_from_opts (
277+ & registry,
278+ "queued_cdn_invalidations_by_distribution" ,
279+ "queued CDN invalidations" ,
280+ Some ( "distribution" ) ,
281+ ) ?,
282+ } )
283+ }
284+
186285 pub ( crate ) fn gather (
187286 & self ,
188287 pool : & Pool ,
189288 queue : & BuildQueue ,
190289 config : & Config ,
191290 ) -> Result < Vec < MetricFamily > , Error > {
192- self . idle_db_connections . set ( pool. idle_connections ( ) as i64 ) ;
193- self . used_db_connections . set ( pool. used_connections ( ) as i64 ) ;
194- self . max_db_connections . set ( pool. max_size ( ) as i64 ) ;
195291 self . queue_is_locked . set ( queue. is_locked ( ) ? as i64 ) ;
196-
197292 self . queued_crates_count . set ( queue. pending_count ( ) ? as i64 ) ;
198293 self . prioritized_crates_count
199294 . set ( queue. prioritized_count ( ) ? as i64 ) ;
@@ -216,23 +311,6 @@ impl Metrics {
216311 }
217312
218313 self . failed_crates_count . set ( queue. failed_count ( ) ? as i64 ) ;
219-
220- self . recently_accessed_releases . gather ( self ) ;
221- self . gather_system_performance ( ) ;
222314 Ok ( self . registry . gather ( ) )
223315 }
224-
225- #[ cfg( not( target_os = "linux" ) ) ]
226- fn gather_system_performance ( & self ) { }
227-
228- #[ cfg( target_os = "linux" ) ]
229- fn gather_system_performance ( & self ) {
230- use procfs:: process:: Process ;
231-
232- let process = Process :: myself ( ) . unwrap ( ) ;
233- self . open_file_descriptors
234- . set ( process. fd_count ( ) . unwrap ( ) as i64 ) ;
235- self . running_threads
236- . set ( process. stat ( ) . unwrap ( ) . num_threads ) ;
237- }
238316}
0 commit comments