Skip to content

Commit c3687d4

Browse files
utpillacijothomas
andauthored
[Metrics-API] Mark no-op structs as pub(crate) (#2203)
Co-authored-by: Cijo Thomas <[email protected]>
1 parent f1a6e16 commit c3687d4

File tree

7 files changed

+93
-11
lines changed

7 files changed

+93
-11
lines changed

opentelemetry-sdk/src/metrics/meter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::{borrow::Cow, sync::Arc};
44
use opentelemetry::{
55
global,
66
metrics::{
7-
noop::{NoopAsyncInstrument, NoopSyncInstrument},
87
AsyncInstrumentBuilder, Counter, Gauge, Histogram, HistogramBuilder, InstrumentBuilder,
98
InstrumentProvider, MetricsError, ObservableCounter, ObservableGauge,
109
ObservableUpDownCounter, Result, UpDownCounter,
@@ -18,6 +17,8 @@ use crate::metrics::{
1817
pipeline::{Pipelines, Resolver},
1918
};
2019

20+
use super::noop::{NoopAsyncInstrument, NoopSyncInstrument};
21+
2122
// maximum length of instrument name
2223
const INSTRUMENT_NAME_MAX_LENGTH: usize = 255;
2324
// maximum length of instrument unit name

opentelemetry-sdk/src/metrics/meter_provider.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ use std::{
99

1010
use opentelemetry::{
1111
global,
12-
metrics::{noop::NoopMeter, Meter, MeterProvider, MetricsError, Result},
12+
metrics::{Meter, MeterProvider, MetricsError, Result},
1313
KeyValue,
1414
};
1515

1616
use crate::{instrumentation::Scope, Resource};
1717

18-
use super::{meter::SdkMeter, pipeline::Pipelines, reader::MetricReader, view::View};
18+
use super::{
19+
meter::SdkMeter, noop::NoopMeter, pipeline::Pipelines, reader::MetricReader, view::View,
20+
};
1921

2022
/// Handles the creation and coordination of [Meter]s.
2123
///

opentelemetry-sdk/src/metrics/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub(crate) mod internal;
4747
pub(crate) mod manual_reader;
4848
pub(crate) mod meter;
4949
mod meter_provider;
50+
pub(crate) mod noop;
5051
pub(crate) mod periodic_reader;
5152
pub(crate) mod pipeline;
5253
pub mod reader;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use opentelemetry::{
2+
metrics::{
3+
AsyncInstrument, InstrumentProvider, SyncCounter, SyncGauge, SyncHistogram,
4+
SyncUpDownCounter,
5+
},
6+
KeyValue,
7+
};
8+
9+
/// A no-op instance of a `Meter`
10+
#[derive(Debug, Default)]
11+
pub(crate) struct NoopMeter {
12+
_private: (),
13+
}
14+
15+
impl NoopMeter {
16+
/// Create a new no-op meter core.
17+
pub(crate) fn new() -> Self {
18+
NoopMeter { _private: () }
19+
}
20+
}
21+
22+
impl InstrumentProvider for NoopMeter {}
23+
24+
/// A no-op sync instrument
25+
#[derive(Debug, Default)]
26+
pub(crate) struct NoopSyncInstrument {
27+
_private: (),
28+
}
29+
30+
impl NoopSyncInstrument {
31+
/// Create a new no-op sync instrument
32+
pub(crate) fn new() -> Self {
33+
NoopSyncInstrument { _private: () }
34+
}
35+
}
36+
37+
impl<T> SyncCounter<T> for NoopSyncInstrument {
38+
fn add(&self, _value: T, _attributes: &[KeyValue]) {
39+
// Ignored
40+
}
41+
}
42+
43+
impl<T> SyncUpDownCounter<T> for NoopSyncInstrument {
44+
fn add(&self, _value: T, _attributes: &[KeyValue]) {
45+
// Ignored
46+
}
47+
}
48+
49+
impl<T> SyncHistogram<T> for NoopSyncInstrument {
50+
fn record(&self, _value: T, _attributes: &[KeyValue]) {
51+
// Ignored
52+
}
53+
}
54+
55+
impl<T> SyncGauge<T> for NoopSyncInstrument {
56+
fn record(&self, _value: T, _attributes: &[KeyValue]) {
57+
// Ignored
58+
}
59+
}
60+
61+
/// A no-op async instrument.
62+
#[derive(Debug, Default)]
63+
pub(crate) struct NoopAsyncInstrument {
64+
_private: (),
65+
}
66+
67+
impl NoopAsyncInstrument {
68+
/// Create a new no-op async instrument
69+
pub(crate) fn new() -> Self {
70+
NoopAsyncInstrument { _private: () }
71+
}
72+
}
73+
74+
impl<T> AsyncInstrument<T> for NoopAsyncInstrument {
75+
fn observe(&self, _value: T, _attributes: &[KeyValue]) {
76+
// Ignored
77+
}
78+
}

opentelemetry/src/global/metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type GlobalMeterProvider = Arc<dyn MeterProvider + Send + Sync>;
77

88
/// The global `MeterProvider` singleton.
99
static GLOBAL_METER_PROVIDER: Lazy<RwLock<GlobalMeterProvider>> =
10-
Lazy::new(|| RwLock::new(Arc::new(metrics::noop::NoopMeterProvider::new())));
10+
Lazy::new(|| RwLock::new(Arc::new(crate::metrics::noop::NoopMeterProvider::new())));
1111

1212
/// Sets the given [`MeterProvider`] instance as the current global meter
1313
/// provider.

opentelemetry/src/metrics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use thiserror::Error;
99

1010
mod instruments;
1111
mod meter;
12-
pub mod noop;
12+
pub(crate) mod noop;
1313

1414
use crate::{Array, ExportError, KeyValue, Value};
1515
pub use instruments::{

opentelemetry/src/metrics/noop.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ impl MeterProvider for NoopMeterProvider {
3939

4040
/// A no-op instance of a `Meter`
4141
#[derive(Debug, Default)]
42-
pub struct NoopMeter {
42+
pub(crate) struct NoopMeter {
4343
_private: (),
4444
}
4545

4646
impl NoopMeter {
4747
/// Create a new no-op meter core.
48-
pub fn new() -> Self {
48+
pub(crate) fn new() -> Self {
4949
NoopMeter { _private: () }
5050
}
5151
}
@@ -54,13 +54,13 @@ impl InstrumentProvider for NoopMeter {}
5454

5555
/// A no-op sync instrument
5656
#[derive(Debug, Default)]
57-
pub struct NoopSyncInstrument {
57+
pub(crate) struct NoopSyncInstrument {
5858
_private: (),
5959
}
6060

6161
impl NoopSyncInstrument {
6262
/// Create a new no-op sync instrument
63-
pub fn new() -> Self {
63+
pub(crate) fn new() -> Self {
6464
NoopSyncInstrument { _private: () }
6565
}
6666
}
@@ -91,13 +91,13 @@ impl<T> SyncGauge<T> for NoopSyncInstrument {
9191

9292
/// A no-op async instrument.
9393
#[derive(Debug, Default)]
94-
pub struct NoopAsyncInstrument {
94+
pub(crate) struct NoopAsyncInstrument {
9595
_private: (),
9696
}
9797

9898
impl NoopAsyncInstrument {
9999
/// Create a new no-op async instrument
100-
pub fn new() -> Self {
100+
pub(crate) fn new() -> Self {
101101
NoopAsyncInstrument { _private: () }
102102
}
103103
}

0 commit comments

Comments
 (0)