11// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22
33use std:: net:: SocketAddr ;
4+ use std:: sync:: LazyLock ;
45
56use hyper:: body:: Incoming ;
67use hyper:: header:: CONTENT_TYPE ;
@@ -9,33 +10,36 @@ use hyper::service::service_fn;
910use hyper:: Request ;
1011use hyper:: Response ;
1112use hyper_util:: rt:: TokioIo ;
12- use lazy_static:: lazy_static;
1313use prometheus:: { labels, opts, register_counter, register_gauge, register_histogram_vec} ;
1414use prometheus:: { Counter , Encoder , Gauge , HistogramVec , TextEncoder } ;
1515use tokio:: net:: TcpListener ;
1616
1717type BoxedErr = Box < dyn std:: error:: Error + Send + Sync + ' static > ;
1818
19- lazy_static ! {
20- static ref HTTP_COUNTER : Counter = register_counter!( opts!(
19+ static HTTP_COUNTER : LazyLock < Counter > = LazyLock :: new ( || {
20+ register_counter ! ( opts!(
2121 "example_http_requests_total" ,
2222 "Number of HTTP requests made." ,
2323 labels! { "handler" => "all" , }
2424 ) )
25- . unwrap( ) ;
26- static ref HTTP_BODY_GAUGE : Gauge = register_gauge!( opts!(
25+ . unwrap ( )
26+ } ) ;
27+ static HTTP_BODY_GAUGE : LazyLock < Gauge > = LazyLock :: new ( || {
28+ register_gauge ! ( opts!(
2729 "example_http_response_size_bytes" ,
2830 "The HTTP response sizes in bytes." ,
2931 labels! { "handler" => "all" , }
3032 ) )
31- . unwrap( ) ;
32- static ref HTTP_REQ_HISTOGRAM : HistogramVec = register_histogram_vec!(
33+ . unwrap ( )
34+ } ) ;
35+ static HTTP_REQ_HISTOGRAM : LazyLock < HistogramVec > = LazyLock :: new ( || {
36+ register_histogram_vec ! (
3337 "example_http_request_duration_seconds" ,
3438 "The HTTP request latencies in seconds." ,
3539 & [ "handler" ]
3640 )
37- . unwrap( ) ;
38- }
41+ . unwrap ( )
42+ } ) ;
3943
4044async fn serve_req ( _req : Request < Incoming > ) -> Result < Response < String > , BoxedErr > {
4145 let encoder = TextEncoder :: new ( ) ;
0 commit comments