Skip to content

Commit 959d382

Browse files
committed
Remove lazy_static dependency from static-metric
1 parent bc1e913 commit 959d382

File tree

9 files changed

+94
-112
lines changed

9 files changed

+94
-112
lines changed

static-metric/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ proc-macro = true
1616
syn = { version = "2.0", features = ["full", "extra-traits"] }
1717
proc-macro2 = "1.0"
1818
quote = "1.0"
19-
lazy_static = "1.4"
2019

2120
[dev-dependencies]
2221
criterion = "0.5"

static-metric/README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ fn main() {
8989
For heavier scenario that a global shared static-metric might not be effecient enough, you can use `make_auto_flush_static_metric!` macro, which will store data in local thread storage, with a custom rate to flush to global `MetricVec`.
9090

9191
```rust
92-
use prometheus::*;
92+
use std::sync::LazyLock;
9393

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

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

static-metric/examples/advanced.rs

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

3-
use prometheus::IntCounterVec;
3+
use std::sync::LazyLock;
44

5-
use lazy_static::lazy_static;
65
use prometheus::register_int_counter_vec;
6+
use prometheus::IntCounterVec;
77
use prometheus_static_metric::make_static_metric;
88

99
make_static_metric! {
@@ -25,17 +25,17 @@ make_static_metric! {
2525
}
2626
}
2727

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

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

4040
/// This example demonstrates the usage of:
4141
/// 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

33
use std::cell::Cell;
4+
use std::sync::LazyLock;
45

56
use prometheus::*;
6-
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());

static-metric/examples/make_auto_flush_static_counter.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
Use metric enums to reuse possible values of a label.
66
77
*/
8-
use prometheus::*;
8+
use std::sync::LazyLock;
99

10-
use lazy_static::lazy_static;
10+
use prometheus::*;
1111
use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric};
1212

1313
make_auto_flush_static_metric! {
@@ -34,31 +34,28 @@ make_auto_flush_static_metric! {
3434
}
3535
}
3636

37-
lazy_static! {
38-
pub static ref HTTP_COUNTER_VEC: IntCounterVec =
39-
register_int_counter_vec ! (
40-
"http_requests_total",
41-
"Number of HTTP requests.",
42-
& ["product", "method", "version"] // it doesn't matter for the label order
43-
).unwrap();
44-
}
37+
pub static HTTP_COUNTER_VEC: LazyLock<IntCounterVec> = LazyLock::new(|| {
38+
register_int_counter_vec!(
39+
"http_requests_total",
40+
"Number of HTTP requests.",
41+
&["product", "method", "version"] // it doesn't matter for the label order
42+
)
43+
.unwrap()
44+
});
4545

4646
// Macro expanded code of auto_flush_from!
47-
// lazy_static! {
48-
// pub static ref TLS_HTTP_COUNTER: Lhrs = {
47+
// pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> = LazyLock::new(|| {
4948
// thread_local! {
5049
// pub static TLS_HTTP_COUNTER_INNER: LhrsInner = LhrsInner::from(& HTTP_COUNTER_VEC);
5150
// }
5251
// Lhrs::from(&TLS_HTTP_COUNTER_INNER)
53-
// };
54-
// }
52+
// });
5553
//
5654

57-
lazy_static! {
58-
// You can also use default flush duration which is 1 second.
59-
// pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs);
60-
pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs, std::time::Duration::from_secs(1));
61-
}
55+
// You can also use default flush duration which is 1 second.
56+
// pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs);
57+
pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> =
58+
LazyLock::new(|| auto_flush_from!(HTTP_COUNTER_VEC, Lhrs, std::time::Duration::from_secs(1)));
6259

6360
fn main() {
6461
TLS_HTTP_COUNTER.foo.post.http1.inc();
@@ -98,10 +95,10 @@ use prometheus::local::*;
9895
use prometheus::*;
9996
use std::collections::HashMap;
10097
use std::mem;
98+
use std::sync::LazyLock;
10199
use std::mem::MaybeUninit;
102100
use std::thread::LocalKey;
103101
104-
use lazy_static::lazy_static;
105102
106103
#[allow(dead_code)]
107104
#[allow(non_camel_case_types)]
@@ -367,22 +364,19 @@ impl Lhrs {
367364
}
368365
}
369366
370-
lazy_static! {
371-
pub static ref HTTP_COUNTER_VEC: IntCounterVec =
372-
register_int_counter_vec ! (
367+
pub static HTTP_COUNTER_VEC: LazyLock<IntCounterVec> =
368+
LazyLock::new(|| register_int_counter_vec ! (
373369
"http_requests_total",
374370
"Number of HTTP requests.",
375371
& ["product", "method", "version"] // it doesn't matter for the label order
376-
).unwrap();
372+
).unwrap());
377373
}
378374
379375
thread_local! {
380376
pub static TLS_HTTP_COUNTER_INNER: LhrsInner = LhrsInner::from(& HTTP_COUNTER_VEC);
381377
}
382378
383-
lazy_static! {
384-
pub static ref TLS_HTTP_COUNTER: Lhrs = Lhrs::from(&TLS_HTTP_COUNTER_INNER);
385-
}
379+
pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> = LazyLock::new(|| Lhrs::from(&TLS_HTTP_COUNTER_INNER));
386380
387381
fn main() {
388382
TLS_HTTP_COUNTER.foo.post.http1.inc();

static-metric/examples/make_auto_flush_static_metric_histogram.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
Use metric enums to reuse possible values of a label.
66
77
*/
8+
use std::sync::LazyLock;
89

910
use prometheus::*;
10-
11-
use lazy_static::lazy_static;
1211
use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric};
1312

1413
make_auto_flush_static_metric! {
@@ -35,19 +34,17 @@ make_auto_flush_static_metric! {
3534
}
3635
}
3736

38-
lazy_static! {
39-
pub static ref HTTP_HISTO_VEC: HistogramVec =
40-
register_histogram_vec ! (
41-
"http_requests_total",
42-
"Number of HTTP requests.",
43-
& ["product", "method", "version"] // it doesn't matter for the label order
44-
).unwrap();
45-
}
37+
pub static HTTP_HISTO_VEC: LazyLock<HistogramVec> = LazyLock::new(|| {
38+
register_histogram_vec!(
39+
"http_requests_total",
40+
"Number of HTTP requests.",
41+
&["product", "method", "version"] // it doesn't matter for the label order
42+
)
43+
.unwrap()
44+
});
4645

47-
lazy_static! {
48-
pub static ref TLS_HTTP_COUNTER: Lhrs =
49-
auto_flush_from!(HTTP_HISTO_VEC, Lhrs, std::time::Duration::from_secs(1));
50-
}
46+
pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> =
47+
LazyLock::new(|| auto_flush_from!(HTTP_HISTO_VEC, Lhrs, std::time::Duration::from_secs(1)));
5148

5249
fn main() {
5350
TLS_HTTP_COUNTER.foo.post.http1.observe(1.0);
@@ -96,8 +93,7 @@ use std::collections::HashMap;
9693
use std::mem;
9794
use std::mem::MaybeUninit;
9895
use std::thread::LocalKey;
99-
100-
use lazy_static::lazy_static;
96+
use std::sync::LazyLock;
10197
10298
#[allow(dead_code)]
10399
#[allow(non_camel_case_types)]
@@ -362,22 +358,19 @@ impl Lhrs {
362358
}
363359
}
364360
365-
lazy_static! {
366-
pub static ref HTTP_HISTO_VEC: HistogramVec =
367-
register_histogram_vec ! (
361+
pub static HTTP_HISTO_VEC: LazyLock<HistogramVec> =
362+
LazyLock::new(|| register_histogram_vec ! (
368363
"http_requests_total",
369364
"Number of HTTP requests.",
370365
& ["product", "method", "version"] // it doesn't matter for the label order
371-
).unwrap();
366+
).unwrap());
372367
}
373368
374369
thread_local! {
375370
pub static TLS_HTTP_COUNTER_INNER: LhrsInner = LhrsInner::from(& HTTP_HISTO_VEC);
376371
}
377372
378-
lazy_static! {
379-
pub static ref TLS_HTTP_COUNTER: Lhrs = Lhrs::from(&TLS_HTTP_COUNTER_INNER);
380-
}
373+
pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> = LazyLock::new(|| Lhrs::from(&TLS_HTTP_COUNTER_INNER));
381374
382375
fn main() {
383376
TLS_HTTP_COUNTER.foo.post.http1.observe(1.0);

static-metric/examples/register_integration.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ by using the `register_static_xxx!` macro provided by this crate.
77
88
*/
99

10-
use prometheus::exponential_buckets;
10+
use std::sync::LazyLock;
1111

12-
use lazy_static::lazy_static;
12+
use prometheus::exponential_buckets;
1313
use prometheus::{register_counter_vec, register_histogram_vec};
1414
use prometheus_static_metric::{
1515
make_static_metric, register_static_counter_vec, register_static_histogram_vec,
@@ -38,23 +38,25 @@ make_static_metric! {
3838
}
3939
}
4040

41-
lazy_static! {
42-
pub static ref HTTP_COUNTER: HttpRequestStatistics = register_static_counter_vec!(
41+
pub static HTTP_COUNTER: LazyLock<HttpRequestStatistics> = LazyLock::new(|| {
42+
register_static_counter_vec!(
4343
HttpRequestStatistics,
4444
"http_requests_total",
4545
"Number of HTTP requests.",
4646
&["method", "product"]
4747
)
48-
.unwrap();
49-
pub static ref HTTP_DURATION: HttpRequestDuration = register_static_histogram_vec!(
48+
.unwrap()
49+
});
50+
pub static HTTP_DURATION: LazyLock<HttpRequestDuration> = LazyLock::new(|| {
51+
register_static_histogram_vec!(
5052
HttpRequestDuration,
5153
"http_request_duration",
5254
"Duration of each HTTP request.",
5355
&["method"],
5456
exponential_buckets(0.0005, 2.0, 20).unwrap()
5557
)
56-
.unwrap();
57-
}
58+
.unwrap()
59+
});
5860

5961
fn main() {
6062
HTTP_COUNTER.post.foo.inc();

static-metric/examples/with_lazy_static.rs

Lines changed: 8 additions & 9 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 prometheus::CounterVec;
4-
5-
use lazy_static::lazy_static;
63
use prometheus::register_counter_vec;
4+
use prometheus::CounterVec;
75
use prometheus_static_metric::make_static_metric;
6+
use std::sync::LazyLock;
87

98
make_static_metric! {
109
pub struct HttpRequestStatistics: Counter {
@@ -21,16 +20,16 @@ make_static_metric! {
2120
}
2221
}
2322

24-
lazy_static! {
25-
pub static ref HTTP_COUNTER_VEC: CounterVec = register_counter_vec!(
23+
pub static HTTP_COUNTER_VEC: LazyLock<CounterVec> = LazyLock::new(|| {
24+
register_counter_vec!(
2625
"http_requests_total",
2726
"Number of HTTP requests.",
2827
&["method", "product"]
2928
)
30-
.unwrap();
31-
pub static ref HTTP_COUNTER: HttpRequestStatistics =
32-
HttpRequestStatistics::from(&HTTP_COUNTER_VEC);
33-
}
29+
.unwrap()
30+
});
31+
pub static HTTP_COUNTER: LazyLock<HttpRequestStatistics> =
32+
LazyLock::new(|| HttpRequestStatistics::from(&HTTP_COUNTER_VEC));
3433

3534
fn main() {
3635
HTTP_COUNTER.post.foo.inc();

0 commit comments

Comments
 (0)