Skip to content

Commit 09c8dd8

Browse files
committed
docs: Promote the usage of LazyLock instead of lazy_static
1 parent 1d3174b commit 09c8dd8

13 files changed

+140
-151
lines changed

examples/example_custom_registry.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
//! This examples shows how to use multiple and custom registries,
44
//! and how to perform registration across function boundaries.
55
6-
use std::collections::HashMap;
6+
use std::{collections::HashMap, sync::LazyLock};
77

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

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-
}
10+
static DEFAULT_COUNTER: LazyLock<IntCounter> =
11+
LazyLock::new(|| IntCounter::new("default", "generic counter").unwrap());
12+
static CUSTOM_COUNTER: LazyLock<IntCounter> =
13+
LazyLock::new(|| IntCounter::new("custom", "dedicated counter").unwrap());
1614

1715
fn main() {
1816
// 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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

3+
use std::sync::LazyLock;
4+
35
use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec};
46

5-
use lazy_static::lazy_static;
67
use prometheus::{
78
register_int_counter, register_int_counter_vec, register_int_gauge, register_int_gauge_vec,
89
};
910

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

2021
fn main() {
2122
A_INT_COUNTER.inc();

examples/example_push.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
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;
89
use prometheus::{Counter, Histogram};
910

10-
use lazy_static::lazy_static;
1111
use prometheus::{labels, register_counter, register_histogram};
1212

13-
lazy_static! {
14-
static ref PUSH_COUNTER: Counter = register_counter!(
13+
static PUSH_COUNTER: LazyLock<Counter> = LazyLock::new(|| {
14+
register_counter!(
1515
"example_push_total",
1616
"Total number of prometheus client pushed."
1717
)
18-
.unwrap();
19-
static ref PUSH_REQ_HISTOGRAM: Histogram = register_histogram!(
18+
.unwrap()
19+
});
20+
static PUSH_REQ_HISTOGRAM: LazyLock<Histogram> = LazyLock::new(|| {
21+
register_histogram!(
2022
"example_push_request_duration_seconds",
2123
"The push request latencies in seconds."
2224
)
23-
.unwrap();
24-
}
25+
.unwrap()
26+
});
2527

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

src/lib.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,18 @@ 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
52+
[`LazyLock`](https://doc.rust-lang.org/std/sync/struct.LazyLock.html) to quickly build up and collect
5353
some metrics.
5454
5555
```rust
5656
use prometheus::{self, IntCounter, TextEncoder, Encoder};
5757
58-
use lazy_static::lazy_static;
58+
use std::sync::LazyLock;
5959
use prometheus::register_int_counter;
6060
61-
lazy_static! {
62-
static ref HIGH_FIVE_COUNTER: IntCounter =
63-
register_int_counter!("highfives", "Number of high fives received").unwrap();
64-
}
61+
static HIGH_FIVE_COUNTER: LazyLock<IntCounter> =
62+
LazyLock::new(|| register_int_counter!("highfives", "Number of high fives received").unwrap());
63+
6564
6665
HIGH_FIVE_COUNTER.inc();
6766
assert_eq!(HIGH_FIVE_COUNTER.get(), 1);
@@ -75,14 +74,12 @@ By default, this registers with a default registry. To make a report, you can ca
7574
# use prometheus::IntCounter;
7675
use prometheus::{self, TextEncoder, Encoder};
7776
78-
use lazy_static::lazy_static;
77+
use std::sync::LazyLock;
7978
use prometheus::register_int_counter;
8079
8180
// 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-
# }
81+
# static HIGH_FIVE_COUNTER: LazyLock<IntCounter> =
82+
# LazyLock::new(|| register_int_counter!("highfives", "Number of high fives received").unwrap());
8683
# HIGH_FIVE_COUNTER.inc();
8784
8885
let mut buffer = Vec::new();

static-metric/README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ For heavier scenario that a global shared static-metric might not be effecient e
9191
```rust
9292
use prometheus::*;
9393

94-
use lazy_static:lazy_static;
94+
use std::sync::LazyLock;
9595
use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric};
9696

9797
make_auto_flush_static_metric! {
@@ -118,20 +118,18 @@ make_auto_flush_static_metric! {
118118
}
119119
}
120120

121-
lazy_static! {
122-
pub static ref HTTP_COUNTER_VEC: IntCounterVec =
123-
register_int_counter_vec ! (
124-
"http_requests_total",
125-
"Number of HTTP requests.",
126-
& ["product", "method", "version"] // it doesn't matter for the label order
127-
).unwrap();
128-
}
129-
130-
lazy_static! {
131-
// You can also use default flush duration which is 1 second.
132-
// pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs);
133-
pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs, std::time::Duration::from_secs(1));
134-
}
121+
pub static HTTP_COUNTER_VEC: LazyLock<CounterVec> = LazyLock::new(|| {
122+
register_counter_vec!(
123+
"http_requests_total",
124+
"Number of HTTP requests.",
125+
& ["product", "method", "version"] // it doesn't matter for the label order
126+
)
127+
.unwrap()
128+
});
129+
130+
// You can also use default flush duration which is 1 second.
131+
// pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> = LazyLock;;new(|| auto_flush_from!(HTTP_COUNTER_VEC, Lhrs));
132+
pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> = LazyLock::new(|| auto_flush_from!(HTTP_COUNTER_VEC, Lhrs, std::time::Duration::from_secs(1)));
135133

136134
fn main() {
137135
TLS_HTTP_COUNTER.foo.post.http1.inc();

static-metric/examples/advanced.rs

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

3+
use std::sync::LazyLock;
4+
35
use prometheus::IntCounterVec;
46

5-
use lazy_static::lazy_static;
67
use prometheus::register_int_counter_vec;
78
use prometheus_static_metric::make_static_metric;
89

@@ -25,17 +26,17 @@ make_static_metric! {
2526
}
2627
}
2728

28-
lazy_static! {
29-
pub static ref HTTP_COUNTER_VEC: IntCounterVec =
30-
register_int_counter_vec!(
31-
"http_requests_total",
32-
"Number of HTTP requests.",
33-
&["product", "method", "version"] // it doesn't matter for the label order
34-
).unwrap();
29+
pub static HTTP_COUNTER_VEC: LazyLock<IntCounterVec> = LazyLock::new(|| {
30+
register_int_counter_vec!(
31+
"http_requests_total",
32+
"Number of HTTP requests.",
33+
&["product", "method", "version"] // it doesn't matter for the label order
34+
)
35+
.unwrap()
36+
});
3537

36-
pub static ref HTTP_COUNTER: HttpRequestStatistics = HttpRequestStatistics
37-
::from(&HTTP_COUNTER_VEC);
38-
}
38+
pub static HTTP_COUNTER: LazyLock<HttpRequestStatistics> =
39+
LazyLock::new(|| HttpRequestStatistics::from(&HTTP_COUNTER_VEC));
3940

4041
/// This example demonstrates the usage of:
4142
/// 1. using alternative metric types (i.e. IntCounter)

static-metric/examples/local.rs

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

3-
use std::cell::Cell;
3+
use std::{cell::Cell, sync::LazyLock};
44

55
use prometheus::*;
66

7-
use lazy_static::lazy_static;
87
use prometheus_static_metric::make_static_metric;
98

109
make_static_metric! {
@@ -28,14 +27,14 @@ make_static_metric! {
2827
}
2928
}
3029

31-
lazy_static! {
32-
pub static ref HTTP_COUNTER_VEC: IntCounterVec =
33-
register_int_counter_vec!(
34-
"http_requests_total",
35-
"Number of HTTP requests.",
36-
&["product", "method", "version"] // it doesn't matter for the label order
37-
).unwrap();
38-
}
30+
pub static HTTP_COUNTER_VEC: LazyLock<IntCounterVec> = LazyLock::new(|| {
31+
register_int_counter_vec!(
32+
"http_requests_total",
33+
"Number of HTTP requests.",
34+
&["product", "method", "version"] // it doesn't matter for the label order
35+
)
36+
.unwrap()
37+
});
3938

4039
thread_local! {
4140
static THREAD_LAST_TICK_TIME: Cell<u64> = Cell::new(timer::now_millis());

0 commit comments

Comments
 (0)