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

refactor: AggregatedMetrics as enum instead of dyn Aggregation #2857

Merged
merged 3 commits into from
Mar 26, 2025

Conversation

fraillt
Copy link
Contributor

@fraillt fraillt commented Mar 24, 2025

Changes

  • Introduced a new AggregatedMetrics enum, which store data reported by Instruments.
  • Change Metric::data field from Box<dyn Aggregation> to AggregatedMetrics.
  • ResourceMetrics is now Clone, because AggregatedMetrics is a simple enum that can be easily cloned.

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@fraillt fraillt requested a review from a team as a code owner March 24, 2025 20:02
@fraillt fraillt force-pushed the aggregated-metrics branch from 0b88d1f to 3692f9b Compare March 24, 2025 20:15
Copy link

codecov bot commented Mar 24, 2025

Codecov Report

Attention: Patch coverage is 71.90570% with 143 lines in your changes missing coverage. Please review.

Project coverage is 80.7%. Comparing base (f12833f) to head (e849cd7).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...-sdk/src/metrics/internal/exponential_histogram.rs 55.8% 38 Missing ⚠️
opentelemetry-sdk/src/metrics/mod.rs 83.0% 31 Missing ⚠️
opentelemetry-stdout/src/metrics/exporter.rs 0.0% 25 Missing ⚠️
opentelemetry-proto/src/transform/metrics.rs 0.0% 11 Missing ⚠️
opentelemetry-sdk/src/metrics/data/mod.rs 57.1% 9 Missing ⚠️
...pentelemetry-sdk/src/metrics/in_memory_exporter.rs 81.0% 7 Missing ⚠️
opentelemetry-sdk/src/metrics/internal/mod.rs 84.6% 6 Missing ⚠️
...pentelemetry-sdk/src/metrics/internal/aggregate.rs 76.1% 5 Missing ⚠️
...entelemetry-sdk/src/metrics/internal/last_value.rs 80.9% 4 Missing ⚠️
...pentelemetry-sdk/src/metrics/internal/histogram.rs 86.3% 3 Missing ⚠️
... and 2 more
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #2857     +/-   ##
=======================================
- Coverage   80.9%   80.7%   -0.2%     
=======================================
  Files        124     124             
  Lines      23726   23703     -23     
=======================================
- Hits       19201   19146     -55     
- Misses      4525    4557     +32     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

/// Support downcasting during aggregation
fn as_mut(&mut self) -> &mut dyn any::Any;
/// Aggregated metrics data from an instrument
#[derive(Debug, Clone)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a user's perspective, it's more convenient to have a MetricData<T>, because this allows almost all code to be generic over metric data.

fn clone_inner<T: Clone>(data: &MetricData<T>) -> MetricData<T> {
    match data {
        MetricData::Gauge(gauge) => Gauge {
            data_points: gauge.data_points.clone(),
            start_time: gauge.start_time,
            time: gauge.time,
        }
        .into(),
        MetricData::Sum(sum) => Sum {
            data_points: sum.data_points.clone(),
            start_time: sum.start_time,
            time: sum.time,
            temporality: sum.temporality,
            is_monotonic: sum.is_monotonic,
        }
        .into(),
        MetricData::Histogram(histogram) => Histogram {
            data_points: histogram.data_points.clone(),
            start_time: histogram.start_time,
            time: histogram.time,
            temporality: histogram.temporality,
        }
        .into(),
        MetricData::ExponentialHistogram(exponential_histogram) => ExponentialHistogram {
            data_points: exponential_histogram.data_points.clone(),
            start_time: exponential_histogram.start_time,
            time: exponential_histogram.time,
            temporality: exponential_histogram.temporality,
        }
        .into(),
    }
}
match data {
    AggregatedMetrics::F64(metric_data) => AggregatedMetrics::F64(clone_inner(metric_data)),
    AggregatedMetrics::U64(metric_data) => AggregatedMetrics::U64(clone_inner(metric_data)),
    AggregatedMetrics::I64(metric_data) => AggregatedMetrics::I64(clone_inner(metric_data)),
}

Copy link
Member

@cijothomas cijothomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. We can followup on the open comments (Moving resource out, removing i64 in aggregation etc.)

@cijothomas cijothomas merged commit f3e93a0 into open-telemetry:main Mar 26, 2025
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants