Skip to content

Commit 6212399

Browse files
authored
Add with_unit method for metrics API (#431)
Add `with_unit` method to `CounterBuilder`, `SumObserverBuilder`, `UpDownSumObserverBuilder`, `ValueObserverBuilder` structs
1 parent c31a449 commit 6212399

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

opentelemetry/src/metrics/counter.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
sync_instrument::{SyncBoundInstrument, SyncInstrument},
44
Descriptor, InstrumentKind, Measurement, Meter, Number, NumberKind, Result,
55
},
6-
KeyValue,
6+
KeyValue, Unit,
77
};
88
use std::marker;
99

@@ -84,6 +84,12 @@ impl<'a, T> CounterBuilder<'a, T> {
8484
self
8585
}
8686

87+
/// Set the unit for this counter.
88+
pub fn with_unit(mut self, unit: Unit) -> Self {
89+
self.descriptor.config.unit = Some(unit);
90+
self
91+
}
92+
8793
/// Creates a new counter instrument.
8894
pub fn try_init(self) -> Result<Counter<T>> {
8995
let instrument = self.meter.new_sync_instrument(self.descriptor)?;

opentelemetry/src/metrics/descriptor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct Descriptor {
1010
name: String,
1111
instrument_kind: InstrumentKind,
1212
number_kind: NumberKind,
13-
config: InstrumentConfig,
13+
pub(crate) config: InstrumentConfig,
1414
attribute_hash: u64,
1515
}
1616

opentelemetry/src/metrics/observer.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::metrics::{
22
sdk_api, AsyncRunner, Descriptor, InstrumentKind, Meter, Number, NumberKind, Observation,
33
Result,
44
};
5+
use crate::Unit;
56
use std::sync::Arc;
67

78
/// An Observer callback that can report observations for multiple instruments.
@@ -127,6 +128,12 @@ impl<'a, T> SumObserverBuilder<'a, T> {
127128
self
128129
}
129130

131+
/// Set the unit for this `SumObserver`.
132+
pub fn with_unit(mut self, unit: Unit) -> Self {
133+
self.descriptor.config.unit = Some(unit);
134+
self
135+
}
136+
130137
/// Create a `SumObserver` from this configuration.
131138
pub fn try_init(self) -> Result<SumObserver<T>> {
132139
let instrument = self
@@ -214,6 +221,12 @@ impl<'a, T> UpDownSumObserverBuilder<'a, T> {
214221
self
215222
}
216223

224+
/// Set the unit for this `UpDownSumObserver`.
225+
pub fn with_unit(mut self, unit: Unit) -> Self {
226+
self.descriptor.config.unit = Some(unit);
227+
self
228+
}
229+
217230
/// Create a `UpDownSumObserver` from this configuration.
218231
pub fn try_init(self) -> Result<UpDownSumObserver<T>> {
219232
let instrument = self
@@ -299,6 +312,12 @@ impl<'a, T> ValueObserverBuilder<'a, T> {
299312
self
300313
}
301314

315+
/// Set the unit for this `ValueObserver`.
316+
pub fn with_unit(mut self, unit: Unit) -> Self {
317+
self.descriptor.config.unit = Some(unit);
318+
self
319+
}
320+
302321
/// Create a `ValueObserver` from this configuration.
303322
pub fn try_init(self) -> Result<ValueObserver<T>> {
304323
let instrument = self

opentelemetry/src/metrics/up_down_counter.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
sync_instrument::{SyncBoundInstrument, SyncInstrument},
44
Descriptor, InstrumentKind, Measurement, Meter, Number, NumberKind, Result,
55
},
6-
KeyValue,
6+
KeyValue, Unit,
77
};
88
use std::marker;
99

@@ -84,6 +84,12 @@ impl<'a, T> UpDownCounterBuilder<'a, T> {
8484
self
8585
}
8686

87+
/// Set the unit for this counter.
88+
pub fn with_unit(mut self, unit: Unit) -> Self {
89+
self.descriptor.config.unit = Some(unit);
90+
self
91+
}
92+
8793
/// Creates a new counter instrument.
8894
pub fn try_init(self) -> Result<UpDownCounter<T>> {
8995
let instrument = self.meter.new_sync_instrument(self.descriptor)?;

opentelemetry/src/metrics/value_recorder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::metrics::{
33
Descriptor, InstrumentKind, Measurement, Meter, Number, NumberKind, Result,
44
};
55
use crate::KeyValue;
6+
use crate::Unit;
67
use std::marker;
78

89
/// ValueRecorder is a metric that records per-request non-additive values.
@@ -84,6 +85,12 @@ impl<'a, T> ValueRecorderBuilder<'a, T> {
8485
self
8586
}
8687

88+
/// Set the unit for this `ValueRecorder`.
89+
pub fn with_unit(mut self, unit: Unit) -> Self {
90+
self.descriptor.config.unit = Some(unit);
91+
self
92+
}
93+
8794
/// Creates a new value recorder.
8895
pub fn try_init(self) -> Result<ValueRecorder<T>> {
8996
let instrument = self.meter.new_sync_instrument(self.descriptor)?;

0 commit comments

Comments
 (0)