55Use metric enums to reuse possible values of a label.
66
77*/
8- use prometheus :: * ;
8+ use std :: sync :: LazyLock ;
99
10- use lazy_static :: lazy_static ;
10+ use prometheus :: * ;
1111use prometheus_static_metric:: { auto_flush_from, make_auto_flush_static_metric} ;
1212
1313make_auto_flush_static_metric ! {
@@ -34,31 +34,28 @@ make_auto_flush_static_metric! {
3434 }
3535}
3636
37- lazy_static ! {
38- pub static ref HTTP_COUNTER_VEC : IntCounterVec =
39- register_int_counter_vec ! (
40- "http_requests_total ",
41- "Number of HTTP requests." ,
42- & [ "product" , "method" , "version" ] // it doesn't matter for the label order
43- ) . unwrap( ) ;
44- }
37+ pub static HTTP_COUNTER_VEC : LazyLock < IntCounterVec > = LazyLock :: new ( || {
38+ register_int_counter_vec ! (
39+ "http_requests_total" ,
40+ "Number of HTTP requests. ",
41+ & [ "product" , "method" , "version" ] // it doesn't matter for the label order
42+ )
43+ . unwrap ( )
44+ } ) ;
4545
4646// Macro expanded code of auto_flush_from!
47- // lazy_static! {
48- // pub static ref TLS_HTTP_COUNTER: Lhrs = {
47+ // pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> = LazyLock::new(|| {
4948// thread_local! {
5049// pub static TLS_HTTP_COUNTER_INNER: LhrsInner = LhrsInner::from(& HTTP_COUNTER_VEC);
5150// }
5251// Lhrs::from(&TLS_HTTP_COUNTER_INNER)
53- // };
54- // }
52+ // });
5553//
5654
57- lazy_static ! {
58- // You can also use default flush duration which is 1 second.
59- // pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs);
60- pub static ref TLS_HTTP_COUNTER : Lhrs = auto_flush_from!( HTTP_COUNTER_VEC , Lhrs , std:: time:: Duration :: from_secs( 1 ) ) ;
61- }
55+ // You can also use default flush duration which is 1 second.
56+ // pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs);
57+ pub static TLS_HTTP_COUNTER : LazyLock < Lhrs > =
58+ LazyLock :: new ( || auto_flush_from ! ( HTTP_COUNTER_VEC , Lhrs , std:: time:: Duration :: from_secs( 1 ) ) ) ;
6259
6360fn main ( ) {
6461 TLS_HTTP_COUNTER . foo . post . http1 . inc ( ) ;
@@ -98,10 +95,10 @@ use prometheus::local::*;
9895use prometheus::*;
9996use std::collections::HashMap;
10097use std::mem;
98+ use std::sync::LazyLock;
10199use std::mem::MaybeUninit;
102100use std::thread::LocalKey;
103101
104- use lazy_static::lazy_static;
105102
106103#[allow(dead_code)]
107104#[allow(non_camel_case_types)]
@@ -367,22 +364,19 @@ impl Lhrs {
367364 }
368365}
369366
370- lazy_static! {
371- pub static ref HTTP_COUNTER_VEC: IntCounterVec =
372- register_int_counter_vec ! (
367+ pub static HTTP_COUNTER_VEC: LazyLock<IntCounterVec> =
368+ LazyLock::new(|| register_int_counter_vec ! (
373369"http_requests_total",
374370"Number of HTTP requests.",
375371& ["product", "method", "version"] // it doesn't matter for the label order
376- ).unwrap();
372+ ).unwrap()) ;
377373}
378374
379375thread_local! {
380376pub static TLS_HTTP_COUNTER_INNER: LhrsInner = LhrsInner::from(& HTTP_COUNTER_VEC);
381377}
382378
383- lazy_static! {
384- pub static ref TLS_HTTP_COUNTER: Lhrs = Lhrs::from(&TLS_HTTP_COUNTER_INNER);
385- }
379+ pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> = LazyLock::new(|| Lhrs::from(&TLS_HTTP_COUNTER_INNER));
386380
387381fn main() {
388382 TLS_HTTP_COUNTER.foo.post.http1.inc();
0 commit comments