Skip to content

Commit

Permalink
Simplify metric::data imports (#2536)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Jan 23, 2025
1 parent 57d1297 commit 3042aa0
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 100 deletions.
5 changes: 4 additions & 1 deletion opentelemetry-otlp/src/exporter/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use std::time::Duration;
#[cfg(feature = "metrics")]
mod metrics;

#[cfg(feature = "metrics")]
use opentelemetry_sdk::metrics::data::ResourceMetrics;

#[cfg(feature = "logs")]
pub(crate) mod logs;

Expand Down Expand Up @@ -336,7 +339,7 @@ impl OtlpHttpClient {
#[cfg(feature = "metrics")]
fn build_metrics_export_body(
&self,
metrics: &mut opentelemetry_sdk::metrics::data::ResourceMetrics,
metrics: &mut ResourceMetrics,
) -> opentelemetry_sdk::metrics::MetricResult<(Vec<u8>, &'static str)> {
use opentelemetry_proto::tonic::collector::metrics::v1::ExportMetricsServiceRequest;

Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-proto/src/transform/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub mod tonic {

use opentelemetry::{otel_debug, Key, Value};
use opentelemetry_sdk::metrics::data::{
self, Exemplar as SdkExemplar, ExponentialHistogram as SdkExponentialHistogram,
Gauge as SdkGauge, Histogram as SdkHistogram, Metric as SdkMetric,
Exemplar as SdkExemplar, ExponentialHistogram as SdkExponentialHistogram,
Gauge as SdkGauge, Histogram as SdkHistogram, Metric as SdkMetric, ResourceMetrics,
ScopeMetrics as SdkScopeMetrics, Sum as SdkSum,
};
use opentelemetry_sdk::metrics::Temporality;
Expand Down Expand Up @@ -110,8 +110,8 @@ pub mod tonic {
}
}

impl From<&data::ResourceMetrics> for ExportMetricsServiceRequest {
fn from(rm: &data::ResourceMetrics) -> Self {
impl From<&ResourceMetrics> for ExportMetricsServiceRequest {
fn from(rm: &ResourceMetrics) -> Self {
ExportMetricsServiceRequest {
resource_metrics: vec![TonicResourceMetrics {
resource: Some((&rm.resource).into()),
Expand Down
23 changes: 12 additions & 11 deletions opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use opentelemetry::{otel_debug, KeyValue};
use std::sync::OnceLock;

use crate::metrics::{
data::{self, Aggregation},
data::{self, Aggregation, ExponentialHistogram},
Temporality,
};

Expand Down Expand Up @@ -386,7 +386,7 @@ impl<T: Number> ExpoHistogram<T> {
fn delta(&self, dest: Option<&mut dyn Aggregation>) -> (usize, Option<Box<dyn Aggregation>>) {
let time = self.init_time.delta();

let h = dest.and_then(|d| d.as_mut().downcast_mut::<data::ExponentialHistogram<T>>());
let h = dest.and_then(|d| d.as_mut().downcast_mut::<ExponentialHistogram<T>>());
let mut new_agg = if h.is_none() {
Some(data::ExponentialHistogram {
data_points: vec![],
Expand Down Expand Up @@ -443,7 +443,7 @@ impl<T: Number> ExpoHistogram<T> {
) -> (usize, Option<Box<dyn Aggregation>>) {
let time = self.init_time.cumulative();

let h = dest.and_then(|d| d.as_mut().downcast_mut::<data::ExponentialHistogram<T>>());
let h = dest.and_then(|d| d.as_mut().downcast_mut::<ExponentialHistogram<T>>());
let mut new_agg = if h.is_none() {
Some(data::ExponentialHistogram {
data_points: vec![],
Expand Down Expand Up @@ -528,6 +528,7 @@ where
mod tests {
use std::{ops::Neg, time::SystemTime};

use data::{ExponentialHistogram, Gauge, Histogram, Sum};
use tests::internal::AggregateFns;

use crate::metrics::internal::{self, AggregateBuilder};
Expand Down Expand Up @@ -1468,8 +1469,8 @@ mod tests {
test_name
);

if let Some(a) = a.as_any().downcast_ref::<data::Gauge<T>>() {
let b = b.as_any().downcast_ref::<data::Gauge<T>>().unwrap();
if let Some(a) = a.as_any().downcast_ref::<Gauge<T>>() {
let b = b.as_any().downcast_ref::<Gauge<T>>().unwrap();
assert_eq!(
a.data_points.len(),
b.data_points.len(),
Expand All @@ -1479,8 +1480,8 @@ mod tests {
for (a, b) in a.data_points.iter().zip(b.data_points.iter()) {
assert_gauge_data_points_eq(a, b, "mismatching gauge data points", test_name);
}
} else if let Some(a) = a.as_any().downcast_ref::<data::Sum<T>>() {
let b = b.as_any().downcast_ref::<data::Sum<T>>().unwrap();
} else if let Some(a) = a.as_any().downcast_ref::<Sum<T>>() {
let b = b.as_any().downcast_ref::<Sum<T>>().unwrap();
assert_eq!(
a.temporality, b.temporality,
"{} mismatching sum temporality",
Expand All @@ -1500,8 +1501,8 @@ mod tests {
for (a, b) in a.data_points.iter().zip(b.data_points.iter()) {
assert_sum_data_points_eq(a, b, "mismatching sum data points", test_name);
}
} else if let Some(a) = a.as_any().downcast_ref::<data::Histogram<T>>() {
let b = b.as_any().downcast_ref::<data::Histogram<T>>().unwrap();
} else if let Some(a) = a.as_any().downcast_ref::<Histogram<T>>() {
let b = b.as_any().downcast_ref::<Histogram<T>>().unwrap();
assert_eq!(
a.temporality, b.temporality,
"{}: mismatching hist temporality",
Expand All @@ -1516,10 +1517,10 @@ mod tests {
for (a, b) in a.data_points.iter().zip(b.data_points.iter()) {
assert_hist_data_points_eq(a, b, "mismatching hist data points", test_name);
}
} else if let Some(a) = a.as_any().downcast_ref::<data::ExponentialHistogram<T>>() {
} else if let Some(a) = a.as_any().downcast_ref::<ExponentialHistogram<T>>() {
let b = b
.as_any()
.downcast_ref::<data::ExponentialHistogram<T>>()
.downcast_ref::<ExponentialHistogram<T>>()
.unwrap();
assert_eq!(
a.temporality, b.temporality,
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-sdk/src/metrics/internal/last_value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::metrics::{
data::{self, Aggregation, GaugeDataPoint},
data::{self, Aggregation, Gauge, GaugeDataPoint},
Temporality,
};
use opentelemetry::KeyValue;
Expand Down Expand Up @@ -65,7 +65,7 @@ impl<T: Number> LastValue<T> {
) -> (usize, Option<Box<dyn Aggregation>>) {
let time = self.init_time.delta();

let s_data = dest.and_then(|d| d.as_mut().downcast_mut::<data::Gauge<T>>());
let s_data = dest.and_then(|d| d.as_mut().downcast_mut::<Gauge<T>>());
let mut new_agg = if s_data.is_none() {
Some(data::Gauge {
data_points: vec![],
Expand Down Expand Up @@ -97,7 +97,7 @@ impl<T: Number> LastValue<T> {
dest: Option<&mut dyn Aggregation>,
) -> (usize, Option<Box<dyn Aggregation>>) {
let time = self.init_time.cumulative();
let s_data = dest.and_then(|d| d.as_mut().downcast_mut::<data::Gauge<T>>());
let s_data = dest.and_then(|d| d.as_mut().downcast_mut::<Gauge<T>>());
let mut new_agg = if s_data.is_none() {
Some(data::Gauge {
data_points: vec![],
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-sdk/src/metrics/internal/precomputed_sum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use opentelemetry::KeyValue;

use crate::metrics::data::{self, Aggregation, SumDataPoint};
use crate::metrics::data::{self, Aggregation, Sum, SumDataPoint};
use crate::metrics::Temporality;

use super::aggregate::{AggregateTimeInitiator, AttributeSetFilter};
Expand Down Expand Up @@ -40,7 +40,7 @@ impl<T: Number> PrecomputedSum<T> {
) -> (usize, Option<Box<dyn Aggregation>>) {
let time = self.init_time.delta();

let s_data = dest.and_then(|d| d.as_mut().downcast_mut::<data::Sum<T>>());
let s_data = dest.and_then(|d| d.as_mut().downcast_mut::<Sum<T>>());
let mut new_agg = if s_data.is_none() {
Some(data::Sum {
data_points: vec![],
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<T: Number> PrecomputedSum<T> {
) -> (usize, Option<Box<dyn Aggregation>>) {
let time = self.init_time.cumulative();

let s_data = dest.and_then(|d| d.as_mut().downcast_mut::<data::Sum<T>>());
let s_data = dest.and_then(|d| d.as_mut().downcast_mut::<Sum<T>>());
let mut new_agg = if s_data.is_none() {
Some(data::Sum {
data_points: vec![],
Expand Down
Loading

0 comments on commit 3042aa0

Please sign in to comment.