Skip to content

Commit

Permalink
Add with_unit method for metrics API (#431)
Browse files Browse the repository at this point in the history
Add `with_unit` method to `CounterBuilder`, `SumObserverBuilder`,
`UpDownSumObserverBuilder`, `ValueObserverBuilder` structs
  • Loading branch information
ozerovandrei committed Jan 21, 2021
1 parent c31a449 commit 6212399
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
8 changes: 7 additions & 1 deletion opentelemetry/src/metrics/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
sync_instrument::{SyncBoundInstrument, SyncInstrument},
Descriptor, InstrumentKind, Measurement, Meter, Number, NumberKind, Result,
},
KeyValue,
KeyValue, Unit,
};
use std::marker;

Expand Down Expand Up @@ -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<Counter<T>> {
let instrument = self.meter.new_sync_instrument(self.descriptor)?;
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry/src/metrics/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Descriptor {
name: String,
instrument_kind: InstrumentKind,
number_kind: NumberKind,
config: InstrumentConfig,
pub(crate) config: InstrumentConfig,
attribute_hash: u64,
}

Expand Down
19 changes: 19 additions & 0 deletions opentelemetry/src/metrics/observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<SumObserver<T>> {
let instrument = self
Expand Down Expand Up @@ -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<UpDownSumObserver<T>> {
let instrument = self
Expand Down Expand Up @@ -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<ValueObserver<T>> {
let instrument = self
Expand Down
8 changes: 7 additions & 1 deletion opentelemetry/src/metrics/up_down_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
sync_instrument::{SyncBoundInstrument, SyncInstrument},
Descriptor, InstrumentKind, Measurement, Meter, Number, NumberKind, Result,
},
KeyValue,
KeyValue, Unit,
};
use std::marker;

Expand Down Expand Up @@ -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<UpDownCounter<T>> {
let instrument = self.meter.new_sync_instrument(self.descriptor)?;
Expand Down
7 changes: 7 additions & 0 deletions opentelemetry/src/metrics/value_recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<ValueRecorder<T>> {
let instrument = self.meter.new_sync_instrument(self.descriptor)?;
Expand Down

0 comments on commit 6212399

Please sign in to comment.