@@ -3,119 +3,149 @@ use crate::BuildQueue;
3
3
use iron:: headers:: ContentType ;
4
4
use iron:: prelude:: * ;
5
5
use iron:: status:: Status ;
6
+ use once_cell:: sync:: Lazy ;
6
7
use prometheus:: {
7
8
opts, register_counter, register_int_counter, register_int_gauge, Encoder , IntCounter ,
8
9
IntGauge , TextEncoder , __register_gauge, register_int_counter_vec, IntCounterVec ,
9
10
__register_counter_vec, histogram_opts, register_histogram_vec, HistogramVec ,
10
11
} ;
11
12
use std:: time:: { Duration , Instant } ;
12
13
13
- lazy_static :: lazy_static! {
14
- static ref QUEUED_CRATES_COUNT : IntGauge = register_int_gauge!(
14
+ static QUEUED_CRATES_COUNT : Lazy < IntGauge > = Lazy :: new ( || {
15
+ register_int_gauge ! (
15
16
"docsrs_queued_crates_count" ,
16
17
"Number of crates in the build queue"
17
18
)
18
- . unwrap( ) ;
19
+ . unwrap ( )
20
+ } ) ;
19
21
20
- pub static ref PRIORITIZED_CRATES_COUNT : IntGauge = register_int_gauge!(
22
+ pub static PRIORITIZED_CRATES_COUNT : Lazy < IntGauge > = Lazy :: new ( || {
23
+ register_int_gauge ! (
21
24
"docsrs_prioritized_crates_count" ,
22
25
"Number of crates in the build queue that have a positive priority"
23
26
)
24
- . unwrap( ) ;
27
+ . unwrap ( )
28
+ } ) ;
25
29
26
- static ref FAILED_CRATES_COUNT : IntGauge = register_int_gauge!(
30
+ static FAILED_CRATES_COUNT : Lazy < IntGauge > = Lazy :: new ( || {
31
+ register_int_gauge ! (
27
32
"docsrs_failed_crates_count" ,
28
33
"Number of crates that failed to build"
29
34
)
30
- . unwrap( ) ;
35
+ . unwrap ( )
36
+ } ) ;
31
37
32
- pub static ref TOTAL_BUILDS : IntCounter = register_int_counter!(
33
- "docsrs_total_builds" ,
34
- "Number of crates built"
35
- )
36
- . unwrap( ) ;
38
+ pub static TOTAL_BUILDS : Lazy < IntCounter > =
39
+ Lazy :: new ( || register_int_counter ! ( "docsrs_total_builds" , "Number of crates built" ) . unwrap ( ) ) ;
37
40
38
- pub static ref SUCCESSFUL_BUILDS : IntCounter = register_int_counter!(
41
+ pub static SUCCESSFUL_BUILDS : Lazy < IntCounter > = Lazy :: new ( || {
42
+ register_int_counter ! (
39
43
"docsrs_successful_builds" ,
40
44
"Number of builds that successfully generated docs"
41
45
)
42
- . unwrap( ) ;
46
+ . unwrap ( )
47
+ } ) ;
43
48
44
- pub static ref FAILED_BUILDS : IntCounter = register_int_counter!(
49
+ pub static FAILED_BUILDS : Lazy < IntCounter > = Lazy :: new ( || {
50
+ register_int_counter ! (
45
51
"docsrs_failed_builds" ,
46
52
"Number of builds that generated a compile error"
47
53
)
48
- . unwrap( ) ;
54
+ . unwrap ( )
55
+ } ) ;
49
56
50
- pub static ref NON_LIBRARY_BUILDS : IntCounter = register_int_counter!(
57
+ pub static NON_LIBRARY_BUILDS : Lazy < IntCounter > = Lazy :: new ( || {
58
+ register_int_counter ! (
51
59
"docsrs_non_library_builds" ,
52
60
"Number of builds that did not complete due to not being a library"
53
61
)
54
- . unwrap( ) ;
62
+ . unwrap ( )
63
+ } ) ;
55
64
56
- pub static ref UPLOADED_FILES_TOTAL : IntCounter = register_int_counter!(
65
+ pub static UPLOADED_FILES_TOTAL : Lazy < IntCounter > = Lazy :: new ( || {
66
+ register_int_counter ! (
57
67
"docsrs_uploaded_files_total" ,
58
68
"Number of files uploaded to S3 or stored in the database"
59
69
)
60
- . unwrap( ) ;
70
+ . unwrap ( )
71
+ } ) ;
61
72
62
- pub static ref ROUTES_VISITED : IntCounterVec = register_int_counter_vec!(
73
+ pub static ROUTES_VISITED : Lazy < IntCounterVec > = Lazy :: new ( || {
74
+ register_int_counter_vec ! (
63
75
"docsrs_routes_visited" ,
64
76
"The traffic of various docs.rs routes" ,
65
77
& [ "route" ]
66
78
)
67
- . unwrap( ) ;
79
+ . unwrap ( )
80
+ } ) ;
68
81
69
- pub static ref RESPONSE_TIMES : HistogramVec = register_histogram_vec!(
82
+ pub static RESPONSE_TIMES : Lazy < HistogramVec > = Lazy :: new ( || {
83
+ register_histogram_vec ! (
70
84
"docsrs_response_time" ,
71
85
"The response times of various docs.rs routes" ,
72
86
& [ "route" ]
73
87
)
74
- . unwrap( ) ;
88
+ . unwrap ( )
89
+ } ) ;
75
90
76
- pub static ref RUSTDOC_RENDERING_TIMES : HistogramVec = register_histogram_vec!(
91
+ pub static RUSTDOC_RENDERING_TIMES : Lazy < HistogramVec > = Lazy :: new ( || {
92
+ register_histogram_vec ! (
77
93
"docsrs_rustdoc_rendering_time" ,
78
94
"The time it takes to render a rustdoc page" ,
79
95
& [ "step" ]
80
96
)
81
- . unwrap( ) ;
97
+ . unwrap ( )
98
+ } ) ;
82
99
83
- pub static ref FAILED_DB_CONNECTIONS : IntCounter = register_int_counter!(
100
+ pub static FAILED_DB_CONNECTIONS : Lazy < IntCounter > = Lazy :: new ( || {
101
+ register_int_counter ! (
84
102
"docsrs_failed_db_connections" ,
85
103
"Number of attempted and failed connections to the database"
86
104
)
87
- . unwrap( ) ;
105
+ . unwrap ( )
106
+ } ) ;
88
107
89
- pub static ref USED_DB_CONNECTIONS : IntGauge = register_int_gauge!(
108
+ pub static USED_DB_CONNECTIONS : Lazy < IntGauge > = Lazy :: new ( || {
109
+ register_int_gauge ! (
90
110
"docsrs_used_db_connections" ,
91
111
"The number of used database connections"
92
112
)
93
- . unwrap( ) ;
113
+ . unwrap ( )
114
+ } ) ;
94
115
95
- pub static ref IDLE_DB_CONNECTIONS : IntGauge = register_int_gauge!(
116
+ pub static IDLE_DB_CONNECTIONS : Lazy < IntGauge > = Lazy :: new ( || {
117
+ register_int_gauge ! (
96
118
"docsrs_idle_db_connections" ,
97
119
"The number of idle database connections"
98
120
)
99
- . unwrap( ) ;
121
+ . unwrap ( )
122
+ } ) ;
100
123
101
- pub static ref MAX_DB_CONNECTIONS : IntGauge = register_int_gauge!(
124
+ pub static MAX_DB_CONNECTIONS : Lazy < IntGauge > = Lazy :: new ( || {
125
+ register_int_gauge ! (
102
126
"docsrs_max_db_connections" ,
103
127
"The maximum database connections"
104
128
)
105
- . unwrap( ) ;
129
+ . unwrap ( )
130
+ } ) ;
106
131
107
- pub static ref OPEN_FILE_DESCRIPTORS : IntGauge = register_int_gauge!(
132
+ #[ cfg( not( windows) ) ]
133
+ pub static OPEN_FILE_DESCRIPTORS : Lazy < IntGauge > = Lazy :: new ( || {
134
+ register_int_gauge ! (
108
135
"docsrs_open_file_descriptors" ,
109
136
"The number of currently opened file descriptors"
110
137
)
111
- . unwrap( ) ;
138
+ . unwrap ( )
139
+ } ) ;
112
140
113
- pub static ref CURRENTLY_RUNNING_THREADS : IntGauge = register_int_gauge!(
141
+ #[ cfg( not( windows) ) ]
142
+ pub static CURRENTLY_RUNNING_THREADS : Lazy < IntGauge > = Lazy :: new ( || {
143
+ register_int_gauge ! (
114
144
"docsrs_running_threads" ,
115
145
"The number of threads being used by docs.rs"
116
146
)
117
- . unwrap( ) ;
118
- }
147
+ . unwrap ( )
148
+ } ) ;
119
149
120
150
pub fn metrics_handler ( req : & mut Request ) -> IronResult < Response > {
121
151
let pool = extension ! ( req, Pool ) ;
@@ -231,6 +261,7 @@ impl Drop for RenderingTimesRecorder {
231
261
#[ cfg( test) ]
232
262
mod tests {
233
263
use crate :: test:: { assert_success, wrapper} ;
264
+ use once_cell:: sync:: Lazy ;
234
265
use std:: {
235
266
collections:: HashMap ,
236
267
sync:: {
@@ -240,9 +271,8 @@ mod tests {
240
271
} ;
241
272
242
273
static ROUTES_VISITED : AtomicUsize = AtomicUsize :: new ( 0 ) ;
243
- lazy_static:: lazy_static! {
244
- static ref RESPONSE_TIMES : Mutex <HashMap <String , usize >> = Mutex :: new( HashMap :: new( ) ) ;
245
- }
274
+ static RESPONSE_TIMES : Lazy < Mutex < HashMap < String , usize > > > =
275
+ Lazy :: new ( || Mutex :: new ( HashMap :: new ( ) ) ) ;
246
276
247
277
pub fn record_tests ( route : & str ) {
248
278
ROUTES_VISITED . fetch_add ( 1 , Ordering :: SeqCst ) ;
0 commit comments