diff --git a/examples/example_custom_registry.rs b/examples/example_custom_registry.rs index 0c99cb31..7fa69f36 100644 --- a/examples/example_custom_registry.rs +++ b/examples/example_custom_registry.rs @@ -3,16 +3,14 @@ //! This examples shows how to use multiple and custom registries, //! and how to perform registration across function boundaries. -use std::collections::HashMap; +use std::{collections::HashMap, sync::LazyLock}; use prometheus::{Encoder, IntCounter, Registry}; -use lazy_static::lazy_static; - -lazy_static! { - static ref DEFAULT_COUNTER: IntCounter = IntCounter::new("default", "generic counter").unwrap(); - static ref CUSTOM_COUNTER: IntCounter = IntCounter::new("custom", "dedicated counter").unwrap(); -} +static DEFAULT_COUNTER: LazyLock = + LazyLock::new(|| IntCounter::new("default", "generic counter").unwrap()); +static CUSTOM_COUNTER: LazyLock = + LazyLock::new(|| IntCounter::new("custom", "dedicated counter").unwrap()); fn main() { // Register default metrics. diff --git a/examples/example_hyper.rs b/examples/example_hyper.rs index 13282192..4e05a64e 100644 --- a/examples/example_hyper.rs +++ b/examples/example_hyper.rs @@ -1,6 +1,7 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. use std::net::SocketAddr; +use std::sync::LazyLock; use hyper::body::Incoming; use hyper::header::CONTENT_TYPE; @@ -9,33 +10,36 @@ use hyper::service::service_fn; use hyper::Request; use hyper::Response; use hyper_util::rt::TokioIo; -use lazy_static::lazy_static; use prometheus::{labels, opts, register_counter, register_gauge, register_histogram_vec}; use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder}; use tokio::net::TcpListener; type BoxedErr = Box; -lazy_static! { - static ref HTTP_COUNTER: Counter = register_counter!(opts!( +static HTTP_COUNTER: LazyLock = LazyLock::new(|| { + register_counter!(opts!( "example_http_requests_total", "Number of HTTP requests made.", labels! {"handler" => "all",} )) - .unwrap(); - static ref HTTP_BODY_GAUGE: Gauge = register_gauge!(opts!( + .unwrap() +}); +static HTTP_BODY_GAUGE: LazyLock = LazyLock::new(|| { + register_gauge!(opts!( "example_http_response_size_bytes", "The HTTP response sizes in bytes.", labels! {"handler" => "all",} )) - .unwrap(); - static ref HTTP_REQ_HISTOGRAM: HistogramVec = register_histogram_vec!( + .unwrap() +}); +static HTTP_REQ_HISTOGRAM: LazyLock = LazyLock::new(|| { + register_histogram_vec!( "example_http_request_duration_seconds", "The HTTP request latencies in seconds.", &["handler"] ) - .unwrap(); -} + .unwrap() +}); async fn serve_req(_req: Request) -> Result, BoxedErr> { let encoder = TextEncoder::new(); diff --git a/examples/example_int_metrics.rs b/examples/example_int_metrics.rs index 05e57e81..f224494c 100644 --- a/examples/example_int_metrics.rs +++ b/examples/example_int_metrics.rs @@ -1,21 +1,22 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. +use std::sync::LazyLock; + use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec}; -use lazy_static::lazy_static; use prometheus::{ register_int_counter, register_int_counter_vec, register_int_gauge, register_int_gauge_vec, }; -lazy_static! { - static ref A_INT_COUNTER: IntCounter = - register_int_counter!("A_int_counter", "foobar").unwrap(); - static ref A_INT_COUNTER_VEC: IntCounterVec = - register_int_counter_vec!("A_int_counter_vec", "foobar", &["a", "b"]).unwrap(); - static ref A_INT_GAUGE: IntGauge = register_int_gauge!("A_int_gauge", "foobar").unwrap(); - static ref A_INT_GAUGE_VEC: IntGaugeVec = - register_int_gauge_vec!("A_int_gauge_vec", "foobar", &["a", "b"]).unwrap(); -} +static A_INT_COUNTER: LazyLock = + LazyLock::new(|| register_int_counter!("A_int_counter", "foobar").unwrap()); +static A_INT_COUNTER_VEC: LazyLock = LazyLock::new(|| { + register_int_counter_vec!("A_int_counter_vec", "foobar", &["a", "b"]).unwrap() +}); +static A_INT_GAUGE: LazyLock = + LazyLock::new(|| register_int_gauge!("A_int_gauge", "foobar").unwrap()); +static A_INT_GAUGE_VEC: LazyLock = + LazyLock::new(|| register_int_gauge_vec!("A_int_gauge_vec", "foobar", &["a", "b"]).unwrap()); fn main() { A_INT_COUNTER.inc(); diff --git a/examples/example_push.rs b/examples/example_push.rs index c56a6eef..50efa490 100644 --- a/examples/example_push.rs +++ b/examples/example_push.rs @@ -1,27 +1,29 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. use std::env; +use std::sync::LazyLock; use std::thread; use std::time; use getopts::Options; use prometheus::{Counter, Histogram}; -use lazy_static::lazy_static; use prometheus::{labels, register_counter, register_histogram}; -lazy_static! { - static ref PUSH_COUNTER: Counter = register_counter!( +static PUSH_COUNTER: LazyLock = LazyLock::new(|| { + register_counter!( "example_push_total", "Total number of prometheus client pushed." ) - .unwrap(); - static ref PUSH_REQ_HISTOGRAM: Histogram = register_histogram!( + .unwrap() +}); +static PUSH_REQ_HISTOGRAM: LazyLock = LazyLock::new(|| { + register_histogram!( "example_push_request_duration_seconds", "The push request latencies in seconds." ) - .unwrap(); -} + .unwrap() +}); fn main() { let args: Vec = env::args().collect(); diff --git a/src/lib.rs b/src/lib.rs index 11289e26..756ddc4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,19 +49,18 @@ You can find more examples within # Static Metrics This crate supports staticly built metrics. You can use it with -[`lazy_static`](https://docs.rs/lazy_static/) to quickly build up and collect +[`LazyLock`](https://doc.rust-lang.org/std/sync/struct.LazyLock.html) to quickly build up and collect some metrics. ```rust use prometheus::{self, IntCounter, TextEncoder, Encoder}; -use lazy_static::lazy_static; +use std::sync::LazyLock; use prometheus::register_int_counter; -lazy_static! { - static ref HIGH_FIVE_COUNTER: IntCounter = - register_int_counter!("highfives", "Number of high fives received").unwrap(); -} +static HIGH_FIVE_COUNTER: LazyLock = + LazyLock::new(|| register_int_counter!("highfives", "Number of high fives received").unwrap()); + HIGH_FIVE_COUNTER.inc(); assert_eq!(HIGH_FIVE_COUNTER.get(), 1); @@ -75,14 +74,12 @@ By default, this registers with a default registry. To make a report, you can ca # use prometheus::IntCounter; use prometheus::{self, TextEncoder, Encoder}; -use lazy_static::lazy_static; +use std::sync::LazyLock; use prometheus::register_int_counter; // Register & measure some metrics. -# lazy_static! { -# static ref HIGH_FIVE_COUNTER: IntCounter = -# register_int_counter!("highfives", "Number of high fives received").unwrap(); -# } +# static HIGH_FIVE_COUNTER: LazyLock = +# LazyLock::new(|| register_int_counter!("highfives", "Number of high fives received").unwrap()); # HIGH_FIVE_COUNTER.inc(); let mut buffer = Vec::new(); diff --git a/static-metric/README.md b/static-metric/README.md index cdf66a1e..c961348e 100644 --- a/static-metric/README.md +++ b/static-metric/README.md @@ -91,7 +91,7 @@ For heavier scenario that a global shared static-metric might not be effecient e ```rust use prometheus::*; -use lazy_static:lazy_static; +use std::sync::LazyLock; use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric}; make_auto_flush_static_metric! { @@ -118,20 +118,18 @@ make_auto_flush_static_metric! { } } -lazy_static! { - pub static ref HTTP_COUNTER_VEC: IntCounterVec = - register_int_counter_vec ! ( - "http_requests_total", - "Number of HTTP requests.", - & ["product", "method", "version"] // it doesn't matter for the label order - ).unwrap(); -} - -lazy_static! { - // You can also use default flush duration which is 1 second. - // pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs); - pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs, std::time::Duration::from_secs(1)); -} +pub static HTTP_COUNTER_VEC: LazyLock = LazyLock::new(|| { + register_counter_vec!( + "http_requests_total", + "Number of HTTP requests.", + & ["product", "method", "version"] // it doesn't matter for the label order + ) + .unwrap() +}); + +// You can also use default flush duration which is 1 second. +// pub static TLS_HTTP_COUNTER: LazyLock = LazyLock;;new(|| auto_flush_from!(HTTP_COUNTER_VEC, Lhrs)); +pub static TLS_HTTP_COUNTER: LazyLock = LazyLock::new(|| auto_flush_from!(HTTP_COUNTER_VEC, Lhrs, std::time::Duration::from_secs(1))); fn main() { TLS_HTTP_COUNTER.foo.post.http1.inc(); diff --git a/static-metric/examples/advanced.rs b/static-metric/examples/advanced.rs index 5a788e71..e8a1f733 100644 --- a/static-metric/examples/advanced.rs +++ b/static-metric/examples/advanced.rs @@ -1,8 +1,9 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. +use std::sync::LazyLock; + use prometheus::IntCounterVec; -use lazy_static::lazy_static; use prometheus::register_int_counter_vec; use prometheus_static_metric::make_static_metric; @@ -25,17 +26,17 @@ make_static_metric! { } } -lazy_static! { - pub static ref HTTP_COUNTER_VEC: IntCounterVec = - register_int_counter_vec!( - "http_requests_total", - "Number of HTTP requests.", - &["product", "method", "version"] // it doesn't matter for the label order - ).unwrap(); +pub static HTTP_COUNTER_VEC: LazyLock = LazyLock::new(|| { + register_int_counter_vec!( + "http_requests_total", + "Number of HTTP requests.", + &["product", "method", "version"] // it doesn't matter for the label order + ) + .unwrap() +}); - pub static ref HTTP_COUNTER: HttpRequestStatistics = HttpRequestStatistics - ::from(&HTTP_COUNTER_VEC); -} +pub static HTTP_COUNTER: LazyLock = + LazyLock::new(|| HttpRequestStatistics::from(&HTTP_COUNTER_VEC)); /// This example demonstrates the usage of: /// 1. using alternative metric types (i.e. IntCounter) diff --git a/static-metric/examples/local.rs b/static-metric/examples/local.rs index 5ea9247f..24fda222 100644 --- a/static-metric/examples/local.rs +++ b/static-metric/examples/local.rs @@ -1,10 +1,9 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -use std::cell::Cell; +use std::{cell::Cell, sync::LazyLock}; use prometheus::*; -use lazy_static::lazy_static; use prometheus_static_metric::make_static_metric; make_static_metric! { @@ -28,14 +27,14 @@ make_static_metric! { } } -lazy_static! { - pub static ref HTTP_COUNTER_VEC: IntCounterVec = - register_int_counter_vec!( - "http_requests_total", - "Number of HTTP requests.", - &["product", "method", "version"] // it doesn't matter for the label order - ).unwrap(); -} +pub static HTTP_COUNTER_VEC: LazyLock = LazyLock::new(|| { + register_int_counter_vec!( + "http_requests_total", + "Number of HTTP requests.", + &["product", "method", "version"] // it doesn't matter for the label order + ) + .unwrap() +}); thread_local! { static THREAD_LAST_TICK_TIME: Cell = Cell::new(timer::now_millis()); diff --git a/static-metric/examples/make_auto_flush_static_counter.rs b/static-metric/examples/make_auto_flush_static_counter.rs index 4c6fa1bb..65a687f2 100644 --- a/static-metric/examples/make_auto_flush_static_counter.rs +++ b/static-metric/examples/make_auto_flush_static_counter.rs @@ -5,9 +5,10 @@ Use metric enums to reuse possible values of a label. */ +use std::sync::LazyLock; + use prometheus::*; -use lazy_static::lazy_static; use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric}; make_auto_flush_static_metric! { @@ -34,31 +35,28 @@ make_auto_flush_static_metric! { } } -lazy_static! { - pub static ref HTTP_COUNTER_VEC: IntCounterVec = - register_int_counter_vec ! ( - "http_requests_total", - "Number of HTTP requests.", - & ["product", "method", "version"] // it doesn't matter for the label order - ).unwrap(); -} +pub static HTTP_COUNTER_VEC: LazyLock = LazyLock::new(|| { + register_int_counter_vec!( + "http_requests_total", + "Number of HTTP requests.", + &["product", "method", "version"] // it doesn't matter for the label order + ) + .unwrap() +}); // Macro expanded code of auto_flush_from! -// lazy_static! { -// pub static ref TLS_HTTP_COUNTER: Lhrs = { -// thread_local! { -// pub static TLS_HTTP_COUNTER_INNER: LhrsInner = LhrsInner::from(& HTTP_COUNTER_VEC); -// } -// Lhrs::from(&TLS_HTTP_COUNTER_INNER) -// }; -// } +// pub static TLS_HTTP_COUNTER: LazyLock = LazyLock::new(|| { +// thread_local! { +// pub static TLS_HTTP_COUNTER_INNER: LhrsInner = LhrsInner::from(& HTTP_COUNTER_VEC); +// } +// Lhrs::from(&TLS_HTTP_COUNTER_INNER) +// }); // -lazy_static! { - // You can also use default flush duration which is 1 second. - // pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs); - pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs, std::time::Duration::from_secs(1)); -} +// You can also use default flush duration which is 1 second. +// pub static TLS_HTTP_COUNTER: LazyLock = LazyLock::new(|| auto_flush_from!(HTTP_COUNTER_VEC, Lhrs)); +pub static TLS_HTTP_COUNTER: LazyLock = + LazyLock::new(|| auto_flush_from!(HTTP_COUNTER_VEC, Lhrs, std::time::Duration::from_secs(1))); fn main() { TLS_HTTP_COUNTER.foo.post.http1.inc(); @@ -101,7 +99,7 @@ use std::mem; use std::mem::MaybeUninit; use std::thread::LocalKey; -use lazy_static::lazy_static; +use std::sync::LazyLock; #[allow(dead_code)] #[allow(non_camel_case_types)] @@ -367,22 +365,18 @@ impl Lhrs { } } -lazy_static! { -pub static ref HTTP_COUNTER_VEC: IntCounterVec = +pub static ref HTTP_COUNTER_VEC: LazyLock = LazyLock::new(|| register_int_counter_vec ! ( "http_requests_total", "Number of HTTP requests.", & ["product", "method", "version"] // it doesn't matter for the label order -).unwrap(); -} +).unwrap()); thread_local! { pub static TLS_HTTP_COUNTER_INNER: LhrsInner = LhrsInner::from(& HTTP_COUNTER_VEC); } -lazy_static! { - pub static ref TLS_HTTP_COUNTER: Lhrs = Lhrs::from(&TLS_HTTP_COUNTER_INNER); -} +pub static ref TLS_HTTP_COUNTER: LazyLock = LazyLock::new(|| Lhrs::from(&TLS_HTTP_COUNTER_INNER)); fn main() { TLS_HTTP_COUNTER.foo.post.http1.inc(); diff --git a/static-metric/examples/make_auto_flush_static_metric_histogram.rs b/static-metric/examples/make_auto_flush_static_metric_histogram.rs index 9ae3247f..d3fe1030 100644 --- a/static-metric/examples/make_auto_flush_static_metric_histogram.rs +++ b/static-metric/examples/make_auto_flush_static_metric_histogram.rs @@ -6,9 +6,10 @@ Use metric enums to reuse possible values of a label. */ +use std::sync::LazyLock; + use prometheus::*; -use lazy_static::lazy_static; use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric}; make_auto_flush_static_metric! { @@ -35,19 +36,17 @@ make_auto_flush_static_metric! { } } -lazy_static! { - pub static ref HTTP_HISTO_VEC: HistogramVec = - register_histogram_vec ! ( - "http_requests_total", - "Number of HTTP requests.", - & ["product", "method", "version"] // it doesn't matter for the label order - ).unwrap(); -} +pub static HTTP_HISTO_VEC: LazyLock = LazyLock::new(|| { + register_histogram_vec!( + "http_requests_total", + "Number of HTTP requests.", + &["product", "method", "version"] // it doesn't matter for the label order + ) + .unwrap() +}); -lazy_static! { - pub static ref TLS_HTTP_COUNTER: Lhrs = - auto_flush_from!(HTTP_HISTO_VEC, Lhrs, std::time::Duration::from_secs(1)); -} +pub static TLS_HTTP_COUNTER: LazyLock = + LazyLock::new(|| auto_flush_from!(HTTP_HISTO_VEC, Lhrs, std::time::Duration::from_secs(1))); fn main() { TLS_HTTP_COUNTER.foo.post.http1.observe(1.0); @@ -97,8 +96,6 @@ use std::mem; use std::mem::MaybeUninit; use std::thread::LocalKey; -use lazy_static::lazy_static; - #[allow(dead_code)] #[allow(non_camel_case_types)] #[derive(Clone, Copy, PartialEq)] @@ -362,22 +359,18 @@ impl Lhrs { } } -lazy_static! { -pub static ref HTTP_HISTO_VEC: HistogramVec = +pub static HTTP_HISTO_VEC: LazyLock = LazyLock::new(|| register_histogram_vec ! ( "http_requests_total", "Number of HTTP requests.", & ["product", "method", "version"] // it doesn't matter for the label order -).unwrap(); -} +).unwrap()); thread_local! { pub static TLS_HTTP_COUNTER_INNER: LhrsInner = LhrsInner::from(& HTTP_HISTO_VEC); } -lazy_static! { - pub static ref TLS_HTTP_COUNTER: Lhrs = Lhrs::from(&TLS_HTTP_COUNTER_INNER); -} +pub static TLS_HTTP_COUNTER: LazyLock = LazyLock::new(|| Lhrs::from(&TLS_HTTP_COUNTER_INNER)); fn main() { TLS_HTTP_COUNTER.foo.post.http1.observe(1.0); diff --git a/static-metric/examples/register_integration.rs b/static-metric/examples/register_integration.rs index 6c0627a7..1f2e4121 100644 --- a/static-metric/examples/register_integration.rs +++ b/static-metric/examples/register_integration.rs @@ -7,9 +7,10 @@ by using the `register_static_xxx!` macro provided by this crate. */ +use std::sync::LazyLock; + use prometheus::exponential_buckets; -use lazy_static::lazy_static; use prometheus::{register_counter_vec, register_histogram_vec}; use prometheus_static_metric::{ make_static_metric, register_static_counter_vec, register_static_histogram_vec, @@ -38,23 +39,25 @@ make_static_metric! { } } -lazy_static! { - pub static ref HTTP_COUNTER: HttpRequestStatistics = register_static_counter_vec!( +pub static HTTP_COUNTER: LazyLock = LazyLock::new(|| { + register_static_counter_vec!( HttpRequestStatistics, "http_requests_total", "Number of HTTP requests.", &["method", "product"] ) - .unwrap(); - pub static ref HTTP_DURATION: HttpRequestDuration = register_static_histogram_vec!( + .unwrap() +}); +pub static HTTP_DURATION: LazyLock = LazyLock::new(|| { + register_static_histogram_vec!( HttpRequestDuration, "http_request_duration", "Duration of each HTTP request.", &["method"], exponential_buckets(0.0005, 2.0, 20).unwrap() ) - .unwrap(); -} + .unwrap() +}); fn main() { HTTP_COUNTER.post.foo.inc(); diff --git a/static-metric/examples/with_lazy_static.rs b/static-metric/examples/with_lazy_static.rs index 198a1c39..874be690 100644 --- a/static-metric/examples/with_lazy_static.rs +++ b/static-metric/examples/with_lazy_static.rs @@ -1,8 +1,9 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. +use std::sync::LazyLock; + use prometheus::CounterVec; -use lazy_static::lazy_static; use prometheus::register_counter_vec; use prometheus_static_metric::make_static_metric; @@ -21,16 +22,16 @@ make_static_metric! { } } -lazy_static! { - pub static ref HTTP_COUNTER_VEC: CounterVec = register_counter_vec!( +pub static HTTP_COUNTER_VEC: LazyLock = LazyLock::new(|| { + register_counter_vec!( "http_requests_total", "Number of HTTP requests.", &["method", "product"] ) - .unwrap(); - pub static ref HTTP_COUNTER: HttpRequestStatistics = - HttpRequestStatistics::from(&HTTP_COUNTER_VEC); -} + .unwrap() +}); +pub static HTTP_COUNTER: LazyLock = + LazyLock::new(|| HttpRequestStatistics::from(&HTTP_COUNTER_VEC)); fn main() { HTTP_COUNTER.post.foo.inc(); diff --git a/static-metric/src/lib.rs b/static-metric/src/lib.rs index a1c85ffa..97eb6edc 100644 --- a/static-metric/src/lib.rs +++ b/static-metric/src/lib.rs @@ -8,13 +8,11 @@ This is useful since it reduces the amount of branching and processing needed at ```rust use prometheus::{self, IntCounter, TextEncoder, Encoder}; -use lazy_static::lazy_static; +use std::sync::LazyLock; use prometheus::register_int_counter; -lazy_static! { - static ref HIGH_FIVE_COUNTER: IntCounter = - register_int_counter!("highfives", "Number of high fives recieved").unwrap(); -} +static HIGH_FIVE_COUNTER: LazyLock = + LazyLock::new(|| register_int_counter!("highfives", "Number of high fives recieved").unwrap()); HIGH_FIVE_COUNTER.inc(); assert_eq!(HIGH_FIVE_COUNTER.get(), 1);