Skip to content
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

Simplify metric::data imports #2536

Merged
merged 4 commits into from
Jan 23, 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
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 @@
#[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 @@
#[cfg(feature = "metrics")]
fn build_metrics_export_body(
&self,
metrics: &mut opentelemetry_sdk::metrics::data::ResourceMetrics,
metrics: &mut ResourceMetrics,

Check warning on line 342 in opentelemetry-otlp/src/exporter/http/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/http/mod.rs#L342

Added line #L342 was not covered by tests
) -> 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 @@

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

impl From<&data::ResourceMetrics> for ExportMetricsServiceRequest {
fn from(rm: &data::ResourceMetrics) -> Self {
impl From<&ResourceMetrics> for ExportMetricsServiceRequest {
fn from(rm: &ResourceMetrics) -> Self {

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

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L114

Added line #L114 was not covered by tests
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 std::sync::OnceLock;

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

Expand Down Expand Up @@ -386,7 +386,7 @@
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 @@
) -> (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 @@
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 @@
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();

Check warning on line 1473 in opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs#L1473

Added line #L1473 was not covered by tests
assert_eq!(
a.data_points.len(),
b.data_points.len(),
Expand All @@ -1479,8 +1480,8 @@
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();

Check warning on line 1484 in opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs#L1484

Added line #L1484 was not covered by tests
assert_eq!(
a.temporality, b.temporality,
"{} mismatching sum temporality",
Expand All @@ -1500,8 +1501,8 @@
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();

Check warning on line 1505 in opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs#L1505

Added line #L1505 was not covered by tests
assert_eq!(
a.temporality, b.temporality,
"{}: mismatching hist temporality",
Expand All @@ -1516,10 +1517,10 @@
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
Loading