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

New InitAggregationData abstraction/trait #2500

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fraillt
Copy link
Contributor

@fraillt fraillt commented Jan 7, 2025

Changes

  • Added new trait InitAggregationData, along with few helper types, which allow to cleanly define logic how aggregation is initialized. E.g.
impl<T> InitAggregationData for Sum<T>
where
    T: Number,
{
    type Aggr = data::Sum<T>;

    fn create_new(&self, time: AggregateTime) -> Self::Aggr {
        data::Sum {
            data_points: vec![],
            start_time: time.start,
            time: time.current,
            temporality: self.temporality,
            is_monotonic: self.monotonic,
        }
    }

    fn reset_existing(&self, existing: &mut Self::Aggr, time: AggregateTime) {
        existing.data_points.clear();
        existing.start_time = time.start;
        existing.time = time.current;
        existing.temporality = self.temporality;
        existing.is_monotonic = self.monotonic;
    }
}
  • Using this implementation, we can significantly simplify code in collect functions (in 10 places). E.g.
-        let time = self.init_time.cumulative();
-        let s_data = dest.and_then(|d| d.as_mut().downcast_mut::<data::Sum<T>>());
-        let mut new_agg = if s_data.is_none() {
-            Some(data::Sum {
-                data_points: vec![],
-                start_time: time.start,
-                time: time.current,
-                temporality: Temporality::Cumulative,
-                is_monotonic: self.monotonic,
-            })
-        } else {
-            None
-        };
-        let s_data = s_data.unwrap_or_else(|| new_agg.as_mut().expect("present if s_data is none"));
-
-        s_data.start_time = time.start;
-        s_data.time = time.current;
-        s_data.temporality = Temporality::Cumulative;
-        s_data.is_monotonic = self.monotonic;
+        let mut s_data = AggregationData::init(self, dest, self.init_time.cumulative());

This could be expanded further to also construct "data points", which might make code simper once we introduce ability to change "value map" implementations (for different temporality in #2328).

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 January 7, 2025 20:05
Copy link

codecov bot commented Jan 7, 2025

Codecov Report

Attention: Patch coverage is 93.79845% with 8 lines in your changes missing coverage. Please review.

Project coverage is 77.8%. Comparing base (a1dda22) to head (e5d3549).

Files with missing lines Patch % Lines
...-sdk/src/metrics/internal/exponential_histogram.rs 60.0% 8 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #2500     +/-   ##
=======================================
- Coverage   77.9%   77.8%   -0.1%     
=======================================
  Files        123     123             
  Lines      22888   22818     -70     
=======================================
- Hits       17839   17774     -65     
+ Misses      5049    5044      -5     

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

@fraillt fraillt force-pushed the init-aggregation-data branch from 468af54 to e5d3549 Compare January 8, 2025 06:44
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.

1 participant