Skip to content

Commit 8d84a76

Browse files
utpillacijothomas
andauthored
[Metrics-API] Update asynchronous instruments to not allow calling observe outside of a callback (#2210)
Co-authored-by: Cijo Thomas <[email protected]>
1 parent 16c0e10 commit 8d84a76

File tree

9 files changed

+51
-115
lines changed

9 files changed

+51
-115
lines changed

opentelemetry-sdk/src/metrics/meter.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::metrics::{
1717
pipeline::{Pipelines, Resolver},
1818
};
1919

20-
use super::noop::{NoopAsyncInstrument, NoopSyncInstrument};
20+
use super::noop::NoopSyncInstrument;
2121

2222
// maximum length of instrument name
2323
const INSTRUMENT_NAME_MAX_LENGTH: usize = 255;
@@ -108,7 +108,7 @@ impl SdkMeter {
108108
let validation_result = validate_instrument_config(builder.name.as_ref(), &builder.unit);
109109
if let Err(err) = validation_result {
110110
global::handle_error(err);
111-
return Ok(ObservableCounter::new(Arc::new(NoopAsyncInstrument::new())));
111+
return Ok(ObservableCounter::new());
112112
}
113113

114114
let ms = resolver.measures(
@@ -120,7 +120,7 @@ impl SdkMeter {
120120
)?;
121121

122122
if ms.is_empty() {
123-
return Ok(ObservableCounter::new(Arc::new(NoopAsyncInstrument::new())));
123+
return Ok(ObservableCounter::new());
124124
}
125125

126126
let observable = Arc::new(Observable::new(ms));
@@ -131,7 +131,7 @@ impl SdkMeter {
131131
.register_callback(move || callback(cb_inst.as_ref()));
132132
}
133133

134-
Ok(ObservableCounter::new(observable))
134+
Ok(ObservableCounter::new())
135135
}
136136

137137
fn create_observable_updown_counter<T>(
@@ -145,9 +145,7 @@ impl SdkMeter {
145145
let validation_result = validate_instrument_config(builder.name.as_ref(), &builder.unit);
146146
if let Err(err) = validation_result {
147147
global::handle_error(err);
148-
return Ok(ObservableUpDownCounter::new(Arc::new(
149-
NoopAsyncInstrument::new(),
150-
)));
148+
return Ok(ObservableUpDownCounter::new());
151149
}
152150

153151
let ms = resolver.measures(
@@ -159,9 +157,7 @@ impl SdkMeter {
159157
)?;
160158

161159
if ms.is_empty() {
162-
return Ok(ObservableUpDownCounter::new(Arc::new(
163-
NoopAsyncInstrument::new(),
164-
)));
160+
return Ok(ObservableUpDownCounter::new());
165161
}
166162

167163
let observable = Arc::new(Observable::new(ms));
@@ -172,7 +168,7 @@ impl SdkMeter {
172168
.register_callback(move || callback(cb_inst.as_ref()));
173169
}
174170

175-
Ok(ObservableUpDownCounter::new(observable))
171+
Ok(ObservableUpDownCounter::new())
176172
}
177173

178174
fn create_observable_gauge<T>(
@@ -186,7 +182,7 @@ impl SdkMeter {
186182
let validation_result = validate_instrument_config(builder.name.as_ref(), &builder.unit);
187183
if let Err(err) = validation_result {
188184
global::handle_error(err);
189-
return Ok(ObservableGauge::new(Arc::new(NoopAsyncInstrument::new())));
185+
return Ok(ObservableGauge::new());
190186
}
191187

192188
let ms = resolver.measures(
@@ -198,7 +194,7 @@ impl SdkMeter {
198194
)?;
199195

200196
if ms.is_empty() {
201-
return Ok(ObservableGauge::new(Arc::new(NoopAsyncInstrument::new())));
197+
return Ok(ObservableGauge::new());
202198
}
203199

204200
let observable = Arc::new(Observable::new(ms));
@@ -209,7 +205,7 @@ impl SdkMeter {
209205
.register_callback(move || callback(cb_inst.as_ref()));
210206
}
211207

212-
Ok(ObservableGauge::new(observable))
208+
Ok(ObservableGauge::new())
213209
}
214210

215211
fn create_updown_counter<T>(

opentelemetry-sdk/src/metrics/noop.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use opentelemetry::{
2-
metrics::{AsyncInstrument, InstrumentProvider, SyncInstrument},
2+
metrics::{InstrumentProvider, SyncInstrument},
33
KeyValue,
44
};
55

@@ -36,22 +36,3 @@ impl<T> SyncInstrument<T> for NoopSyncInstrument {
3636
// Ignored
3737
}
3838
}
39-
40-
/// A no-op async instrument.
41-
#[derive(Debug, Default)]
42-
pub(crate) struct NoopAsyncInstrument {
43-
_private: (),
44-
}
45-
46-
impl NoopAsyncInstrument {
47-
/// Create a new no-op async instrument
48-
pub(crate) fn new() -> Self {
49-
NoopAsyncInstrument { _private: () }
50-
}
51-
}
52-
53-
impl<T> AsyncInstrument<T> for NoopAsyncInstrument {
54-
fn observe(&self, _value: T, _attributes: &[KeyValue]) {
55-
// Ignored
56-
}
57-
}

opentelemetry/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
- Bump MSRV to 1.70 [#2179](https://github.com/open-telemetry/opentelemetry-rust/pull/2179)
66
- Add `LogRecord::set_trace_context`; an optional method conditional on the `trace` feature for setting trace context on a log record.
7-
- Removed unnecessary public methods named `as_any` from `AsyncInstrument` trait and the implementing instruments: `ObservableCounter`, `ObservableGauge`, and `ObservableUpDownCounter` [#2187](https://github.com/open-telemetry/opentelemetry-rust/issues/2187)
8-
- Introduced `SyncInstrument` trait to replace the individual synchronous instrument traits (`SyncCounter`, `SyncGauge`, `SyncHistogram`, `SyncUpDownCounter`) which are meant for SDK implementation. [#2207](https://github.com/open-telemetry/opentelemetry-rust/issues/2207)
7+
- Removed unnecessary public methods named `as_any` from `AsyncInstrument` trait and the implementing instruments: `ObservableCounter`, `ObservableGauge`, and `ObservableUpDownCounter` [#2187](https://github.com/open-telemetry/opentelemetry-rust/pull/2187)
8+
- Introduced `SyncInstrument` trait to replace the individual synchronous instrument traits (`SyncCounter`, `SyncGauge`, `SyncHistogram`, `SyncUpDownCounter`) which are meant for SDK implementation. [#2207](https://github.com/open-telemetry/opentelemetry-rust/pull/2207)
9+
- Ensured that `observe` method on asynchronous instruments can only be called inside a callback. This was done by removing the implementation of `AsyncInstrument` trait for each of the asynchronous instruments. [#2210](https://github.com/open-telemetry/opentelemetry-rust/pull/2210)
910

1011
## v0.26.0
1112
Released 2024-Sep-30

opentelemetry/src/metrics/instruments/counter.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{metrics::AsyncInstrument, KeyValue};
1+
use crate::KeyValue;
22
use core::fmt;
33
use std::sync::Arc;
44

@@ -33,12 +33,17 @@ impl<T> Counter<T> {
3333
/// An async instrument that records increasing values.
3434
#[derive(Clone)]
3535
#[non_exhaustive]
36-
pub struct ObservableCounter<T>(Arc<dyn AsyncInstrument<T>>);
36+
pub struct ObservableCounter<T> {
37+
_marker: std::marker::PhantomData<T>,
38+
}
3739

3840
impl<T> ObservableCounter<T> {
3941
/// Create a new observable counter.
40-
pub fn new(inner: Arc<dyn AsyncInstrument<T>>) -> Self {
41-
ObservableCounter(inner)
42+
#[allow(clippy::new_without_default)]
43+
pub fn new() -> Self {
44+
ObservableCounter {
45+
_marker: std::marker::PhantomData,
46+
}
4247
}
4348
}
4449

@@ -50,9 +55,3 @@ impl<T> fmt::Debug for ObservableCounter<T> {
5055
))
5156
}
5257
}
53-
54-
impl<T> AsyncInstrument<T> for ObservableCounter<T> {
55-
fn observe(&self, measurement: T, attributes: &[KeyValue]) {
56-
self.0.observe(measurement, attributes)
57-
}
58-
}

opentelemetry/src/metrics/instruments/gauge.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{metrics::AsyncInstrument, KeyValue};
1+
use crate::KeyValue;
22
use core::fmt;
33
use std::sync::Arc;
44

@@ -33,7 +33,9 @@ impl<T> Gauge<T> {
3333
/// An async instrument that records independent readings.
3434
#[derive(Clone)]
3535
#[non_exhaustive]
36-
pub struct ObservableGauge<T>(Arc<dyn AsyncInstrument<T>>);
36+
pub struct ObservableGauge<T> {
37+
_marker: std::marker::PhantomData<T>,
38+
}
3739

3840
impl<T> fmt::Debug for ObservableGauge<T>
3941
where
@@ -47,15 +49,12 @@ where
4749
}
4850
}
4951

50-
impl<M> AsyncInstrument<M> for ObservableGauge<M> {
51-
fn observe(&self, measurement: M, attributes: &[KeyValue]) {
52-
self.0.observe(measurement, attributes)
53-
}
54-
}
55-
5652
impl<T> ObservableGauge<T> {
5753
/// Create a new gauge
58-
pub fn new(inner: Arc<dyn AsyncInstrument<T>>) -> Self {
59-
ObservableGauge(inner)
54+
#[allow(clippy::new_without_default)]
55+
pub fn new() -> Self {
56+
ObservableGauge {
57+
_marker: std::marker::PhantomData,
58+
}
6059
}
6160
}

opentelemetry/src/metrics/instruments/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,7 @@ pub type Callback<T> = Box<dyn Fn(&dyn AsyncInstrument<T>) + Send + Sync>;
241241

242242
/// Configuration for building an async instrument.
243243
#[non_exhaustive] // We expect to add more configuration fields in the future
244-
pub struct AsyncInstrumentBuilder<'a, I, M>
245-
where
246-
I: AsyncInstrument<M>,
247-
{
244+
pub struct AsyncInstrumentBuilder<'a, I, M> {
248245
/// Instrument provider is used to create the instrument.
249246
pub instrument_provider: &'a dyn InstrumentProvider,
250247

@@ -263,10 +260,7 @@ where
263260
_inst: marker::PhantomData<I>,
264261
}
265262

266-
impl<'a, I, M> AsyncInstrumentBuilder<'a, I, M>
267-
where
268-
I: AsyncInstrument<M>,
269-
{
263+
impl<'a, I, M> AsyncInstrumentBuilder<'a, I, M> {
270264
/// Create a new instrument builder
271265
pub(crate) fn new(meter: &'a Meter, name: Cow<'static, str>) -> Self {
272266
AsyncInstrumentBuilder {

opentelemetry/src/metrics/instruments/up_down_counter.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::KeyValue;
22
use core::fmt;
33
use std::sync::Arc;
44

5-
use super::{AsyncInstrument, SyncInstrument};
5+
use super::SyncInstrument;
66

77
/// An instrument that records increasing or decreasing values.
88
#[derive(Clone)]
@@ -36,7 +36,9 @@ impl<T> UpDownCounter<T> {
3636
/// An async instrument that records increasing or decreasing values.
3737
#[derive(Clone)]
3838
#[non_exhaustive]
39-
pub struct ObservableUpDownCounter<T>(Arc<dyn AsyncInstrument<T>>);
39+
pub struct ObservableUpDownCounter<T> {
40+
_marker: std::marker::PhantomData<T>,
41+
}
4042

4143
impl<T> fmt::Debug for ObservableUpDownCounter<T>
4244
where
@@ -52,13 +54,10 @@ where
5254

5355
impl<T> ObservableUpDownCounter<T> {
5456
/// Create a new observable up down counter.
55-
pub fn new(inner: Arc<dyn AsyncInstrument<T>>) -> Self {
56-
ObservableUpDownCounter(inner)
57-
}
58-
}
59-
60-
impl<T> AsyncInstrument<T> for ObservableUpDownCounter<T> {
61-
fn observe(&self, measurement: T, attributes: &[KeyValue]) {
62-
self.0.observe(measurement, attributes)
57+
#[allow(clippy::new_without_default)]
58+
pub fn new() -> Self {
59+
ObservableUpDownCounter {
60+
_marker: std::marker::PhantomData,
61+
}
6362
}
6463
}

opentelemetry/src/metrics/mod.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,15 @@ pub trait InstrumentProvider {
123123
&self,
124124
_builder: AsyncInstrumentBuilder<'_, ObservableCounter<u64>, u64>,
125125
) -> Result<ObservableCounter<u64>> {
126-
Ok(ObservableCounter::new(Arc::new(
127-
noop::NoopAsyncInstrument::new(),
128-
)))
126+
Ok(ObservableCounter::new())
129127
}
130128

131129
/// creates an instrument for recording increasing values via callback.
132130
fn f64_observable_counter(
133131
&self,
134132
_builder: AsyncInstrumentBuilder<'_, ObservableCounter<f64>, f64>,
135133
) -> Result<ObservableCounter<f64>> {
136-
Ok(ObservableCounter::new(Arc::new(
137-
noop::NoopAsyncInstrument::new(),
138-
)))
134+
Ok(ObservableCounter::new())
139135
}
140136

141137
/// creates an instrument for recording changes of a value.
@@ -163,19 +159,15 @@ pub trait InstrumentProvider {
163159
&self,
164160
_builder: AsyncInstrumentBuilder<'_, ObservableUpDownCounter<i64>, i64>,
165161
) -> Result<ObservableUpDownCounter<i64>> {
166-
Ok(ObservableUpDownCounter::new(Arc::new(
167-
noop::NoopAsyncInstrument::new(),
168-
)))
162+
Ok(ObservableUpDownCounter::new())
169163
}
170164

171165
/// creates an instrument for recording changes of a value via callback.
172166
fn f64_observable_up_down_counter(
173167
&self,
174168
_builder: AsyncInstrumentBuilder<'_, ObservableUpDownCounter<f64>, f64>,
175169
) -> Result<ObservableUpDownCounter<f64>> {
176-
Ok(ObservableUpDownCounter::new(Arc::new(
177-
noop::NoopAsyncInstrument::new(),
178-
)))
170+
Ok(ObservableUpDownCounter::new())
179171
}
180172

181173
/// creates an instrument for recording independent values.
@@ -198,29 +190,23 @@ pub trait InstrumentProvider {
198190
&self,
199191
_builder: AsyncInstrumentBuilder<'_, ObservableGauge<u64>, u64>,
200192
) -> Result<ObservableGauge<u64>> {
201-
Ok(ObservableGauge::new(Arc::new(
202-
noop::NoopAsyncInstrument::new(),
203-
)))
193+
Ok(ObservableGauge::new())
204194
}
205195

206196
/// creates an instrument for recording the current value via callback.
207197
fn i64_observable_gauge(
208198
&self,
209199
_builder: AsyncInstrumentBuilder<'_, ObservableGauge<i64>, i64>,
210200
) -> Result<ObservableGauge<i64>> {
211-
Ok(ObservableGauge::new(Arc::new(
212-
noop::NoopAsyncInstrument::new(),
213-
)))
201+
Ok(ObservableGauge::new())
214202
}
215203

216204
/// creates an instrument for recording the current value via callback.
217205
fn f64_observable_gauge(
218206
&self,
219207
_builder: AsyncInstrumentBuilder<'_, ObservableGauge<f64>, f64>,
220208
) -> Result<ObservableGauge<f64>> {
221-
Ok(ObservableGauge::new(Arc::new(
222-
noop::NoopAsyncInstrument::new(),
223-
)))
209+
Ok(ObservableGauge::new())
224210
}
225211

226212
/// creates an instrument for recording a distribution of values.

opentelemetry/src/metrics/noop.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! has been set. It is expected to have minimal resource utilization and
55
//! runtime impact.
66
use crate::{
7-
metrics::{AsyncInstrument, InstrumentProvider, Meter, MeterProvider},
7+
metrics::{InstrumentProvider, Meter, MeterProvider},
88
KeyValue,
99
};
1010
use std::sync::Arc;
@@ -69,22 +69,3 @@ impl<T> SyncInstrument<T> for NoopSyncInstrument {
6969
// Ignored
7070
}
7171
}
72-
73-
/// A no-op async instrument.
74-
#[derive(Debug, Default)]
75-
pub(crate) struct NoopAsyncInstrument {
76-
_private: (),
77-
}
78-
79-
impl NoopAsyncInstrument {
80-
/// Create a new no-op async instrument
81-
pub(crate) fn new() -> Self {
82-
NoopAsyncInstrument { _private: () }
83-
}
84-
}
85-
86-
impl<T> AsyncInstrument<T> for NoopAsyncInstrument {
87-
fn observe(&self, _value: T, _attributes: &[KeyValue]) {
88-
// Ignored
89-
}
90-
}

0 commit comments

Comments
 (0)