Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Replace lazy_static with once_cell #450

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ push = ["reqwest", "libc", "protobuf"]
[dependencies]
cfg-if = "^1.0"
fnv = "^1.0"
lazy_static = "^1.4"
once_cell = "^1.0"
libc = { version = "^0.2", optional = true }
parking_lot = "^0.12"
protobuf = { version = "^2.0", optional = true }
Expand Down
10 changes: 5 additions & 5 deletions examples/example_custom_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use std::collections::HashMap;

use prometheus::{Encoder, IntCounter, Registry};

use lazy_static::lazy_static;
use once_cell::sync::Lazy;

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: Lazy<IntCounter> =
Lazy::new(|| IntCounter::new("default", "generic counter").unwrap());
static CUSTOM_COUNTER: Lazy<IntCounter> =
Lazy::new(|| IntCounter::new("custom", "dedicated counter").unwrap());

fn main() {
// Register default metrics.
Expand Down
22 changes: 13 additions & 9 deletions examples/example_hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,33 @@ use hyper::{
};
use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder};

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use prometheus::{labels, opts, register_counter, register_gauge, register_histogram_vec};

lazy_static! {
static ref HTTP_COUNTER: Counter = register_counter!(opts!(
static HTTP_COUNTER: Lazy<Counter> = Lazy::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: Lazy<Gauge> = Lazy::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: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
"example_http_request_duration_seconds",
"The HTTP request latencies in seconds.",
&["handler"]
)
.unwrap();
}
.unwrap()
});

async fn serve_req(_req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
let encoder = TextEncoder::new();
Expand Down
19 changes: 9 additions & 10 deletions examples/example_int_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec};

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
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: Lazy<IntCounter> =
Lazy::new(|| register_int_counter!("A_int_counter", "foobar").unwrap());
static A_INT_COUNTER_VEC: Lazy<IntCounterVec> =
Lazy::new(|| register_int_counter_vec!("A_int_counter_vec", "foobar", &["a", "b"]).unwrap());
static A_INT_GAUGE: Lazy<IntGauge> =
Lazy::new(|| register_int_gauge!("A_int_gauge", "foobar").unwrap());
static A_INT_GAUGE_VEC: Lazy<IntGaugeVec> =
Lazy::new(|| register_int_gauge_vec!("A_int_gauge_vec", "foobar", &["a", "b"]).unwrap());

fn main() {
A_INT_COUNTER.inc();
Expand Down
20 changes: 12 additions & 8 deletions examples/example_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ use std::time;
use getopts::Options;
use prometheus::{Counter, Histogram};

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use prometheus::{labels, register_counter, register_histogram};

lazy_static! {
static ref PUSH_COUNTER: Counter = register_counter!(
static PUSH_COUNTER: Lazy<Counter> = Lazy::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: Lazy<Histogram> = Lazy::new(|| {
register_histogram!(
"example_push_request_duration_seconds",
"The push request latencies in seconds."
)
.unwrap();
}
.unwrap()
});

#[cfg(feature = "push")]
fn main() {
Expand All @@ -47,7 +49,9 @@ fn main() {
}
println!("Pushing, please start Pushgateway first.");

let address = matches.opt_str("A").unwrap_or("127.0.0.1:9091".to_owned());
let address = matches
.opt_str("A")
.unwrap_or_else(|| "127.0.0.1:9091".to_owned());
for _ in 0..5 {
thread::sleep(time::Duration::from_secs(2));
PUSH_COUNTER.inc();
Expand Down
Loading