@@ -5,15 +5,15 @@ use axum::http::{HeaderMap, StatusCode};
5
5
use axum:: response:: { IntoResponse , Response } ;
6
6
use axum:: routing:: get;
7
7
use axum:: Router ;
8
+ use prometheus_client:: encoding:: negotiate_escaping_scheme;
8
9
use prometheus_client:: encoding:: text:: encode;
10
+ use prometheus_client:: encoding:: EscapingScheme :: UnderscoreEscaping ;
11
+ use prometheus_client:: encoding:: ValidationScheme :: UTF8Validation ;
9
12
use prometheus_client:: metrics:: counter:: Counter ;
10
13
use prometheus_client:: metrics:: family:: Family ;
11
14
use prometheus_client:: registry:: { Registry , RegistryBuilder } ;
12
15
use std:: sync:: Arc ;
13
16
use tokio:: sync:: Mutex ;
14
- use prometheus_client:: encoding:: EscapingScheme :: UnderscoreEscaping ;
15
- use prometheus_client:: encoding:: negotiate_escaping_scheme;
16
- use prometheus_client:: encoding:: ValidationScheme :: UTF8Validation ;
17
17
18
18
#[ derive( Debug ) ]
19
19
pub struct Metrics {
@@ -22,7 +22,9 @@ pub struct Metrics {
22
22
23
23
impl Metrics {
24
24
pub fn inc_requests ( & self , method : String ) {
25
- self . requests . get_or_create ( & vec ! [ ( "method.label" . to_owned( ) , method) ] ) . inc ( ) ;
25
+ self . requests
26
+ . get_or_create ( & vec ! [ ( "method.label" . to_owned( ) , method) ] )
27
+ . inc ( ) ;
26
28
}
27
29
}
28
30
@@ -31,14 +33,15 @@ pub struct AppState {
31
33
pub registry : Registry ,
32
34
}
33
35
34
- pub async fn metrics_handler ( State ( state) : State < Arc < Mutex < AppState > > > , headers : HeaderMap ) -> impl IntoResponse {
36
+ pub async fn metrics_handler (
37
+ State ( state) : State < Arc < Mutex < AppState > > > ,
38
+ headers : HeaderMap ,
39
+ ) -> impl IntoResponse {
35
40
let mut state = state. lock ( ) . await ;
36
41
let mut buffer = String :: new ( ) ;
37
42
if let Some ( accept) = headers. get ( "Accept" ) {
38
- let escaping_scheme = negotiate_escaping_scheme (
39
- accept. to_str ( ) . unwrap ( ) ,
40
- state. registry . escaping_scheme ( )
41
- ) ;
43
+ let escaping_scheme =
44
+ negotiate_escaping_scheme ( accept. to_str ( ) . unwrap ( ) , state. registry . escaping_scheme ( ) ) ;
42
45
state. registry . set_escaping_scheme ( escaping_scheme) ;
43
46
}
44
47
encode ( & mut buffer, & state. registry ) . unwrap ( ) ;
@@ -47,7 +50,8 @@ pub async fn metrics_handler(State(state): State<Arc<Mutex<AppState>>>, headers:
47
50
. status ( StatusCode :: OK )
48
51
. header (
49
52
CONTENT_TYPE ,
50
- "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=" . to_owned ( ) + state. registry . escaping_scheme ( ) . as_str ( ) ,
53
+ "application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=" . to_owned ( )
54
+ + state. registry . escaping_scheme ( ) . as_str ( ) ,
51
55
)
52
56
. body ( Body :: from ( buffer) )
53
57
. unwrap ( )
@@ -69,9 +73,11 @@ async fn main() {
69
73
. with_escaping_scheme ( UnderscoreEscaping )
70
74
. build ( ) ,
71
75
} ;
72
- state
73
- . registry
74
- . register ( "requests.count" , "Count of requests" , metrics. requests . clone ( ) ) ;
76
+ state. registry . register (
77
+ "requests.count" ,
78
+ "Count of requests" ,
79
+ metrics. requests . clone ( ) ,
80
+ ) ;
75
81
let metrics = Arc :: new ( Mutex :: new ( metrics) ) ;
76
82
let state = Arc :: new ( Mutex :: new ( state) ) ;
77
83
0 commit comments