Skip to content

Commit

Permalink
Merge pull request #34 from CleverCloud/devel/fdubois/chore/once-cell
Browse files Browse the repository at this point in the history
Use `once_cell` instead of `lazy_static`
  • Loading branch information
FlorentinDUBOIS authored Mar 13, 2023
2 parents a7c7fda + a923f74 commit 1bd99bf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
## 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
Expand Down
20 changes: 12 additions & 8 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CounterVec> = 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<CounterVec> = 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
Expand Down

0 comments on commit 1bd99bf

Please sign in to comment.