@@ -7,7 +7,7 @@ use iron::status::Status;
77use prometheus:: { Encoder , HistogramVec , TextEncoder } ;
88use std:: time:: { Duration , Instant } ;
99
10- pub fn metrics_handler ( req : & mut Request ) -> IronResult < Response > {
10+ pub ( super ) fn metrics_handler ( req : & mut Request ) -> IronResult < Response > {
1111 let metrics = extension ! ( req, Metrics ) ;
1212 let pool = extension ! ( req, Pool ) ;
1313 let queue = extension ! ( req, BuildQueue ) ;
@@ -30,7 +30,7 @@ fn duration_to_seconds(d: Duration) -> f64 {
3030 d. as_secs ( ) as f64 + nanos
3131}
3232
33- pub struct RequestRecorder {
33+ pub ( super ) struct RequestRecorder {
3434 handler : Box < dyn iron:: Handler > ,
3535 route_name : String ,
3636}
@@ -108,6 +108,7 @@ impl Drop for RenderingTimesRecorder<'_> {
108108#[ cfg( test) ]
109109mod tests {
110110 use crate :: test:: { assert_success, wrapper} ;
111+ use crate :: Context ;
111112 use std:: collections:: HashMap ;
112113
113114 #[ test]
@@ -133,8 +134,8 @@ mod tests {
133134 ( "/sitemap.xml" , "static resource" ) ,
134135 ( "/-/static/style.css" , "static resource" ) ,
135136 ( "/-/static/vendored.css" , "static resource" ) ,
136- ( "/rustdoc/rcc/0.0.0/rcc/index.html" , "database " ) ,
137- ( "/rustdoc/gcc/0.0.0/gcc/index.html" , "database " ) ,
137+ ( "/rustdoc/rcc/0.0.0/rcc/index.html" , "rustdoc page " ) ,
138+ ( "/rustdoc/gcc/0.0.0/gcc/index.html" , "rustdoc page " ) ,
138139 ] ;
139140
140141 wrapper ( |env| {
@@ -167,29 +168,43 @@ mod tests {
167168 * entry += 2 ;
168169 }
169170
171+ // this shows what the routes were *actually* recorded as, making it easier to update ROUTES if the name changes.
172+ let metrics_serialized = metrics. gather ( & env. pool ( ) ?, & env. build_queue ( ) ) ?;
173+ let all_routes_visited = metrics_serialized
174+ . iter ( )
175+ . find ( |x| x. get_name ( ) == "docsrs_routes_visited" )
176+ . unwrap ( ) ;
177+ let routes_visited_pretty: Vec < _ > = all_routes_visited
178+ . get_metric ( )
179+ . iter ( )
180+ . map ( |metric| {
181+ let labels = metric. get_label ( ) ;
182+ assert_eq ! ( labels. len( ) , 1 ) ; // not sure when this would be false
183+ let route = labels[ 0 ] . get_value ( ) ;
184+ let count = metric. get_counter ( ) . get_value ( ) ;
185+ format ! ( "{}: {}" , route, count)
186+ } )
187+ . collect ( ) ;
188+ println ! ( "routes: {:?}" , routes_visited_pretty) ;
189+
170190 for ( label, count) in expected. iter ( ) {
171191 assert_eq ! (
172192 metrics. routes_visited. with_label_values( & [ * label] ) . get( ) ,
173- * count
193+ * count,
194+ "routes_visited metrics for {} are incorrect" ,
195+ label,
174196 ) ;
175197 assert_eq ! (
176198 metrics
177199 . response_time
178200 . with_label_values( & [ * label] )
179201 . get_sample_count( ) ,
180- * count as u64
202+ * count as u64 ,
203+ "response_time metrics for {} are incorrect" ,
204+ label,
181205 ) ;
182206 }
183207
184- // extra metrics for the "database success" hack
185- assert_eq ! (
186- metrics
187- . routes_visited
188- . with_label_values( & [ "database success" ] )
189- . get( ) ,
190- 2
191- ) ;
192-
193208 Ok ( ( ) )
194209 } )
195210 }
0 commit comments