diff --git a/Cargo.toml b/Cargo.toml index feffd54..f541fd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ hmac = { version = "^0.12.1", features = ["std"], optional = true } hyper = { version = "^0.14.25", default-features = false, optional = true } hyper-tls = { version = "^0.5.0", optional = true } hyper-proxy = { version = "^0.9.1", default-features = false, features = ["tls"], optional = true } -lazy_static = { version = "^1.4.0", optional = true } +once_cell = { version = "^1.17.1", optional = true } log = { version = "^0.4.17", optional = true } prometheus = { version = "^0.13.3", optional = true } serde = { version = "^1.0.155", features = ["derive"], optional = true } @@ -68,5 +68,5 @@ client = [ logging = ["log", "tracing/log-always"] trace = ["tracing", "tracing-futures"] tokio = ["tracing-futures/tokio"] -metrics = ["lazy_static", "prometheus"] +metrics = ["once_cell", "prometheus"] proxy = ["cidr", "headers", "hyper-proxy", "url"] diff --git a/README.md b/README.md index f139a53..c3257e7 100644 --- a/README.md +++ b/README.md @@ -47,11 +47,11 @@ async fn main() -> Result<(), Box> { ## Features | name | description | -| ------- | ------------------------------------------------------------------------- | +| ------- |---------------------------------------------------------------------------| | default | Default enable features are `client`, `logging`, `proxy` | | client | The oauth 1.0a client implementation | | logging | Use the `log` facility crate to print logs | -| metrics | Use `lazy_static` and `prometheus` crates to register metrics | +| metrics | Use `once_cell` and `prometheus` crates to register metrics | | proxy | Enable the support of environment variable `http_proxy` and `https_proxy` | ### Metrics diff --git a/src/client/mod.rs b/src/client/mod.rs index f008f83..a96804e 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -27,11 +27,11 @@ use hyper::{ header, Body, Method, StatusCode, }; use hyper_tls::HttpsConnector; -#[cfg(feature = "metrics")] -use lazy_static::lazy_static; #[cfg(feature = "logging")] use log::{error, log_enabled, trace, Level}; #[cfg(feature = "metrics")] +use once_cell::sync::Lazy; +#[cfg(feature = "metrics")] use prometheus::{opts, register_counter_vec, CounterVec}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use sha2::Sha512; @@ -45,21 +45,25 @@ pub mod proxy; // Telemetry #[cfg(feature = "metrics")] -lazy_static! { - static ref CLIENT_REQUEST: CounterVec = register_counter_vec!( +static CLIENT_REQUEST: Lazy = Lazy::new(|| { + register_counter_vec!( opts!("oauth10a_client_request", "number of request on api"), &["endpoint", "method", "status"] ) - .expect("metrics 'oauth10a_client_request' to not be initialized"); - static ref CLIENT_REQUEST_DURATION: CounterVec = register_counter_vec!( + .expect("metrics 'oauth10a_client_request' to not be initialized") +}); + +#[cfg(feature = "metrics")] +static CLIENT_REQUEST_DURATION: Lazy = Lazy::new(|| { + register_counter_vec!( opts!( "oauth10a_client_request_duration", "duration of request on api" ), &["endpoint", "method", "status", "unit"] ) - .expect("metrics 'oauth10a_client_request_duration' to not be initialized"); -} + .expect("metrics 'oauth10a_client_request_duration' to not be initialized") +}); // ----------------------------------------------------------------------------- // Types