diff --git a/opentelemetry-sdk/src/metrics/meter.rs b/opentelemetry-sdk/src/metrics/meter.rs index 26169f9616..0234df35b7 100644 --- a/opentelemetry-sdk/src/metrics/meter.rs +++ b/opentelemetry-sdk/src/metrics/meter.rs @@ -17,7 +17,7 @@ use crate::metrics::{ pipeline::{Pipelines, Resolver}, }; -use super::noop::{NoopAsyncInstrument, NoopSyncInstrument}; +use super::noop::NoopSyncInstrument; // maximum length of instrument name const INSTRUMENT_NAME_MAX_LENGTH: usize = 255; @@ -108,7 +108,7 @@ impl SdkMeter { let validation_result = validate_instrument_config(builder.name.as_ref(), &builder.unit); if let Err(err) = validation_result { global::handle_error(err); - return Ok(ObservableCounter::new(Arc::new(NoopAsyncInstrument::new()))); + return Ok(ObservableCounter::new()); } let ms = resolver.measures( @@ -120,7 +120,7 @@ impl SdkMeter { )?; if ms.is_empty() { - return Ok(ObservableCounter::new(Arc::new(NoopAsyncInstrument::new()))); + return Ok(ObservableCounter::new()); } let observable = Arc::new(Observable::new(ms)); @@ -131,7 +131,7 @@ impl SdkMeter { .register_callback(move || callback(cb_inst.as_ref())); } - Ok(ObservableCounter::new(observable)) + Ok(ObservableCounter::new()) } fn create_observable_updown_counter( @@ -145,9 +145,7 @@ impl SdkMeter { let validation_result = validate_instrument_config(builder.name.as_ref(), &builder.unit); if let Err(err) = validation_result { global::handle_error(err); - return Ok(ObservableUpDownCounter::new(Arc::new( - NoopAsyncInstrument::new(), - ))); + return Ok(ObservableUpDownCounter::new()); } let ms = resolver.measures( @@ -159,9 +157,7 @@ impl SdkMeter { )?; if ms.is_empty() { - return Ok(ObservableUpDownCounter::new(Arc::new( - NoopAsyncInstrument::new(), - ))); + return Ok(ObservableUpDownCounter::new()); } let observable = Arc::new(Observable::new(ms)); @@ -172,7 +168,7 @@ impl SdkMeter { .register_callback(move || callback(cb_inst.as_ref())); } - Ok(ObservableUpDownCounter::new(observable)) + Ok(ObservableUpDownCounter::new()) } fn create_observable_gauge( @@ -186,7 +182,7 @@ impl SdkMeter { let validation_result = validate_instrument_config(builder.name.as_ref(), &builder.unit); if let Err(err) = validation_result { global::handle_error(err); - return Ok(ObservableGauge::new(Arc::new(NoopAsyncInstrument::new()))); + return Ok(ObservableGauge::new()); } let ms = resolver.measures( @@ -198,7 +194,7 @@ impl SdkMeter { )?; if ms.is_empty() { - return Ok(ObservableGauge::new(Arc::new(NoopAsyncInstrument::new()))); + return Ok(ObservableGauge::new()); } let observable = Arc::new(Observable::new(ms)); @@ -209,7 +205,7 @@ impl SdkMeter { .register_callback(move || callback(cb_inst.as_ref())); } - Ok(ObservableGauge::new(observable)) + Ok(ObservableGauge::new()) } fn create_updown_counter( diff --git a/opentelemetry-sdk/src/metrics/noop.rs b/opentelemetry-sdk/src/metrics/noop.rs index 447c89432c..1a490698ad 100644 --- a/opentelemetry-sdk/src/metrics/noop.rs +++ b/opentelemetry-sdk/src/metrics/noop.rs @@ -1,5 +1,5 @@ use opentelemetry::{ - metrics::{AsyncInstrument, InstrumentProvider, SyncInstrument}, + metrics::{InstrumentProvider, SyncInstrument}, KeyValue, }; @@ -36,22 +36,3 @@ impl SyncInstrument for NoopSyncInstrument { // Ignored } } - -/// A no-op async instrument. -#[derive(Debug, Default)] -pub(crate) struct NoopAsyncInstrument { - _private: (), -} - -impl NoopAsyncInstrument { - /// Create a new no-op async instrument - pub(crate) fn new() -> Self { - NoopAsyncInstrument { _private: () } - } -} - -impl AsyncInstrument for NoopAsyncInstrument { - fn observe(&self, _value: T, _attributes: &[KeyValue]) { - // Ignored - } -} diff --git a/opentelemetry/CHANGELOG.md b/opentelemetry/CHANGELOG.md index 18b70af0c9..cbe3269d3b 100644 --- a/opentelemetry/CHANGELOG.md +++ b/opentelemetry/CHANGELOG.md @@ -4,8 +4,9 @@ - Bump MSRV to 1.70 [#2179](https://github.com/open-telemetry/opentelemetry-rust/pull/2179) - Add `LogRecord::set_trace_context`; an optional method conditional on the `trace` feature for setting trace context on a log record. -- 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) -- 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) +- 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) +- 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) +- 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) ## v0.26.0 Released 2024-Sep-30 diff --git a/opentelemetry/src/metrics/instruments/counter.rs b/opentelemetry/src/metrics/instruments/counter.rs index 554871af8b..171756f46b 100644 --- a/opentelemetry/src/metrics/instruments/counter.rs +++ b/opentelemetry/src/metrics/instruments/counter.rs @@ -1,4 +1,4 @@ -use crate::{metrics::AsyncInstrument, KeyValue}; +use crate::KeyValue; use core::fmt; use std::sync::Arc; @@ -33,12 +33,17 @@ impl Counter { /// An async instrument that records increasing values. #[derive(Clone)] #[non_exhaustive] -pub struct ObservableCounter(Arc>); +pub struct ObservableCounter { + _marker: std::marker::PhantomData, +} impl ObservableCounter { /// Create a new observable counter. - pub fn new(inner: Arc>) -> Self { - ObservableCounter(inner) + #[allow(clippy::new_without_default)] + pub fn new() -> Self { + ObservableCounter { + _marker: std::marker::PhantomData, + } } } @@ -50,9 +55,3 @@ impl fmt::Debug for ObservableCounter { )) } } - -impl AsyncInstrument for ObservableCounter { - fn observe(&self, measurement: T, attributes: &[KeyValue]) { - self.0.observe(measurement, attributes) - } -} diff --git a/opentelemetry/src/metrics/instruments/gauge.rs b/opentelemetry/src/metrics/instruments/gauge.rs index 95e4391f77..914c3178a9 100644 --- a/opentelemetry/src/metrics/instruments/gauge.rs +++ b/opentelemetry/src/metrics/instruments/gauge.rs @@ -1,4 +1,4 @@ -use crate::{metrics::AsyncInstrument, KeyValue}; +use crate::KeyValue; use core::fmt; use std::sync::Arc; @@ -33,7 +33,9 @@ impl Gauge { /// An async instrument that records independent readings. #[derive(Clone)] #[non_exhaustive] -pub struct ObservableGauge(Arc>); +pub struct ObservableGauge { + _marker: std::marker::PhantomData, +} impl fmt::Debug for ObservableGauge where @@ -47,15 +49,12 @@ where } } -impl AsyncInstrument for ObservableGauge { - fn observe(&self, measurement: M, attributes: &[KeyValue]) { - self.0.observe(measurement, attributes) - } -} - impl ObservableGauge { /// Create a new gauge - pub fn new(inner: Arc>) -> Self { - ObservableGauge(inner) + #[allow(clippy::new_without_default)] + pub fn new() -> Self { + ObservableGauge { + _marker: std::marker::PhantomData, + } } } diff --git a/opentelemetry/src/metrics/instruments/mod.rs b/opentelemetry/src/metrics/instruments/mod.rs index 41f684e11f..c147fddfda 100644 --- a/opentelemetry/src/metrics/instruments/mod.rs +++ b/opentelemetry/src/metrics/instruments/mod.rs @@ -241,10 +241,7 @@ pub type Callback = Box) + Send + Sync>; /// Configuration for building an async instrument. #[non_exhaustive] // We expect to add more configuration fields in the future -pub struct AsyncInstrumentBuilder<'a, I, M> -where - I: AsyncInstrument, -{ +pub struct AsyncInstrumentBuilder<'a, I, M> { /// Instrument provider is used to create the instrument. pub instrument_provider: &'a dyn InstrumentProvider, @@ -263,10 +260,7 @@ where _inst: marker::PhantomData, } -impl<'a, I, M> AsyncInstrumentBuilder<'a, I, M> -where - I: AsyncInstrument, -{ +impl<'a, I, M> AsyncInstrumentBuilder<'a, I, M> { /// Create a new instrument builder pub(crate) fn new(meter: &'a Meter, name: Cow<'static, str>) -> Self { AsyncInstrumentBuilder { diff --git a/opentelemetry/src/metrics/instruments/up_down_counter.rs b/opentelemetry/src/metrics/instruments/up_down_counter.rs index 300fff7a7e..0814e41cda 100644 --- a/opentelemetry/src/metrics/instruments/up_down_counter.rs +++ b/opentelemetry/src/metrics/instruments/up_down_counter.rs @@ -2,7 +2,7 @@ use crate::KeyValue; use core::fmt; use std::sync::Arc; -use super::{AsyncInstrument, SyncInstrument}; +use super::SyncInstrument; /// An instrument that records increasing or decreasing values. #[derive(Clone)] @@ -36,7 +36,9 @@ impl UpDownCounter { /// An async instrument that records increasing or decreasing values. #[derive(Clone)] #[non_exhaustive] -pub struct ObservableUpDownCounter(Arc>); +pub struct ObservableUpDownCounter { + _marker: std::marker::PhantomData, +} impl fmt::Debug for ObservableUpDownCounter where @@ -52,13 +54,10 @@ where impl ObservableUpDownCounter { /// Create a new observable up down counter. - pub fn new(inner: Arc>) -> Self { - ObservableUpDownCounter(inner) - } -} - -impl AsyncInstrument for ObservableUpDownCounter { - fn observe(&self, measurement: T, attributes: &[KeyValue]) { - self.0.observe(measurement, attributes) + #[allow(clippy::new_without_default)] + pub fn new() -> Self { + ObservableUpDownCounter { + _marker: std::marker::PhantomData, + } } } diff --git a/opentelemetry/src/metrics/mod.rs b/opentelemetry/src/metrics/mod.rs index f2ac0a23d0..9ce27f53b6 100644 --- a/opentelemetry/src/metrics/mod.rs +++ b/opentelemetry/src/metrics/mod.rs @@ -123,9 +123,7 @@ pub trait InstrumentProvider { &self, _builder: AsyncInstrumentBuilder<'_, ObservableCounter, u64>, ) -> Result> { - Ok(ObservableCounter::new(Arc::new( - noop::NoopAsyncInstrument::new(), - ))) + Ok(ObservableCounter::new()) } /// creates an instrument for recording increasing values via callback. @@ -133,9 +131,7 @@ pub trait InstrumentProvider { &self, _builder: AsyncInstrumentBuilder<'_, ObservableCounter, f64>, ) -> Result> { - Ok(ObservableCounter::new(Arc::new( - noop::NoopAsyncInstrument::new(), - ))) + Ok(ObservableCounter::new()) } /// creates an instrument for recording changes of a value. @@ -163,9 +159,7 @@ pub trait InstrumentProvider { &self, _builder: AsyncInstrumentBuilder<'_, ObservableUpDownCounter, i64>, ) -> Result> { - Ok(ObservableUpDownCounter::new(Arc::new( - noop::NoopAsyncInstrument::new(), - ))) + Ok(ObservableUpDownCounter::new()) } /// creates an instrument for recording changes of a value via callback. @@ -173,9 +167,7 @@ pub trait InstrumentProvider { &self, _builder: AsyncInstrumentBuilder<'_, ObservableUpDownCounter, f64>, ) -> Result> { - Ok(ObservableUpDownCounter::new(Arc::new( - noop::NoopAsyncInstrument::new(), - ))) + Ok(ObservableUpDownCounter::new()) } /// creates an instrument for recording independent values. @@ -198,9 +190,7 @@ pub trait InstrumentProvider { &self, _builder: AsyncInstrumentBuilder<'_, ObservableGauge, u64>, ) -> Result> { - Ok(ObservableGauge::new(Arc::new( - noop::NoopAsyncInstrument::new(), - ))) + Ok(ObservableGauge::new()) } /// creates an instrument for recording the current value via callback. @@ -208,9 +198,7 @@ pub trait InstrumentProvider { &self, _builder: AsyncInstrumentBuilder<'_, ObservableGauge, i64>, ) -> Result> { - Ok(ObservableGauge::new(Arc::new( - noop::NoopAsyncInstrument::new(), - ))) + Ok(ObservableGauge::new()) } /// creates an instrument for recording the current value via callback. @@ -218,9 +206,7 @@ pub trait InstrumentProvider { &self, _builder: AsyncInstrumentBuilder<'_, ObservableGauge, f64>, ) -> Result> { - Ok(ObservableGauge::new(Arc::new( - noop::NoopAsyncInstrument::new(), - ))) + Ok(ObservableGauge::new()) } /// creates an instrument for recording a distribution of values. diff --git a/opentelemetry/src/metrics/noop.rs b/opentelemetry/src/metrics/noop.rs index 8e388641d2..dbe0726515 100644 --- a/opentelemetry/src/metrics/noop.rs +++ b/opentelemetry/src/metrics/noop.rs @@ -4,7 +4,7 @@ //! has been set. It is expected to have minimal resource utilization and //! runtime impact. use crate::{ - metrics::{AsyncInstrument, InstrumentProvider, Meter, MeterProvider}, + metrics::{InstrumentProvider, Meter, MeterProvider}, KeyValue, }; use std::sync::Arc; @@ -69,22 +69,3 @@ impl SyncInstrument for NoopSyncInstrument { // Ignored } } - -/// A no-op async instrument. -#[derive(Debug, Default)] -pub(crate) struct NoopAsyncInstrument { - _private: (), -} - -impl NoopAsyncInstrument { - /// Create a new no-op async instrument - pub(crate) fn new() -> Self { - NoopAsyncInstrument { _private: () } - } -} - -impl AsyncInstrument for NoopAsyncInstrument { - fn observe(&self, _value: T, _attributes: &[KeyValue]) { - // Ignored - } -}