diff --git a/opentelemetry/src/metrics/counter.rs b/opentelemetry/src/metrics/counter.rs index 6d96c89d29..d45054ca3c 100644 --- a/opentelemetry/src/metrics/counter.rs +++ b/opentelemetry/src/metrics/counter.rs @@ -3,7 +3,7 @@ use crate::{ sync_instrument::{SyncBoundInstrument, SyncInstrument}, Descriptor, InstrumentKind, Measurement, Meter, Number, NumberKind, Result, }, - KeyValue, + KeyValue, Unit, }; use std::marker; @@ -84,6 +84,12 @@ impl<'a, T> CounterBuilder<'a, T> { self } + /// Set the unit for this counter. + pub fn with_unit(mut self, unit: Unit) -> Self { + self.descriptor.config.unit = Some(unit); + self + } + /// Creates a new counter instrument. pub fn try_init(self) -> Result> { let instrument = self.meter.new_sync_instrument(self.descriptor)?; diff --git a/opentelemetry/src/metrics/descriptor.rs b/opentelemetry/src/metrics/descriptor.rs index addcfec295..83e37eb0c2 100644 --- a/opentelemetry/src/metrics/descriptor.rs +++ b/opentelemetry/src/metrics/descriptor.rs @@ -10,7 +10,7 @@ pub struct Descriptor { name: String, instrument_kind: InstrumentKind, number_kind: NumberKind, - config: InstrumentConfig, + pub(crate) config: InstrumentConfig, attribute_hash: u64, } diff --git a/opentelemetry/src/metrics/observer.rs b/opentelemetry/src/metrics/observer.rs index 49a8749721..da6e70a59a 100644 --- a/opentelemetry/src/metrics/observer.rs +++ b/opentelemetry/src/metrics/observer.rs @@ -2,6 +2,7 @@ use crate::metrics::{ sdk_api, AsyncRunner, Descriptor, InstrumentKind, Meter, Number, NumberKind, Observation, Result, }; +use crate::Unit; use std::sync::Arc; /// An Observer callback that can report observations for multiple instruments. @@ -127,6 +128,12 @@ impl<'a, T> SumObserverBuilder<'a, T> { self } + /// Set the unit for this `SumObserver`. + pub fn with_unit(mut self, unit: Unit) -> Self { + self.descriptor.config.unit = Some(unit); + self + } + /// Create a `SumObserver` from this configuration. pub fn try_init(self) -> Result> { let instrument = self @@ -214,6 +221,12 @@ impl<'a, T> UpDownSumObserverBuilder<'a, T> { self } + /// Set the unit for this `UpDownSumObserver`. + pub fn with_unit(mut self, unit: Unit) -> Self { + self.descriptor.config.unit = Some(unit); + self + } + /// Create a `UpDownSumObserver` from this configuration. pub fn try_init(self) -> Result> { let instrument = self @@ -299,6 +312,12 @@ impl<'a, T> ValueObserverBuilder<'a, T> { self } + /// Set the unit for this `ValueObserver`. + pub fn with_unit(mut self, unit: Unit) -> Self { + self.descriptor.config.unit = Some(unit); + self + } + /// Create a `ValueObserver` from this configuration. pub fn try_init(self) -> Result> { let instrument = self diff --git a/opentelemetry/src/metrics/up_down_counter.rs b/opentelemetry/src/metrics/up_down_counter.rs index aaa3742d5f..a5a1d7d87d 100644 --- a/opentelemetry/src/metrics/up_down_counter.rs +++ b/opentelemetry/src/metrics/up_down_counter.rs @@ -3,7 +3,7 @@ use crate::{ sync_instrument::{SyncBoundInstrument, SyncInstrument}, Descriptor, InstrumentKind, Measurement, Meter, Number, NumberKind, Result, }, - KeyValue, + KeyValue, Unit, }; use std::marker; @@ -84,6 +84,12 @@ impl<'a, T> UpDownCounterBuilder<'a, T> { self } + /// Set the unit for this counter. + pub fn with_unit(mut self, unit: Unit) -> Self { + self.descriptor.config.unit = Some(unit); + self + } + /// Creates a new counter instrument. pub fn try_init(self) -> Result> { let instrument = self.meter.new_sync_instrument(self.descriptor)?; diff --git a/opentelemetry/src/metrics/value_recorder.rs b/opentelemetry/src/metrics/value_recorder.rs index 56dea086d1..85117fca85 100644 --- a/opentelemetry/src/metrics/value_recorder.rs +++ b/opentelemetry/src/metrics/value_recorder.rs @@ -3,6 +3,7 @@ use crate::metrics::{ Descriptor, InstrumentKind, Measurement, Meter, Number, NumberKind, Result, }; use crate::KeyValue; +use crate::Unit; use std::marker; /// ValueRecorder is a metric that records per-request non-additive values. @@ -84,6 +85,12 @@ impl<'a, T> ValueRecorderBuilder<'a, T> { self } + /// Set the unit for this `ValueRecorder`. + pub fn with_unit(mut self, unit: Unit) -> Self { + self.descriptor.config.unit = Some(unit); + self + } + /// Creates a new value recorder. pub fn try_init(self) -> Result> { let instrument = self.meter.new_sync_instrument(self.descriptor)?;