Skip to content

Commit 1bd99bf

Browse files
Merge pull request #34 from CleverCloud/devel/fdubois/chore/once-cell
Use `once_cell` instead of `lazy_static`
2 parents a7c7fda + a923f74 commit 1bd99bf

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ hmac = { version = "^0.12.1", features = ["std"], optional = true }
2929
hyper = { version = "^0.14.25", default-features = false, optional = true }
3030
hyper-tls = { version = "^0.5.0", optional = true }
3131
hyper-proxy = { version = "^0.9.1", default-features = false, features = ["tls"], optional = true }
32-
lazy_static = { version = "^1.4.0", optional = true }
32+
once_cell = { version = "^1.17.1", optional = true }
3333
log = { version = "^0.4.17", optional = true }
3434
prometheus = { version = "^0.13.3", optional = true }
3535
serde = { version = "^1.0.155", features = ["derive"], optional = true }
@@ -68,5 +68,5 @@ client = [
6868
logging = ["log", "tracing/log-always"]
6969
trace = ["tracing", "tracing-futures"]
7070
tokio = ["tracing-futures/tokio"]
71-
metrics = ["lazy_static", "prometheus"]
71+
metrics = ["once_cell", "prometheus"]
7272
proxy = ["cidr", "headers", "hyper-proxy", "url"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
4747
## Features
4848

4949
| name | description |
50-
| ------- | ------------------------------------------------------------------------- |
50+
| ------- |---------------------------------------------------------------------------|
5151
| default | Default enable features are `client`, `logging`, `proxy` |
5252
| client | The oauth 1.0a client implementation |
5353
| logging | Use the `log` facility crate to print logs |
54-
| metrics | Use `lazy_static` and `prometheus` crates to register metrics |
54+
| metrics | Use `once_cell` and `prometheus` crates to register metrics |
5555
| proxy | Enable the support of environment variable `http_proxy` and `https_proxy` |
5656

5757
### Metrics

src/client/mod.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ use hyper::{
2727
header, Body, Method, StatusCode,
2828
};
2929
use hyper_tls::HttpsConnector;
30-
#[cfg(feature = "metrics")]
31-
use lazy_static::lazy_static;
3230
#[cfg(feature = "logging")]
3331
use log::{error, log_enabled, trace, Level};
3432
#[cfg(feature = "metrics")]
33+
use once_cell::sync::Lazy;
34+
#[cfg(feature = "metrics")]
3535
use prometheus::{opts, register_counter_vec, CounterVec};
3636
use serde::{de::DeserializeOwned, Deserialize, Serialize};
3737
use sha2::Sha512;
@@ -45,21 +45,25 @@ pub mod proxy;
4545
// Telemetry
4646

4747
#[cfg(feature = "metrics")]
48-
lazy_static! {
49-
static ref CLIENT_REQUEST: CounterVec = register_counter_vec!(
48+
static CLIENT_REQUEST: Lazy<CounterVec> = Lazy::new(|| {
49+
register_counter_vec!(
5050
opts!("oauth10a_client_request", "number of request on api"),
5151
&["endpoint", "method", "status"]
5252
)
53-
.expect("metrics 'oauth10a_client_request' to not be initialized");
54-
static ref CLIENT_REQUEST_DURATION: CounterVec = register_counter_vec!(
53+
.expect("metrics 'oauth10a_client_request' to not be initialized")
54+
});
55+
56+
#[cfg(feature = "metrics")]
57+
static CLIENT_REQUEST_DURATION: Lazy<CounterVec> = Lazy::new(|| {
58+
register_counter_vec!(
5559
opts!(
5660
"oauth10a_client_request_duration",
5761
"duration of request on api"
5862
),
5963
&["endpoint", "method", "status", "unit"]
6064
)
61-
.expect("metrics 'oauth10a_client_request_duration' to not be initialized");
62-
}
65+
.expect("metrics 'oauth10a_client_request_duration' to not be initialized")
66+
});
6367

6468
// -----------------------------------------------------------------------------
6569
// Types

0 commit comments

Comments
 (0)