Skip to content

Commit d71dc83

Browse files
committed
Remove lazy_static dependency from main crate
Signed-off-by: Thomas Habets <[email protected]>
1 parent 1d3174b commit d71dc83

File tree

11 files changed

+71
-90
lines changed

11 files changed

+71
-90
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ push = ["reqwest", "libc", "protobuf"]
2525
[dependencies]
2626
cfg-if = "^1.0"
2727
fnv = "^1.0"
28-
lazy_static = "^1.4"
2928
libc = { version = "^0.2", optional = true }
3029
parking_lot = "^0.12"
3130
protobuf = { version = "^3.7.2", optional = true }

examples/example_custom_registry.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
//! and how to perform registration across function boundaries.
55
66
use std::collections::HashMap;
7+
use std::sync::LazyLock;
78

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

10-
use lazy_static::lazy_static;
11-
12-
lazy_static! {
13-
static ref DEFAULT_COUNTER: IntCounter = IntCounter::new("default", "generic counter").unwrap();
14-
static ref CUSTOM_COUNTER: IntCounter = IntCounter::new("custom", "dedicated counter").unwrap();
15-
}
11+
static DEFAULT_COUNTER: LazyLock<IntCounter> =
12+
LazyLock::new(|| IntCounter::new("default", "generic counter").unwrap());
13+
static CUSTOM_COUNTER: LazyLock<IntCounter> =
14+
LazyLock::new(|| IntCounter::new("custom", "dedicated counter").unwrap());
1615

1716
fn main() {
1817
// Register default metrics.

examples/example_hyper.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

33
use std::net::SocketAddr;
4+
use std::sync::LazyLock;
45

56
use hyper::body::Incoming;
67
use hyper::header::CONTENT_TYPE;
@@ -9,33 +10,36 @@ use hyper::service::service_fn;
910
use hyper::Request;
1011
use hyper::Response;
1112
use hyper_util::rt::TokioIo;
12-
use lazy_static::lazy_static;
1313
use prometheus::{labels, opts, register_counter, register_gauge, register_histogram_vec};
1414
use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder};
1515
use tokio::net::TcpListener;
1616

1717
type BoxedErr = Box<dyn std::error::Error + Send + Sync + 'static>;
1818

19-
lazy_static! {
20-
static ref HTTP_COUNTER: Counter = register_counter!(opts!(
19+
static HTTP_COUNTER: LazyLock<Counter> = LazyLock::new(|| {
20+
register_counter!(opts!(
2121
"example_http_requests_total",
2222
"Number of HTTP requests made.",
2323
labels! {"handler" => "all",}
2424
))
25-
.unwrap();
26-
static ref HTTP_BODY_GAUGE: Gauge = register_gauge!(opts!(
25+
.unwrap()
26+
});
27+
static HTTP_BODY_GAUGE: LazyLock<Gauge> = LazyLock::new(|| {
28+
register_gauge!(opts!(
2729
"example_http_response_size_bytes",
2830
"The HTTP response sizes in bytes.",
2931
labels! {"handler" => "all",}
3032
))
31-
.unwrap();
32-
static ref HTTP_REQ_HISTOGRAM: HistogramVec = register_histogram_vec!(
33+
.unwrap()
34+
});
35+
static HTTP_REQ_HISTOGRAM: LazyLock<HistogramVec> = LazyLock::new(|| {
36+
register_histogram_vec!(
3337
"example_http_request_duration_seconds",
3438
"The HTTP request latencies in seconds.",
3539
&["handler"]
3640
)
37-
.unwrap();
38-
}
41+
.unwrap()
42+
});
3943

4044
async fn serve_req(_req: Request<Incoming>) -> Result<Response<String>, BoxedErr> {
4145
let encoder = TextEncoder::new();

examples/example_int_metrics.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

3-
use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec};
3+
use std::sync::LazyLock;
44

5-
use lazy_static::lazy_static;
65
use prometheus::{
76
register_int_counter, register_int_counter_vec, register_int_gauge, register_int_gauge_vec,
87
};
8+
use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec};
99

10-
lazy_static! {
11-
static ref A_INT_COUNTER: IntCounter =
12-
register_int_counter!("A_int_counter", "foobar").unwrap();
13-
static ref A_INT_COUNTER_VEC: IntCounterVec =
14-
register_int_counter_vec!("A_int_counter_vec", "foobar", &["a", "b"]).unwrap();
15-
static ref A_INT_GAUGE: IntGauge = register_int_gauge!("A_int_gauge", "foobar").unwrap();
16-
static ref A_INT_GAUGE_VEC: IntGaugeVec =
17-
register_int_gauge_vec!("A_int_gauge_vec", "foobar", &["a", "b"]).unwrap();
18-
}
10+
static A_INT_COUNTER: LazyLock<IntCounter> =
11+
LazyLock::new(|| register_int_counter!("A_int_counter", "foobar").unwrap());
12+
static A_INT_COUNTER_VEC: LazyLock<IntCounterVec> = LazyLock::new(|| {
13+
register_int_counter_vec!("A_int_counter_vec", "foobar", &["a", "b"]).unwrap()
14+
});
15+
static A_INT_GAUGE: LazyLock<IntGauge> =
16+
LazyLock::new(|| register_int_gauge!("A_int_gauge", "foobar").unwrap());
17+
static A_INT_GAUGE_VEC: LazyLock<IntGaugeVec> =
18+
LazyLock::new(|| register_int_gauge_vec!("A_int_gauge_vec", "foobar", &["a", "b"]).unwrap());
1919

2020
fn main() {
2121
A_INT_COUNTER.inc();

examples/example_push.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

33
use std::env;
4+
use std::sync::LazyLock;
45
use std::thread;
56
use std::time;
67

78
use getopts::Options;
8-
use prometheus::{Counter, Histogram};
9-
10-
use lazy_static::lazy_static;
119
use prometheus::{labels, register_counter, register_histogram};
10+
use prometheus::{Counter, Histogram};
1211

13-
lazy_static! {
14-
static ref PUSH_COUNTER: Counter = register_counter!(
12+
static PUSH_COUNTER: LazyLock<Counter> = LazyLock::new(|| {
13+
register_counter!(
1514
"example_push_total",
1615
"Total number of prometheus client pushed."
1716
)
18-
.unwrap();
19-
static ref PUSH_REQ_HISTOGRAM: Histogram = register_histogram!(
17+
.unwrap()
18+
});
19+
static PUSH_REQ_HISTOGRAM: LazyLock<Histogram> = LazyLock::new(|| {
20+
register_histogram!(
2021
"example_push_request_duration_seconds",
2122
"The push request latencies in seconds."
2223
)
23-
.unwrap();
24-
}
24+
.unwrap()
25+
});
2526

2627
fn main() {
2728
let args: Vec<String> = env::args().collect();

proto/proto_model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is generated by rust-protobuf 3.7.2. Do not edit
2-
// .proto file is parsed by protoc 29.3
2+
// .proto file is parsed by protoc 3.21.12
33
// @generated
44

55
// https://github.com/rust-lang/rust-clippy/issues/702

src/lib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,16 @@ You can find more examples within
4949
# Static Metrics
5050
5151
This crate supports staticly built metrics. You can use it with
52-
[`lazy_static`](https://docs.rs/lazy_static/) to quickly build up and collect
53-
some metrics.
52+
`LazyLock` to quickly build up and collect some metrics.
5453
5554
```rust
56-
use prometheus::{self, IntCounter, TextEncoder, Encoder};
55+
use std::sync::LazyLock;
5756
58-
use lazy_static::lazy_static;
57+
use prometheus::{self, IntCounter, TextEncoder, Encoder};
5958
use prometheus::register_int_counter;
6059
61-
lazy_static! {
62-
static ref HIGH_FIVE_COUNTER: IntCounter =
63-
register_int_counter!("highfives", "Number of high fives received").unwrap();
64-
}
60+
static HIGH_FIVE_COUNTER: LazyLock<IntCounter> =
61+
LazyLock::new(|| register_int_counter!("highfives", "Number of high fives received").unwrap());
6562
6663
HIGH_FIVE_COUNTER.inc();
6764
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
7269
[`Encoder`](trait.Encoder.html) and report to Promethus.
7370
7471
```
72+
use std::sync::LazyLock;
7573
# use prometheus::IntCounter;
7674
use prometheus::{self, TextEncoder, Encoder};
7775
78-
use lazy_static::lazy_static;
7976
use prometheus::register_int_counter;
8077
8178
// Register & measure some metrics.
82-
# lazy_static! {
83-
# static ref HIGH_FIVE_COUNTER: IntCounter =
84-
# register_int_counter!("highfives", "Number of high fives received").unwrap();
85-
# }
79+
# static HIGH_FIVE_COUNTER: LazyLock<IntCounter> = LazyLock::new(||
80+
# register_int_counter!("highfives", "Number of high fives received").unwrap());
8681
# HIGH_FIVE_COUNTER.inc();
8782
8883
let mut buffer = Vec::new();

src/process_collector.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//!
55
//! This module only supports **Linux** platform.
66
7-
use lazy_static::lazy_static;
7+
use std::sync::LazyLock;
88

99
use crate::counter::IntCounter;
1010
use crate::desc::Desc;
@@ -190,21 +190,13 @@ impl Collector for ProcessCollector {
190190
}
191191
}
192192

193-
lazy_static! {
194-
// getconf CLK_TCK
195-
static ref CLK_TCK: i64 = {
196-
unsafe {
197-
libc::sysconf(libc::_SC_CLK_TCK)
198-
}.into()
199-
};
200-
201-
// getconf PAGESIZE
202-
static ref PAGESIZE: i64 = {
203-
unsafe {
204-
libc::sysconf(libc::_SC_PAGESIZE)
205-
}.into()
206-
};
207-
}
193+
// getconf CLK_TCK
194+
static CLK_TCK: LazyLock<i64> =
195+
LazyLock::new(|| unsafe { libc::sysconf(libc::_SC_CLK_TCK) }.into());
196+
197+
// getconf PAGESIZE
198+
static PAGESIZE: LazyLock<i64> =
199+
LazyLock::new(|| unsafe { libc::sysconf(libc::_SC_PAGESIZE) }.into());
208200

209201
#[cfg(test)]
210202
mod tests {

src/push.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
use std::collections::HashMap;
55
use std::hash::BuildHasher;
66
use std::str::{self, FromStr};
7+
use std::sync::LazyLock;
78
use std::time::Duration;
89

910
use reqwest::blocking::Client;
1011
use reqwest::header::CONTENT_TYPE;
1112
use reqwest::{Method, StatusCode, Url};
1213

13-
use lazy_static::lazy_static;
14-
1514
use crate::encoder::{Encoder, ProtobufEncoder};
1615
use crate::errors::{Error, Result};
1716
use crate::metrics::Collector;
@@ -20,12 +19,12 @@ use crate::registry::Registry;
2019

2120
const REQWEST_TIMEOUT_SEC: Duration = Duration::from_secs(10);
2221

23-
lazy_static! {
24-
static ref HTTP_CLIENT: Client = Client::builder()
22+
static HTTP_CLIENT: LazyLock<Client> = LazyLock::new(|| {
23+
Client::builder()
2524
.timeout(REQWEST_TIMEOUT_SEC)
2625
.build()
27-
.unwrap();
28-
}
26+
.unwrap()
27+
});
2928

3029
/// `BasicAuthentication` holder for supporting `push` to Pushgateway endpoints
3130
/// using Basic access authentication.

src/registry.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::collections::btree_map::Entry as BEntry;
55
use std::collections::hash_map::Entry as HEntry;
66
use std::collections::{BTreeMap, HashMap, HashSet};
77
use std::sync::Arc;
8+
use std::sync::LazyLock;
89

910
use parking_lot::RwLock;
1011

@@ -13,7 +14,6 @@ use crate::metrics::Collector;
1314
use crate::proto;
1415

1516
use cfg_if::cfg_if;
16-
use lazy_static::lazy_static;
1717

1818
#[derive(Default)]
1919
struct RegistryCore {
@@ -293,20 +293,17 @@ cfg_if! {
293293
}
294294

295295
// Default registry for rust-prometheus.
296-
lazy_static! {
297-
static ref DEFAULT_REGISTRY: Registry = {
298-
let reg = Registry::default();
296+
static DEFAULT_REGISTRY: LazyLock<Registry> = LazyLock::new(|| {
297+
let reg = Registry::default();
299298

300-
// Register a default process collector.
301-
register_default_process_collector(&reg).unwrap();
299+
// Register a default process collector.
300+
register_default_process_collector(&reg).unwrap();
302301

303-
reg
304-
};
305-
}
302+
reg
303+
});
306304

307305
/// Default registry (global static).
308306
pub fn default_registry() -> &'static Registry {
309-
lazy_static::initialize(&DEFAULT_REGISTRY);
310307
&DEFAULT_REGISTRY
311308
}
312309

0 commit comments

Comments
 (0)