diff --git a/Cargo.toml b/Cargo.toml index f12ee553..abad055a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,6 @@ push = ["reqwest", "libc", "protobuf"] [dependencies] cfg-if = "^1.0" fnv = "^1.0" -lazy_static = "^1.4" libc = { version = "^0.2", optional = true } parking_lot = "^0.12" protobuf = { version = "^3.7.2", optional = true } diff --git a/examples/example_custom_registry.rs b/examples/example_custom_registry.rs index 0c99cb31..5a9d4648 100644 --- a/examples/example_custom_registry.rs +++ b/examples/example_custom_registry.rs @@ -4,15 +4,14 @@ //! and how to perform registration across function boundaries. use std::collections::HashMap; +use std::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..3e621f38 100644 --- a/examples/example_int_metrics.rs +++ b/examples/example_int_metrics.rs @@ -1,21 +1,21 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec}; +use std::sync::LazyLock; -use lazy_static::lazy_static; use prometheus::{ register_int_counter, register_int_counter_vec, register_int_gauge, register_int_gauge_vec, }; +use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec}; -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..1f954194 100644 --- a/examples/example_push.rs +++ b/examples/example_push.rs @@ -1,27 +1,28 @@ // 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}; +use prometheus::{Counter, 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/proto/proto_model.rs b/proto/proto_model.rs index 626cbd13..b67ef0e5 100644 --- a/proto/proto_model.rs +++ b/proto/proto_model.rs @@ -1,5 +1,5 @@ // This file is generated by rust-protobuf 3.7.2. Do not edit -// .proto file is parsed by protoc 29.3 +// .proto file is parsed by protoc 3.21.12 // @generated // https://github.com/rust-lang/rust-clippy/issues/702 diff --git a/src/lib.rs b/src/lib.rs index 11289e26..a3312220 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,19 +49,16 @@ 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 -some metrics. +`LazyLock` to quickly build up and collect some metrics. ```rust -use prometheus::{self, IntCounter, TextEncoder, Encoder}; +use std::sync::LazyLock; -use lazy_static::lazy_static; +use prometheus::{self, IntCounter, TextEncoder, Encoder}; 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); @@ -72,17 +69,15 @@ By default, this registers with a default registry. To make a report, you can ca [`Encoder`](trait.Encoder.html) and report to Promethus. ``` +use std::sync::LazyLock; # use prometheus::IntCounter; use prometheus::{self, TextEncoder, Encoder}; -use lazy_static::lazy_static; 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/src/process_collector.rs b/src/process_collector.rs index 136efc9a..5769cc7f 100644 --- a/src/process_collector.rs +++ b/src/process_collector.rs @@ -4,7 +4,7 @@ //! //! This module only supports **Linux** platform. -use lazy_static::lazy_static; +use std::sync::LazyLock; use crate::counter::IntCounter; use crate::desc::Desc; @@ -190,21 +190,13 @@ impl Collector for ProcessCollector { } } -lazy_static! { - // getconf CLK_TCK - static ref CLK_TCK: i64 = { - unsafe { - libc::sysconf(libc::_SC_CLK_TCK) - }.into() - }; - - // getconf PAGESIZE - static ref PAGESIZE: i64 = { - unsafe { - libc::sysconf(libc::_SC_PAGESIZE) - }.into() - }; -} +// getconf CLK_TCK +static CLK_TCK: LazyLock = + LazyLock::new(|| unsafe { libc::sysconf(libc::_SC_CLK_TCK) }.into()); + +// getconf PAGESIZE +static PAGESIZE: LazyLock = + LazyLock::new(|| unsafe { libc::sysconf(libc::_SC_PAGESIZE) }.into()); #[cfg(test)] mod tests { diff --git a/src/push.rs b/src/push.rs index be5961ac..5fd05cb4 100644 --- a/src/push.rs +++ b/src/push.rs @@ -4,14 +4,13 @@ use std::collections::HashMap; use std::hash::BuildHasher; use std::str::{self, FromStr}; +use std::sync::LazyLock; use std::time::Duration; use reqwest::blocking::Client; use reqwest::header::CONTENT_TYPE; use reqwest::{Method, StatusCode, Url}; -use lazy_static::lazy_static; - use crate::encoder::{Encoder, ProtobufEncoder}; use crate::errors::{Error, Result}; use crate::metrics::Collector; @@ -20,12 +19,12 @@ use crate::registry::Registry; const REQWEST_TIMEOUT_SEC: Duration = Duration::from_secs(10); -lazy_static! { - static ref HTTP_CLIENT: Client = Client::builder() +static HTTP_CLIENT: LazyLock = LazyLock::new(|| { + Client::builder() .timeout(REQWEST_TIMEOUT_SEC) .build() - .unwrap(); -} + .unwrap() +}); /// `BasicAuthentication` holder for supporting `push` to Pushgateway endpoints /// using Basic access authentication. diff --git a/src/registry.rs b/src/registry.rs index a36993c9..d6b7ea9f 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -5,6 +5,7 @@ use std::collections::btree_map::Entry as BEntry; use std::collections::hash_map::Entry as HEntry; use std::collections::{BTreeMap, HashMap, HashSet}; use std::sync::Arc; +use std::sync::LazyLock; use parking_lot::RwLock; @@ -13,7 +14,6 @@ use crate::metrics::Collector; use crate::proto; use cfg_if::cfg_if; -use lazy_static::lazy_static; #[derive(Default)] struct RegistryCore { @@ -293,20 +293,17 @@ cfg_if! { } // Default registry for rust-prometheus. -lazy_static! { - static ref DEFAULT_REGISTRY: Registry = { - let reg = Registry::default(); +static DEFAULT_REGISTRY: LazyLock = LazyLock::new(|| { + let reg = Registry::default(); - // Register a default process collector. - register_default_process_collector(®).unwrap(); + // Register a default process collector. + register_default_process_collector(®).unwrap(); - reg - }; -} + reg +}); /// Default registry (global static). pub fn default_registry() -> &'static Registry { - lazy_static::initialize(&DEFAULT_REGISTRY); &DEFAULT_REGISTRY } diff --git a/src/timer.rs b/src/timer.rs index 7530c72d..c0a61d14 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -1,14 +1,11 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::LazyLock; use std::thread; use std::time::{Duration, Instant}; -use lazy_static::lazy_static; - /// Milliseconds since ANCHOR. static RECENT: AtomicU64 = AtomicU64::new(0); -lazy_static! { - static ref ANCHOR: Instant = Instant::now(); -} +static ANCHOR: LazyLock = LazyLock::new(Instant::now); /// Convert a duration to millisecond. #[inline] @@ -39,9 +36,7 @@ pub fn recent_millis() -> u64 { RECENT.load(Ordering::Relaxed) } -lazy_static! { - static ref UPDATER_IS_RUNNING: AtomicBool = AtomicBool::new(false); -} +static UPDATER_IS_RUNNING: LazyLock = LazyLock::new(AtomicBool::default); const CHECK_UPDATE_INTERVAL: Duration = Duration::from_millis(200); diff --git a/static-metric/Cargo.toml b/static-metric/Cargo.toml index 88e0e735..503fb80a 100644 --- a/static-metric/Cargo.toml +++ b/static-metric/Cargo.toml @@ -16,7 +16,6 @@ proc-macro = true syn = { version = "2.0", features = ["full", "extra-traits"] } proc-macro2 = "1.0" quote = "1.0" -lazy_static = "1.4" [dev-dependencies] criterion = "0.5" diff --git a/static-metric/README.md b/static-metric/README.md index cdf66a1e..553284a4 100644 --- a/static-metric/README.md +++ b/static-metric/README.md @@ -89,9 +89,9 @@ fn main() { For heavier scenario that a global shared static-metric might not be effecient enough, you can use `make_auto_flush_static_metric!` macro, which will store data in local thread storage, with a custom rate to flush to global `MetricVec`. ```rust -use prometheus::*; +use std::sync::LazyLock; -use lazy_static:lazy_static; +use prometheus::*; 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_int_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 ref TLS_HTTP_COUNTER: Lhrs = 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..92240837 100644 --- a/static-metric/examples/advanced.rs +++ b/static-metric/examples/advanced.rs @@ -1,9 +1,9 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -use prometheus::IntCounterVec; +use std::sync::LazyLock; -use lazy_static::lazy_static; use prometheus::register_int_counter_vec; +use prometheus::IntCounterVec; use prometheus_static_metric::make_static_metric; make_static_metric! { @@ -25,17 +25,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..3067df5a 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::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..940c2fef 100644 --- a/static-metric/examples/make_auto_flush_static_counter.rs +++ b/static-metric/examples/make_auto_flush_static_counter.rs @@ -5,9 +5,9 @@ Use metric enums to reuse possible values of a label. */ -use prometheus::*; +use std::sync::LazyLock; -use lazy_static::lazy_static; +use prometheus::*; use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric}; make_auto_flush_static_metric! { @@ -34,31 +34,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 = { +// 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 ref TLS_HTTP_COUNTER: Lhrs = 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(); @@ -98,10 +95,10 @@ use prometheus::local::*; use prometheus::*; use std::collections::HashMap; use std::mem; +use std::sync::LazyLock; use std::mem::MaybeUninit; use std::thread::LocalKey; -use lazy_static::lazy_static; #[allow(dead_code)] #[allow(non_camel_case_types)] @@ -367,22 +364,19 @@ impl Lhrs { } } -lazy_static! { -pub static ref HTTP_COUNTER_VEC: IntCounterVec = -register_int_counter_vec ! ( +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(); +).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 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..81096e1b 100644 --- a/static-metric/examples/make_auto_flush_static_metric_histogram.rs +++ b/static-metric/examples/make_auto_flush_static_metric_histogram.rs @@ -5,10 +5,9 @@ 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 +34,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); @@ -96,8 +93,7 @@ use std::collections::HashMap; 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)] @@ -362,22 +358,19 @@ impl Lhrs { } } -lazy_static! { -pub static ref HTTP_HISTO_VEC: HistogramVec = -register_histogram_vec ! ( +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..33103333 100644 --- a/static-metric/examples/register_integration.rs +++ b/static-metric/examples/register_integration.rs @@ -7,9 +7,9 @@ by using the `register_static_xxx!` macro provided by this crate. */ -use prometheus::exponential_buckets; +use std::sync::LazyLock; -use lazy_static::lazy_static; +use prometheus::exponential_buckets; 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 +38,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_lazylock.rs similarity index 76% rename from static-metric/examples/with_lazy_static.rs rename to static-metric/examples/with_lazylock.rs index 198a1c39..98fa023b 100644 --- a/static-metric/examples/with_lazy_static.rs +++ b/static-metric/examples/with_lazylock.rs @@ -1,10 +1,9 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -use prometheus::CounterVec; - -use lazy_static::lazy_static; use prometheus::register_counter_vec; +use prometheus::CounterVec; use prometheus_static_metric::make_static_metric; +use std::sync::LazyLock; make_static_metric! { pub struct HttpRequestStatistics: Counter { @@ -21,16 +20,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..ba1edb71 100644 --- a/static-metric/src/lib.rs +++ b/static-metric/src/lib.rs @@ -6,15 +6,13 @@ This crate provides staticly built metrics to your Prometheus application. This is useful since it reduces the amount of branching and processing needed at runtime to collect metrics. ```rust +use std::sync::LazyLock; use prometheus::{self, IntCounter, TextEncoder, Encoder}; - -use lazy_static::lazy_static; 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);