Skip to content

Commit

Permalink
Update builder for Counter, UpDownCounter, and Gauge (#2131)
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla authored Sep 20, 2024
1 parent 606a3cd commit 3347bde
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 149 deletions.
190 changes: 111 additions & 79 deletions opentelemetry-sdk/src/metrics/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use opentelemetry::{
global,
metrics::{
noop::NoopAsyncInstrument, Callback, Counter, Gauge, Histogram, HistogramBuilder,
InstrumentProvider, MetricsError, ObservableCounter, ObservableGauge,
InstrumentBuilder, InstrumentProvider, MetricsError, ObservableCounter, ObservableGauge,
ObservableUpDownCounter, Result, UpDownCounter,
},
};
Expand Down Expand Up @@ -76,28 +76,28 @@ impl SdkMeter {

#[doc(hidden)]
impl InstrumentProvider for SdkMeter {
fn u64_counter(
&self,
name: Cow<'static, str>,
description: Option<Cow<'static, str>>,
unit: Option<Cow<'static, str>>,
) -> Result<Counter<u64>> {
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
fn u64_counter(&self, builder: InstrumentBuilder<'_, Counter<u64>>) -> Result<Counter<u64>> {
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
let p = InstrumentResolver::new(self, &self.u64_resolver);
p.lookup(InstrumentKind::Counter, name, description, unit)
.map(|i| Counter::new(Arc::new(i)))
p.lookup(
InstrumentKind::Counter,
builder.name,
builder.description,
builder.unit,
)
.map(|i| Counter::new(Arc::new(i)))
}

fn f64_counter(
&self,
name: Cow<'static, str>,
description: Option<Cow<'static, str>>,
unit: Option<Cow<'static, str>>,
) -> Result<Counter<f64>> {
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
fn f64_counter(&self, builder: InstrumentBuilder<'_, Counter<f64>>) -> Result<Counter<f64>> {
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
let p = InstrumentResolver::new(self, &self.f64_resolver);
p.lookup(InstrumentKind::Counter, name, description, unit)
.map(|i| Counter::new(Arc::new(i)))
p.lookup(
InstrumentKind::Counter,
builder.name,
builder.description,
builder.unit,
)
.map(|i| Counter::new(Arc::new(i)))
}

fn u64_observable_counter(
Expand Down Expand Up @@ -161,26 +161,32 @@ impl InstrumentProvider for SdkMeter {

fn i64_up_down_counter(
&self,
name: Cow<'static, str>,
description: Option<Cow<'static, str>>,
unit: Option<Cow<'static, str>>,
builder: InstrumentBuilder<'_, UpDownCounter<i64>>,
) -> Result<UpDownCounter<i64>> {
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
let p = InstrumentResolver::new(self, &self.i64_resolver);
p.lookup(InstrumentKind::UpDownCounter, name, description, unit)
.map(|i| UpDownCounter::new(Arc::new(i)))
p.lookup(
InstrumentKind::UpDownCounter,
builder.name,
builder.description,
builder.unit,
)
.map(|i| UpDownCounter::new(Arc::new(i)))
}

fn f64_up_down_counter(
&self,
name: Cow<'static, str>,
description: Option<Cow<'static, str>>,
unit: Option<Cow<'static, str>>,
builder: InstrumentBuilder<'_, UpDownCounter<f64>>,
) -> Result<UpDownCounter<f64>> {
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
let p = InstrumentResolver::new(self, &self.f64_resolver);
p.lookup(InstrumentKind::UpDownCounter, name, description, unit)
.map(|i| UpDownCounter::new(Arc::new(i)))
p.lookup(
InstrumentKind::UpDownCounter,
builder.name,
builder.description,
builder.unit,
)
.map(|i| UpDownCounter::new(Arc::new(i)))
}

fn i64_observable_up_down_counter(
Expand Down Expand Up @@ -247,40 +253,40 @@ impl InstrumentProvider for SdkMeter {
Ok(ObservableUpDownCounter::new(observable))
}

fn u64_gauge(
&self,
name: Cow<'static, str>,
description: Option<Cow<'static, str>>,
unit: Option<Cow<'static, str>>,
) -> Result<Gauge<u64>> {
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
fn u64_gauge(&self, builder: InstrumentBuilder<'_, Gauge<u64>>) -> Result<Gauge<u64>> {
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
let p = InstrumentResolver::new(self, &self.u64_resolver);
p.lookup(InstrumentKind::Gauge, name, description, unit)
.map(|i| Gauge::new(Arc::new(i)))
p.lookup(
InstrumentKind::Gauge,
builder.name,
builder.description,
builder.unit,
)
.map(|i| Gauge::new(Arc::new(i)))
}

fn f64_gauge(
&self,
name: Cow<'static, str>,
description: Option<Cow<'static, str>>,
unit: Option<Cow<'static, str>>,
) -> Result<Gauge<f64>> {
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
fn f64_gauge(&self, builder: InstrumentBuilder<'_, Gauge<f64>>) -> Result<Gauge<f64>> {
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
let p = InstrumentResolver::new(self, &self.f64_resolver);
p.lookup(InstrumentKind::Gauge, name, description, unit)
.map(|i| Gauge::new(Arc::new(i)))
p.lookup(
InstrumentKind::Gauge,
builder.name,
builder.description,
builder.unit,
)
.map(|i| Gauge::new(Arc::new(i)))
}

fn i64_gauge(
&self,
name: Cow<'static, str>,
description: Option<Cow<'static, str>>,
unit: Option<Cow<'static, str>>,
) -> Result<Gauge<i64>> {
validate_instrument_config(name.as_ref(), &unit, self.validation_policy)?;
fn i64_gauge(&self, builder: InstrumentBuilder<'_, Gauge<i64>>) -> Result<Gauge<i64>> {
validate_instrument_config(builder.name.as_ref(), &builder.unit, self.validation_policy)?;
let p = InstrumentResolver::new(self, &self.i64_resolver);
p.lookup(InstrumentKind::Gauge, name, description, unit)
.map(|i| Gauge::new(Arc::new(i)))
p.lookup(
InstrumentKind::Gauge,
builder.name,
builder.description,
builder.unit,
)
.map(|i| Gauge::new(Arc::new(i)))
}

fn u64_observable_gauge(
Expand Down Expand Up @@ -582,8 +588,13 @@ mod tests {
}
};

assert(meter.u64_counter(name.into(), None, None).map(|_| ()));
assert(meter.f64_counter(name.into(), None, None).map(|_| ()));
// Get handle to InstrumentBuilder for testing
let global_meter = global::meter("test");
let counter_builder_u64 = global_meter.u64_counter(name);
let counter_builder_f64 = global_meter.f64_counter(name);

assert(meter.u64_counter(counter_builder_u64).map(|_| ()));
assert(meter.f64_counter(counter_builder_f64).map(|_| ()));
assert(
meter
.u64_observable_counter(name.into(), None, None, Vec::new())
Expand All @@ -594,14 +605,19 @@ mod tests {
.f64_observable_counter(name.into(), None, None, Vec::new())
.map(|_| ()),
);

// Get handle to InstrumentBuilder for testing
let up_down_counter_builder_i64 = global_meter.i64_up_down_counter(name);
let up_down_counter_builder_f64 = global_meter.f64_up_down_counter(name);

assert(
meter
.i64_up_down_counter(name.into(), None, None)
.i64_up_down_counter(up_down_counter_builder_i64)
.map(|_| ()),
);
assert(
meter
.f64_up_down_counter(name.into(), None, None)
.f64_up_down_counter(up_down_counter_builder_f64)
.map(|_| ()),
);
assert(
Expand All @@ -614,9 +630,15 @@ mod tests {
.f64_observable_up_down_counter(name.into(), None, None, Vec::new())
.map(|_| ()),
);
assert(meter.u64_gauge(name.into(), None, None).map(|_| ()));
assert(meter.f64_gauge(name.into(), None, None).map(|_| ()));
assert(meter.i64_gauge(name.into(), None, None).map(|_| ()));

// Get handle to InstrumentBuilder for testing
let gauge_builder_u64 = global_meter.u64_gauge(name);
let gauge_builder_f64 = global_meter.f64_gauge(name);
let gauge_builder_i64 = global_meter.i64_gauge(name);

assert(meter.u64_gauge(gauge_builder_u64).map(|_| ()));
assert(meter.f64_gauge(gauge_builder_f64).map(|_| ()));
assert(meter.i64_gauge(gauge_builder_i64).map(|_| ()));
assert(
meter
.u64_observable_gauge(name.into(), None, None, Vec::new())
Expand All @@ -634,7 +656,6 @@ mod tests {
);

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

Expand Down Expand Up @@ -667,16 +688,18 @@ mod tests {
}
};
let unit = Some(unit.into());
assert(
meter
.u64_counter("test".into(), None, unit.clone())
.map(|_| ()),
);
assert(
meter
.f64_counter("test".into(), None, unit.clone())
.map(|_| ()),
);

// Get handle to InstrumentBuilder for testing
let global_meter = global::meter("test");
let counter_builder_u64 = global_meter
.u64_counter("test")
.with_unit(unit.clone().unwrap());
let counter_builder_f64 = global_meter
.f64_counter("test")
.with_unit(unit.clone().unwrap());

assert(meter.u64_counter(counter_builder_u64).map(|_| ()));
assert(meter.f64_counter(counter_builder_f64).map(|_| ()));
assert(
meter
.u64_observable_counter("test".into(), None, unit.clone(), Vec::new())
Expand All @@ -687,14 +710,24 @@ mod tests {
.f64_observable_counter("test".into(), None, unit.clone(), Vec::new())
.map(|_| ()),
);

// Get handle to InstrumentBuilder for testing
let up_down_counter_builder_i64 = global_meter
.i64_up_down_counter("test")
.with_unit(unit.clone().unwrap());
let up_down_counter_builder_f64 = global_meter
.f64_up_down_counter("test")
.with_unit(unit.clone().unwrap());

assert(
meter
.i64_up_down_counter("test".into(), None, unit.clone())
.i64_up_down_counter(up_down_counter_builder_i64)
.map(|_| ()),
);

assert(
meter
.f64_up_down_counter("test".into(), None, unit.clone())
.f64_up_down_counter(up_down_counter_builder_f64)
.map(|_| ()),
);
assert(
Expand Down Expand Up @@ -724,7 +757,6 @@ mod tests {
);

// Get handle to HistogramBuilder for testing
let global_meter = global::meter("test");
let histogram_builder_f64 = global_meter
.f64_histogram("test")
.with_unit(unit.clone().unwrap());
Expand Down
12 changes: 5 additions & 7 deletions opentelemetry/src/metrics/instruments/counter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::{
metrics::{AsyncInstrument, AsyncInstrumentBuilder, InstrumentBuilder, MetricsError},
metrics::{AsyncInstrument, AsyncInstrumentBuilder, MetricsError},
KeyValue,
};
use core::fmt;
use std::any::Any;
use std::sync::Arc;

use super::InstrumentBuilder;

/// An SDK implemented instrument that records increasing values.
pub trait SyncCounter<T> {
/// Records an increment to the counter.
Expand Down Expand Up @@ -41,19 +43,15 @@ impl TryFrom<InstrumentBuilder<'_, Counter<u64>>> for Counter<u64> {
type Error = MetricsError;

fn try_from(builder: InstrumentBuilder<'_, Counter<u64>>) -> Result<Self, Self::Error> {
builder
.instrument_provider
.u64_counter(builder.name, builder.description, builder.unit)
builder.instrument_provider.u64_counter(builder)
}
}

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

fn try_from(builder: InstrumentBuilder<'_, Counter<f64>>) -> Result<Self, Self::Error> {
builder
.instrument_provider
.f64_counter(builder.name, builder.description, builder.unit)
builder.instrument_provider.f64_counter(builder)
}
}

Expand Down
12 changes: 3 additions & 9 deletions opentelemetry/src/metrics/instruments/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,23 @@ impl TryFrom<InstrumentBuilder<'_, Gauge<u64>>> for Gauge<u64> {
type Error = MetricsError;

fn try_from(builder: InstrumentBuilder<'_, Gauge<u64>>) -> Result<Self, Self::Error> {
builder
.instrument_provider
.u64_gauge(builder.name, builder.description, builder.unit)
builder.instrument_provider.u64_gauge(builder)
}
}

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

fn try_from(builder: InstrumentBuilder<'_, Gauge<f64>>) -> Result<Self, Self::Error> {
builder
.instrument_provider
.f64_gauge(builder.name, builder.description, builder.unit)
builder.instrument_provider.f64_gauge(builder)
}
}

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

fn try_from(builder: InstrumentBuilder<'_, Gauge<i64>>) -> Result<Self, Self::Error> {
builder
.instrument_provider
.i64_gauge(builder.name, builder.description, builder.unit)
builder.instrument_provider.i64_gauge(builder)
}
}

Expand Down
Loading

0 comments on commit 3347bde

Please sign in to comment.