Skip to content

Commit 03fe982

Browse files
authored
Ensure metrics noop types go through constructors (#345)
1 parent 45b0963 commit 03fe982

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

opentelemetry/src/api/metrics/noop.rs

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,51 @@ lazy_static::lazy_static! {
2222
}
2323

2424
/// A no-op instance of a `MetricProvider`
25-
#[derive(Debug)]
26-
pub struct NoopMeterProvider;
25+
#[derive(Debug, Default)]
26+
pub struct NoopMeterProvider {
27+
_private: (),
28+
}
29+
30+
impl NoopMeterProvider {
31+
/// Create a new no-op meter provider.
32+
pub fn new() -> Self {
33+
NoopMeterProvider { _private: () }
34+
}
35+
}
36+
2737
impl MeterProvider for NoopMeterProvider {
2838
fn meter(&self, name: &str) -> Meter {
29-
Meter::new(name, Arc::new(NoopMeterCore))
39+
Meter::new(name, Arc::new(NoopMeterCore::new()))
3040
}
3141
}
3242

3343
/// A no-op instance of a `Meter`
34-
#[derive(Debug)]
35-
pub struct NoopMeterCore;
44+
#[derive(Debug, Default)]
45+
pub struct NoopMeterCore {
46+
_private: (),
47+
}
48+
49+
impl NoopMeterCore {
50+
/// Create a new no-op meter core.
51+
pub fn new() -> Self {
52+
NoopMeterCore { _private: () }
53+
}
54+
}
3655

3756
impl MeterCore for NoopMeterCore {
3857
fn new_sync_instrument(
3958
&self,
4059
_descriptor: Descriptor,
4160
) -> Result<Arc<dyn SyncInstrumentCore + Send + Sync>> {
42-
Ok(Arc::new(NoopSyncInstrument))
61+
Ok(Arc::new(NoopSyncInstrument::new()))
4362
}
4463

4564
fn new_async_instrument(
4665
&self,
4766
_descriptor: Descriptor,
4867
_runner: AsyncRunner,
4968
) -> Result<Arc<dyn AsyncInstrumentCore + Send + Sync>> {
50-
Ok(Arc::new(NoopAsyncInstrument))
69+
Ok(Arc::new(NoopAsyncInstrument::new()))
5170
}
5271

5372
fn record_batch_with_context(
@@ -61,8 +80,17 @@ impl MeterCore for NoopMeterCore {
6180
}
6281

6382
/// A no-op sync instrument
64-
#[derive(Debug)]
65-
pub struct NoopSyncInstrument;
83+
#[derive(Debug, Default)]
84+
pub struct NoopSyncInstrument {
85+
_private: (),
86+
}
87+
88+
impl NoopSyncInstrument {
89+
/// Create a new no-op sync instrument
90+
pub fn new() -> Self {
91+
NoopSyncInstrument { _private: () }
92+
}
93+
}
6694

6795
impl InstrumentCore for NoopSyncInstrument {
6896
fn descriptor(&self) -> &Descriptor {
@@ -72,7 +100,7 @@ impl InstrumentCore for NoopSyncInstrument {
72100

73101
impl SyncInstrumentCore for NoopSyncInstrument {
74102
fn bind(&self, _labels: &'_ [KeyValue]) -> Arc<dyn SyncBoundInstrumentCore + Send + Sync> {
75-
Arc::new(NoopBoundSyncInstrument)
103+
Arc::new(NoopBoundSyncInstrument::new())
76104
}
77105
fn record_one(&self, _number: Number, _labels: &'_ [KeyValue]) {
78106
// Ignored
@@ -83,8 +111,17 @@ impl SyncInstrumentCore for NoopSyncInstrument {
83111
}
84112

85113
/// A no-op bound sync instrument
86-
#[derive(Debug)]
87-
pub struct NoopBoundSyncInstrument;
114+
#[derive(Debug, Default)]
115+
pub struct NoopBoundSyncInstrument {
116+
_private: (),
117+
}
118+
119+
impl NoopBoundSyncInstrument {
120+
/// Create a new no-op bound sync instrument
121+
pub fn new() -> Self {
122+
NoopBoundSyncInstrument { _private: () }
123+
}
124+
}
88125

89126
impl SyncBoundInstrumentCore for NoopBoundSyncInstrument {
90127
fn record_one(&self, _number: Number) {
@@ -93,8 +130,17 @@ impl SyncBoundInstrumentCore for NoopBoundSyncInstrument {
93130
}
94131

95132
/// A no-op async instrument.
96-
#[derive(Debug)]
97-
pub struct NoopAsyncInstrument;
133+
#[derive(Debug, Default)]
134+
pub struct NoopAsyncInstrument {
135+
_private: (),
136+
}
137+
138+
impl NoopAsyncInstrument {
139+
/// Create a new no-op async instrument
140+
pub fn new() -> Self {
141+
NoopAsyncInstrument { _private: () }
142+
}
143+
}
98144

99145
impl InstrumentCore for NoopAsyncInstrument {
100146
fn descriptor(&self) -> &Descriptor {

opentelemetry/src/global/metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
33

44
lazy_static::lazy_static! {
55
/// The global `Meter` provider singleton.
6-
static ref GLOBAL_METER_PROVIDER: RwLock<GlobalMeterProvider> = RwLock::new(GlobalMeterProvider::new(metrics::noop::NoopMeterProvider));
6+
static ref GLOBAL_METER_PROVIDER: RwLock<GlobalMeterProvider> = RwLock::new(GlobalMeterProvider::new(metrics::noop::NoopMeterProvider::new()));
77
}
88

99
/// Represents the globally configured [`MeterProvider`] instance for this

opentelemetry/src/global/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
//! use opentelemetry::{global, KeyValue};
8585
//!
8686
//! fn init_meter() {
87-
//! let provider = NoopMeterProvider;
87+
//! let provider = NoopMeterProvider::new();
8888
//!
8989
//! // Configure the global `MeterProvider` singleton when your app starts
9090
//! // (there is a no-op default if this is not set by your application)

0 commit comments

Comments
 (0)