Skip to content

refactor: AggregatedMetrics as enum instead of dyn Aggregation #2857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 23 additions & 42 deletions opentelemetry-proto/src/transform/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#[allow(deprecated)]
#[cfg(feature = "gen-tonic-messages")]
pub mod tonic {
use std::any::Any;
use std::fmt;
use std::fmt::Debug;

use opentelemetry::{otel_debug, Key, Value};
use opentelemetry_sdk::metrics::data::{
Exemplar as SdkExemplar, ExponentialHistogram as SdkExponentialHistogram,
Gauge as SdkGauge, Histogram as SdkHistogram, Metric as SdkMetric, ResourceMetrics,
AggregatedMetrics, Exemplar as SdkExemplar,
ExponentialHistogram as SdkExponentialHistogram, Gauge as SdkGauge,
Histogram as SdkHistogram, Metric as SdkMetric, MetricData, ResourceMetrics,
ScopeMetrics as SdkScopeMetrics, Sum as SdkSum,
};
use opentelemetry_sdk::metrics::Temporality;
Expand Down Expand Up @@ -152,46 +152,27 @@
description: metric.description.to_string(),
unit: metric.unit.to_string(),
metadata: vec![], // internal and currently unused
data: metric.data.as_any().try_into().ok(),
data: Some(match &metric.data {
AggregatedMetrics::F64(data) => data.into(),
AggregatedMetrics::U64(data) => data.into(),
AggregatedMetrics::I64(data) => data.into(),

Check warning on line 158 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L155-L158

Added lines #L155 - L158 were not covered by tests
}),
}
}
}

impl TryFrom<&dyn Any> for TonicMetricData {
type Error = ();

fn try_from(data: &dyn Any) -> Result<Self, Self::Error> {
if let Some(hist) = data.downcast_ref::<SdkHistogram<i64>>() {
Ok(TonicMetricData::Histogram(hist.into()))
} else if let Some(hist) = data.downcast_ref::<SdkHistogram<u64>>() {
Ok(TonicMetricData::Histogram(hist.into()))
} else if let Some(hist) = data.downcast_ref::<SdkHistogram<f64>>() {
Ok(TonicMetricData::Histogram(hist.into()))
} else if let Some(hist) = data.downcast_ref::<SdkExponentialHistogram<i64>>() {
Ok(TonicMetricData::ExponentialHistogram(hist.into()))
} else if let Some(hist) = data.downcast_ref::<SdkExponentialHistogram<u64>>() {
Ok(TonicMetricData::ExponentialHistogram(hist.into()))
} else if let Some(hist) = data.downcast_ref::<SdkExponentialHistogram<f64>>() {
Ok(TonicMetricData::ExponentialHistogram(hist.into()))
} else if let Some(sum) = data.downcast_ref::<SdkSum<u64>>() {
Ok(TonicMetricData::Sum(sum.into()))
} else if let Some(sum) = data.downcast_ref::<SdkSum<i64>>() {
Ok(TonicMetricData::Sum(sum.into()))
} else if let Some(sum) = data.downcast_ref::<SdkSum<f64>>() {
Ok(TonicMetricData::Sum(sum.into()))
} else if let Some(gauge) = data.downcast_ref::<SdkGauge<u64>>() {
Ok(TonicMetricData::Gauge(gauge.into()))
} else if let Some(gauge) = data.downcast_ref::<SdkGauge<i64>>() {
Ok(TonicMetricData::Gauge(gauge.into()))
} else if let Some(gauge) = data.downcast_ref::<SdkGauge<f64>>() {
Ok(TonicMetricData::Gauge(gauge.into()))
} else {
otel_debug!(
name: "TonicMetricData::UnknownAggregator",
message= "Unknown aggregator type",
unknown_type= format!("{:?}", data),
);
Err(())
impl<T> From<&MetricData<T>> for TonicMetricData
where
T: Numeric + Debug,
{
fn from(data: &MetricData<T>) -> Self {
match data {
MetricData::Gauge(gauge) => TonicMetricData::Gauge(gauge.into()),
MetricData::Sum(sum) => TonicMetricData::Sum(sum.into()),
MetricData::Histogram(hist) => TonicMetricData::Histogram(hist.into()),
MetricData::ExponentialHistogram(hist) => {
TonicMetricData::ExponentialHistogram(hist.into())

Check warning on line 174 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L168-L174

Added lines #L168 - L174 were not covered by tests
}
}
}
}
Expand Down Expand Up @@ -286,7 +267,7 @@

impl<T> From<&SdkSum<T>> for TonicSum
where
T: fmt::Debug + Into<TonicExemplarValue> + Into<TonicDataPointValue> + Copy,
T: Debug + Into<TonicExemplarValue> + Into<TonicDataPointValue> + Copy,
{
fn from(sum: &SdkSum<T>) -> Self {
TonicSum {
Expand All @@ -310,7 +291,7 @@

impl<T> From<&SdkGauge<T>> for TonicGauge
where
T: fmt::Debug + Into<TonicExemplarValue> + Into<TonicDataPointValue> + Copy,
T: Debug + Into<TonicExemplarValue> + Into<TonicDataPointValue> + Copy,
{
fn from(gauge: &SdkGauge<T>) -> Self {
TonicGauge {
Expand Down
Loading