1- use crate :: db:: Pool ;
2- use crate :: BuildQueue ;
3- use crate :: Metrics ;
1+ use crate :: { db:: Pool , utils:: spawn_blocking, web:: error:: AxumResult , BuildQueue , Metrics } ;
2+ use anyhow:: Context as _;
43use axum:: {
5- extract:: MatchedPath , http:: Request as AxumRequest , middleware:: Next , response:: IntoResponse ,
4+ body:: Body ,
5+ extract:: { Extension , MatchedPath } ,
6+ http:: Request as AxumRequest ,
7+ http:: {
8+ header:: { CONTENT_LENGTH , CONTENT_TYPE } ,
9+ Response as AxumHttpResponse , StatusCode ,
10+ } ,
11+ middleware:: Next ,
12+ response:: IntoResponse ,
613} ;
7- use iron:: headers:: ContentType ;
814use iron:: prelude:: * ;
9- use iron:: status:: Status ;
1015use prometheus:: { Encoder , HistogramVec , TextEncoder } ;
1116use std:: {
1217 borrow:: Cow ,
@@ -16,20 +21,24 @@ use std::{
1621#[ cfg( test) ]
1722use tracing:: debug;
1823
19- pub ( super ) fn metrics_handler ( req : & mut Request ) -> IronResult < Response > {
20- let metrics = extension ! ( req, Metrics ) ;
21- let pool = extension ! ( req, Pool ) ;
22- let queue = extension ! ( req, BuildQueue ) ;
24+ pub ( super ) async fn metrics_handler (
25+ Extension ( pool) : Extension < Pool > ,
26+ Extension ( metrics) : Extension < Arc < Metrics > > ,
27+ Extension ( queue) : Extension < Arc < BuildQueue > > ,
28+ ) -> AxumResult < impl IntoResponse > {
29+ let families = spawn_blocking ( move || metrics. gather ( & pool, & queue) ) . await ?;
2330
2431 let mut buffer = Vec :: new ( ) ;
25- let families = ctry ! ( req, metrics. gather( pool, queue) ) ;
26- ctry ! ( req, TextEncoder :: new( ) . encode( & families, & mut buffer) ) ;
32+ TextEncoder :: new ( )
33+ . encode ( & families, & mut buffer)
34+ . context ( "error encoding metrics" ) ?;
2735
28- let mut resp = Response :: with ( buffer) ;
29- resp. status = Some ( Status :: Ok ) ;
30- resp. headers . set ( ContentType :: plaintext ( ) ) ;
31-
32- Ok ( resp)
36+ Ok ( AxumHttpResponse :: builder ( )
37+ . status ( StatusCode :: OK )
38+ . header ( CONTENT_TYPE , mime:: TEXT_PLAIN . as_ref ( ) )
39+ . header ( CONTENT_LENGTH , buffer. len ( ) )
40+ . body ( Body :: from ( buffer) )
41+ . context ( "error generating response" ) ?)
3342}
3443
3544/// Converts a `Duration` to seconds, used by prometheus internally
0 commit comments