Skip to content

Commit

Permalink
Merge branch 'main' into feat/otel_0.26_promo
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Oct 10, 2024
2 parents e91f7f2 + 78a684c commit 2710d6e
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 270 deletions.
6 changes: 1 addition & 5 deletions opentelemetry-sdk/src/metrics/instrument.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{any::Any, borrow::Cow, collections::HashSet, hash::Hash, sync::Arc};
use std::{borrow::Cow, collections::HashSet, sync::Arc};

use opentelemetry::{
metrics::{AsyncInstrument, SyncCounter, SyncGauge, SyncHistogram, SyncUpDownCounter},
Expand Down Expand Up @@ -301,8 +301,4 @@ impl<T: Copy + Send + Sync + 'static> AsyncInstrument<T> for Observable<T> {
measure.call(measurement, attrs)
}
}

fn as_any(&self) -> Arc<dyn Any> {
Arc::new(self.clone())
}
}
12 changes: 9 additions & 3 deletions opentelemetry-sdk/src/metrics/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl SdkMeter {

fn create_histogram<T>(
&self,
builder: HistogramBuilder<'_, T>,
builder: HistogramBuilder<'_, Histogram<T>>,
resolver: &InstrumentResolver<'_, T>,
) -> Result<Histogram<T>>
where
Expand Down Expand Up @@ -407,12 +407,18 @@ impl InstrumentProvider for SdkMeter {
self.create_observable_gauge(builder, &resolver)
}

fn f64_histogram(&self, builder: HistogramBuilder<'_, f64>) -> Result<Histogram<f64>> {
fn f64_histogram(
&self,
builder: HistogramBuilder<'_, Histogram<f64>>,
) -> Result<Histogram<f64>> {
let resolver = InstrumentResolver::new(self, &self.f64_resolver);
self.create_histogram(builder, &resolver)
}

fn u64_histogram(&self, builder: HistogramBuilder<'_, u64>) -> Result<Histogram<u64>> {
fn u64_histogram(
&self,
builder: HistogramBuilder<'_, Histogram<u64>>,
) -> Result<Histogram<u64>> {
let resolver = InstrumentResolver::new(self, &self.u64_resolver);
self.create_histogram(builder, &resolver)
}
Expand Down
1 change: 1 addition & 0 deletions opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Bump MSRV to 1.70 [#2179](https://github.com/open-telemetry/opentelemetry-rust/pull/2179)
- Add `LogRecord::set_trace_context`; an optional method conditional on the `trace` feature for setting trace context on a log record.
- Remove unnecessary public methods named `as_any` from `AsyncInstrument` trait and the implementing instruments: `ObservableCounter`, `ObservableGauge`, and `ObservableUpDownCounter` [#2187](https://github.com/open-telemetry/opentelemetry-rust/issues/2187)

## v0.26.0
Released 2024-Sep-30
Expand Down
64 changes: 1 addition & 63 deletions opentelemetry/src/metrics/instruments/counter.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
use crate::{
metrics::{AsyncInstrument, AsyncInstrumentBuilder, MetricsError},
KeyValue,
};
use crate::{metrics::AsyncInstrument, 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 @@ -39,22 +33,6 @@ impl<T> Counter<T> {
}
}

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)
}
}

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)
}
}

/// An async instrument that records increasing values.
#[derive(Clone)]
pub struct ObservableCounter<T>(Arc<dyn AsyncInstrument<T>>);
Expand All @@ -75,48 +53,8 @@ impl<T> fmt::Debug for ObservableCounter<T> {
}
}

impl<T> ObservableCounter<T> {
/// Records an increment to the counter.
///
/// It is only valid to call this within a callback. If called outside of the
/// registered callback it should have no effect on the instrument, and an
/// error will be reported via the error handler.
pub fn observe(&self, value: T, attributes: &[KeyValue]) {
self.0.observe(value, attributes)
}

/// Used for SDKs to downcast instruments in callbacks.
pub fn as_any(&self) -> Arc<dyn Any> {
self.0.as_any()
}
}

impl<T> AsyncInstrument<T> for ObservableCounter<T> {
fn observe(&self, measurement: T, attributes: &[KeyValue]) {
self.0.observe(measurement, attributes)
}

fn as_any(&self) -> Arc<dyn Any> {
self.0.as_any()
}
}

impl TryFrom<AsyncInstrumentBuilder<'_, ObservableCounter<u64>, u64>> for ObservableCounter<u64> {
type Error = MetricsError;

fn try_from(
builder: AsyncInstrumentBuilder<'_, ObservableCounter<u64>, u64>,
) -> Result<Self, Self::Error> {
builder.instrument_provider.u64_observable_counter(builder)
}
}

impl TryFrom<AsyncInstrumentBuilder<'_, ObservableCounter<f64>, f64>> for ObservableCounter<f64> {
type Error = MetricsError;

fn try_from(
builder: AsyncInstrumentBuilder<'_, ObservableCounter<f64>, f64>,
) -> Result<Self, Self::Error> {
builder.instrument_provider.f64_observable_counter(builder)
}
}
82 changes: 2 additions & 80 deletions opentelemetry/src/metrics/instruments/gauge.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::{
metrics::{AsyncInstrument, AsyncInstrumentBuilder, InstrumentBuilder, MetricsError},
KeyValue,
};
use crate::{metrics::AsyncInstrument, KeyValue};
use core::fmt;
use std::any::Any;
use std::sync::Arc;

/// An SDK implemented instrument that records independent values
Expand Down Expand Up @@ -37,30 +33,6 @@ impl<T> Gauge<T> {
}
}

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)
}
}

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)
}
}

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)
}
}

/// An async instrument that records independent readings.
#[derive(Clone)]
pub struct ObservableGauge<T>(Arc<dyn AsyncInstrument<T>>);
Expand All @@ -77,29 +49,9 @@ where
}
}

impl<T> ObservableGauge<T> {
/// Records the state of the instrument.
///
/// It is only valid to call this within a callback. If called outside of the
/// registered callback it should have no effect on the instrument, and an
/// error will be reported via the error handler.
pub fn observe(&self, measurement: T, attributes: &[KeyValue]) {
self.0.observe(measurement, attributes)
}

/// Used by SDKs to downcast instruments in callbacks.
pub fn as_any(&self) -> Arc<dyn Any> {
self.0.as_any()
}
}

impl<M> AsyncInstrument<M> for ObservableGauge<M> {
fn observe(&self, measurement: M, attributes: &[KeyValue]) {
self.observe(measurement, attributes)
}

fn as_any(&self) -> Arc<dyn Any> {
self.0.as_any()
self.0.observe(measurement, attributes)
}
}

Expand All @@ -109,33 +61,3 @@ impl<T> ObservableGauge<T> {
ObservableGauge(inner)
}
}

impl TryFrom<AsyncInstrumentBuilder<'_, ObservableGauge<u64>, u64>> for ObservableGauge<u64> {
type Error = MetricsError;

fn try_from(
builder: AsyncInstrumentBuilder<'_, ObservableGauge<u64>, u64>,
) -> Result<Self, Self::Error> {
builder.instrument_provider.u64_observable_gauge(builder)
}
}

impl TryFrom<AsyncInstrumentBuilder<'_, ObservableGauge<f64>, f64>> for ObservableGauge<f64> {
type Error = MetricsError;

fn try_from(
builder: AsyncInstrumentBuilder<'_, ObservableGauge<f64>, f64>,
) -> Result<Self, Self::Error> {
builder.instrument_provider.f64_observable_gauge(builder)
}
}

impl TryFrom<AsyncInstrumentBuilder<'_, ObservableGauge<i64>, i64>> for ObservableGauge<i64> {
type Error = MetricsError;

fn try_from(
builder: AsyncInstrumentBuilder<'_, ObservableGauge<i64>, i64>,
) -> Result<Self, Self::Error> {
builder.instrument_provider.i64_observable_gauge(builder)
}
}
Loading

0 comments on commit 2710d6e

Please sign in to comment.