Skip to content

Commit 3347bde

Browse files
authored
Update builder for Counter, UpDownCounter, and Gauge (#2131)
1 parent 606a3cd commit 3347bde

File tree

7 files changed

+145
-149
lines changed

7 files changed

+145
-149
lines changed

opentelemetry-sdk/src/metrics/meter.rs

Lines changed: 111 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use opentelemetry::{
55
global,
66
metrics::{
77
noop::NoopAsyncInstrument, Callback, Counter, Gauge, Histogram, HistogramBuilder,
8-
InstrumentProvider, MetricsError, ObservableCounter, ObservableGauge,
8+
InstrumentBuilder, InstrumentProvider, MetricsError, ObservableCounter, ObservableGauge,
99
ObservableUpDownCounter, Result, UpDownCounter,
1010
},
1111
};
@@ -76,28 +76,28 @@ impl SdkMeter {
7676

7777
#[doc(hidden)]
7878
impl InstrumentProvider for SdkMeter {
79-
fn u64_counter(
80-
&self,
81-
name: Cow<'static, str>,
82-
description: Option<Cow<'static, str>>,
83-
unit: Option<Cow<'static, str>>,
84-
) -> Result<Counter<u64>> {
85-
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
79+
fn u64_counter(&self, builder: InstrumentBuilder<'_, Counter<u64>>) -> Result<Counter<u64>> {
80+
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
8681
let p = InstrumentResolver::new(self, &self.u64_resolver);
87-
p.lookup(InstrumentKind::Counter, name, description, unit)
88-
.map(|i| Counter::new(Arc::new(i)))
82+
p.lookup(
83+
InstrumentKind::Counter,
84+
builder.name,
85+
builder.description,
86+
builder.unit,
87+
)
88+
.map(|i| Counter::new(Arc::new(i)))
8989
}
9090

91-
fn f64_counter(
92-
&self,
93-
name: Cow<'static, str>,
94-
description: Option<Cow<'static, str>>,
95-
unit: Option<Cow<'static, str>>,
96-
) -> Result<Counter<f64>> {
97-
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
91+
fn f64_counter(&self, builder: InstrumentBuilder<'_, Counter<f64>>) -> Result<Counter<f64>> {
92+
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
9893
let p = InstrumentResolver::new(self, &self.f64_resolver);
99-
p.lookup(InstrumentKind::Counter, name, description, unit)
100-
.map(|i| Counter::new(Arc::new(i)))
94+
p.lookup(
95+
InstrumentKind::Counter,
96+
builder.name,
97+
builder.description,
98+
builder.unit,
99+
)
100+
.map(|i| Counter::new(Arc::new(i)))
101101
}
102102

103103
fn u64_observable_counter(
@@ -161,26 +161,32 @@ impl InstrumentProvider for SdkMeter {
161161

162162
fn i64_up_down_counter(
163163
&self,
164-
name: Cow<'static, str>,
165-
description: Option<Cow<'static, str>>,
166-
unit: Option<Cow<'static, str>>,
164+
builder: InstrumentBuilder<'_, UpDownCounter<i64>>,
167165
) -> Result<UpDownCounter<i64>> {
168-
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
166+
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
169167
let p = InstrumentResolver::new(self, &self.i64_resolver);
170-
p.lookup(InstrumentKind::UpDownCounter, name, description, unit)
171-
.map(|i| UpDownCounter::new(Arc::new(i)))
168+
p.lookup(
169+
InstrumentKind::UpDownCounter,
170+
builder.name,
171+
builder.description,
172+
builder.unit,
173+
)
174+
.map(|i| UpDownCounter::new(Arc::new(i)))
172175
}
173176

174177
fn f64_up_down_counter(
175178
&self,
176-
name: Cow<'static, str>,
177-
description: Option<Cow<'static, str>>,
178-
unit: Option<Cow<'static, str>>,
179+
builder: InstrumentBuilder<'_, UpDownCounter<f64>>,
179180
) -> Result<UpDownCounter<f64>> {
180-
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
181+
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
181182
let p = InstrumentResolver::new(self, &self.f64_resolver);
182-
p.lookup(InstrumentKind::UpDownCounter, name, description, unit)
183-
.map(|i| UpDownCounter::new(Arc::new(i)))
183+
p.lookup(
184+
InstrumentKind::UpDownCounter,
185+
builder.name,
186+
builder.description,
187+
builder.unit,
188+
)
189+
.map(|i| UpDownCounter::new(Arc::new(i)))
184190
}
185191

186192
fn i64_observable_up_down_counter(
@@ -247,40 +253,40 @@ impl InstrumentProvider for SdkMeter {
247253
Ok(ObservableUpDownCounter::new(observable))
248254
}
249255

250-
fn u64_gauge(
251-
&self,
252-
name: Cow<'static, str>,
253-
description: Option<Cow<'static, str>>,
254-
unit: Option<Cow<'static, str>>,
255-
) -> Result<Gauge<u64>> {
256-
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
256+
fn u64_gauge(&self, builder: InstrumentBuilder<'_, Gauge<u64>>) -> Result<Gauge<u64>> {
257+
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
257258
let p = InstrumentResolver::new(self, &self.u64_resolver);
258-
p.lookup(InstrumentKind::Gauge, name, description, unit)
259-
.map(|i| Gauge::new(Arc::new(i)))
259+
p.lookup(
260+
InstrumentKind::Gauge,
261+
builder.name,
262+
builder.description,
263+
builder.unit,
264+
)
265+
.map(|i| Gauge::new(Arc::new(i)))
260266
}
261267

262-
fn f64_gauge(
263-
&self,
264-
name: Cow<'static, str>,
265-
description: Option<Cow<'static, str>>,
266-
unit: Option<Cow<'static, str>>,
267-
) -> Result<Gauge<f64>> {
268-
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
268+
fn f64_gauge(&self, builder: InstrumentBuilder<'_, Gauge<f64>>) -> Result<Gauge<f64>> {
269+
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
269270
let p = InstrumentResolver::new(self, &self.f64_resolver);
270-
p.lookup(InstrumentKind::Gauge, name, description, unit)
271-
.map(|i| Gauge::new(Arc::new(i)))
271+
p.lookup(
272+
InstrumentKind::Gauge,
273+
builder.name,
274+
builder.description,
275+
builder.unit,
276+
)
277+
.map(|i| Gauge::new(Arc::new(i)))
272278
}
273279

274-
fn i64_gauge(
275-
&self,
276-
name: Cow<'static, str>,
277-
description: Option<Cow<'static, str>>,
278-
unit: Option<Cow<'static, str>>,
279-
) -> Result<Gauge<i64>> {
280-
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
280+
fn i64_gauge(&self, builder: InstrumentBuilder<'_, Gauge<i64>>) -> Result<Gauge<i64>> {
281+
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
281282
let p = InstrumentResolver::new(self, &self.i64_resolver);
282-
p.lookup(InstrumentKind::Gauge, name, description, unit)
283-
.map(|i| Gauge::new(Arc::new(i)))
283+
p.lookup(
284+
InstrumentKind::Gauge,
285+
builder.name,
286+
builder.description,
287+
builder.unit,
288+
)
289+
.map(|i| Gauge::new(Arc::new(i)))
284290
}
285291

286292
fn u64_observable_gauge(
@@ -582,8 +588,13 @@ mod tests {
582588
}
583589
};
584590

585-
assert(meter.u64_counter(name.into(), None, None).map(|_| ()));
586-
assert(meter.f64_counter(name.into(), None, None).map(|_| ()));
591+
// Get handle to InstrumentBuilder for testing
592+
let global_meter = global::meter("test");
593+
let counter_builder_u64 = global_meter.u64_counter(name);
594+
let counter_builder_f64 = global_meter.f64_counter(name);
595+
596+
assert(meter.u64_counter(counter_builder_u64).map(|_| ()));
597+
assert(meter.f64_counter(counter_builder_f64).map(|_| ()));
587598
assert(
588599
meter
589600
.u64_observable_counter(name.into(), None, None, Vec::new())
@@ -594,14 +605,19 @@ mod tests {
594605
.f64_observable_counter(name.into(), None, None, Vec::new())
595606
.map(|_| ()),
596607
);
608+
609+
// Get handle to InstrumentBuilder for testing
610+
let up_down_counter_builder_i64 = global_meter.i64_up_down_counter(name);
611+
let up_down_counter_builder_f64 = global_meter.f64_up_down_counter(name);
612+
597613
assert(
598614
meter
599-
.i64_up_down_counter(name.into(), None, None)
615+
.i64_up_down_counter(up_down_counter_builder_i64)
600616
.map(|_| ()),
601617
);
602618
assert(
603619
meter
604-
.f64_up_down_counter(name.into(), None, None)
620+
.f64_up_down_counter(up_down_counter_builder_f64)
605621
.map(|_| ()),
606622
);
607623
assert(
@@ -614,9 +630,15 @@ mod tests {
614630
.f64_observable_up_down_counter(name.into(), None, None, Vec::new())
615631
.map(|_| ()),
616632
);
617-
assert(meter.u64_gauge(name.into(), None, None).map(|_| ()));
618-
assert(meter.f64_gauge(name.into(), None, None).map(|_| ()));
619-
assert(meter.i64_gauge(name.into(), None, None).map(|_| ()));
633+
634+
// Get handle to InstrumentBuilder for testing
635+
let gauge_builder_u64 = global_meter.u64_gauge(name);
636+
let gauge_builder_f64 = global_meter.f64_gauge(name);
637+
let gauge_builder_i64 = global_meter.i64_gauge(name);
638+
639+
assert(meter.u64_gauge(gauge_builder_u64).map(|_| ()));
640+
assert(meter.f64_gauge(gauge_builder_f64).map(|_| ()));
641+
assert(meter.i64_gauge(gauge_builder_i64).map(|_| ()));
620642
assert(
621643
meter
622644
.u64_observable_gauge(name.into(), None, None, Vec::new())
@@ -634,7 +656,6 @@ mod tests {
634656
);
635657

636658
// Get handle to HistogramBuilder for testing
637-
let global_meter = global::meter("test");
638659
let histogram_builder_f64 = global_meter.f64_histogram(name);
639660
let histogram_builder_u64 = global_meter.u64_histogram(name);
640661

@@ -667,16 +688,18 @@ mod tests {
667688
}
668689
};
669690
let unit = Some(unit.into());
670-
assert(
671-
meter
672-
.u64_counter("test".into(), None, unit.clone())
673-
.map(|_| ()),
674-
);
675-
assert(
676-
meter
677-
.f64_counter("test".into(), None, unit.clone())
678-
.map(|_| ()),
679-
);
691+
692+
// Get handle to InstrumentBuilder for testing
693+
let global_meter = global::meter("test");
694+
let counter_builder_u64 = global_meter
695+
.u64_counter("test")
696+
.with_unit(unit.clone().unwrap());
697+
let counter_builder_f64 = global_meter
698+
.f64_counter("test")
699+
.with_unit(unit.clone().unwrap());
700+
701+
assert(meter.u64_counter(counter_builder_u64).map(|_| ()));
702+
assert(meter.f64_counter(counter_builder_f64).map(|_| ()));
680703
assert(
681704
meter
682705
.u64_observable_counter("test".into(), None, unit.clone(), Vec::new())
@@ -687,14 +710,24 @@ mod tests {
687710
.f64_observable_counter("test".into(), None, unit.clone(), Vec::new())
688711
.map(|_| ()),
689712
);
713+
714+
// Get handle to InstrumentBuilder for testing
715+
let up_down_counter_builder_i64 = global_meter
716+
.i64_up_down_counter("test")
717+
.with_unit(unit.clone().unwrap());
718+
let up_down_counter_builder_f64 = global_meter
719+
.f64_up_down_counter("test")
720+
.with_unit(unit.clone().unwrap());
721+
690722
assert(
691723
meter
692-
.i64_up_down_counter("test".into(), None, unit.clone())
724+
.i64_up_down_counter(up_down_counter_builder_i64)
693725
.map(|_| ()),
694726
);
727+
695728
assert(
696729
meter
697-
.f64_up_down_counter("test".into(), None, unit.clone())
730+
.f64_up_down_counter(up_down_counter_builder_f64)
698731
.map(|_| ()),
699732
);
700733
assert(
@@ -724,7 +757,6 @@ mod tests {
724757
);
725758

726759
// Get handle to HistogramBuilder for testing
727-
let global_meter = global::meter("test");
728760
let histogram_builder_f64 = global_meter
729761
.f64_histogram("test")
730762
.with_unit(unit.clone().unwrap());

opentelemetry/src/metrics/instruments/counter.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::{
2-
metrics::{AsyncInstrument, AsyncInstrumentBuilder, InstrumentBuilder, MetricsError},
2+
metrics::{AsyncInstrument, AsyncInstrumentBuilder, MetricsError},
33
KeyValue,
44
};
55
use core::fmt;
66
use std::any::Any;
77
use std::sync::Arc;
88

9+
use super::InstrumentBuilder;
10+
911
/// An SDK implemented instrument that records increasing values.
1012
pub trait SyncCounter<T> {
1113
/// Records an increment to the counter.
@@ -41,19 +43,15 @@ impl TryFrom<InstrumentBuilder<'_, Counter<u64>>> for Counter<u64> {
4143
type Error = MetricsError;
4244

4345
fn try_from(builder: InstrumentBuilder<'_, Counter<u64>>) -> Result<Self, Self::Error> {
44-
builder
45-
.instrument_provider
46-
.u64_counter(builder.name, builder.description, builder.unit)
46+
builder.instrument_provider.u64_counter(builder)
4747
}
4848
}
4949

5050
impl TryFrom<InstrumentBuilder<'_, Counter<f64>>> for Counter<f64> {
5151
type Error = MetricsError;
5252

5353
fn try_from(builder: InstrumentBuilder<'_, Counter<f64>>) -> Result<Self, Self::Error> {
54-
builder
55-
.instrument_provider
56-
.f64_counter(builder.name, builder.description, builder.unit)
54+
builder.instrument_provider.f64_counter(builder)
5755
}
5856
}
5957

opentelemetry/src/metrics/instruments/gauge.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,23 @@ impl TryFrom<InstrumentBuilder<'_, Gauge<u64>>> for Gauge<u64> {
4141
type Error = MetricsError;
4242

4343
fn try_from(builder: InstrumentBuilder<'_, Gauge<u64>>) -> Result<Self, Self::Error> {
44-
builder
45-
.instrument_provider
46-
.u64_gauge(builder.name, builder.description, builder.unit)
44+
builder.instrument_provider.u64_gauge(builder)
4745
}
4846
}
4947

5048
impl TryFrom<InstrumentBuilder<'_, Gauge<f64>>> for Gauge<f64> {
5149
type Error = MetricsError;
5250

5351
fn try_from(builder: InstrumentBuilder<'_, Gauge<f64>>) -> Result<Self, Self::Error> {
54-
builder
55-
.instrument_provider
56-
.f64_gauge(builder.name, builder.description, builder.unit)
52+
builder.instrument_provider.f64_gauge(builder)
5753
}
5854
}
5955

6056
impl TryFrom<InstrumentBuilder<'_, Gauge<i64>>> for Gauge<i64> {
6157
type Error = MetricsError;
6258

6359
fn try_from(builder: InstrumentBuilder<'_, Gauge<i64>>) -> Result<Self, Self::Error> {
64-
builder
65-
.instrument_provider
66-
.i64_gauge(builder.name, builder.description, builder.unit)
60+
builder.instrument_provider.i64_gauge(builder)
6761
}
6862
}
6963

0 commit comments

Comments
 (0)