-
Notifications
You must be signed in to change notification settings - Fork 502
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
Metrics aggregate collector generic over temporality #2506
Open
fraillt
wants to merge
2
commits into
open-telemetry:main
Choose a base branch
from
fraillt:generic-metrics-collector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
use opentelemetry::KeyValue; | ||
|
||
use crate::metrics::{ | ||
data::{Aggregation, AggregationDataPoints}, | ||
Temporality, | ||
}; | ||
|
||
use super::{ | ||
aggregate::{AggregateTime, AttributeSetFilter}, | ||
AggregateTimeInitiator, Aggregator, InitAggregationData, ValueMap, | ||
}; | ||
|
||
/// Aggregate measurements for attribute sets and collect these aggregates into data points for specific temporality | ||
pub(crate) trait AggregateMap: Send + Sync + 'static { | ||
const TEMPORALITY: Temporality; | ||
type Aggr: Aggregator; | ||
|
||
fn measure(&self, value: <Self::Aggr as Aggregator>::PreComputedValue, attributes: &[KeyValue]); | ||
|
||
fn collect_data_points<DP, MapFn>(&self, dest: &mut Vec<DP>, map_fn: MapFn) | ||
where | ||
MapFn: FnMut(Vec<KeyValue>, &Self::Aggr) -> DP; | ||
} | ||
|
||
/// Higher level abstraction (compared to [`AggregateMap`]) that also does the filtering and collection into aggregation data | ||
pub(crate) trait AggregateCollector: Send + Sync + 'static { | ||
const TEMPORALITY: Temporality; | ||
type Aggr: Aggregator; | ||
|
||
fn measure(&self, value: <Self::Aggr as Aggregator>::PreComputedValue, attributes: &[KeyValue]); | ||
|
||
fn collect<InitAggregate, F>( | ||
&self, | ||
aggregate: &InitAggregate, | ||
dest: Option<&mut dyn Aggregation>, | ||
create_point: F, | ||
) -> (usize, Option<Box<dyn Aggregation>>) | ||
where | ||
InitAggregate: InitAggregationData, | ||
F: FnMut( | ||
Vec<KeyValue>, | ||
&Self::Aggr, | ||
) -> <InitAggregate::Aggr as AggregationDataPoints>::DataPoint; | ||
} | ||
|
||
pub(crate) struct Collector<AM> { | ||
filter: AttributeSetFilter, | ||
aggregate_map: AM, | ||
time: AggregateTimeInitiator, | ||
} | ||
|
||
impl<AM> Collector<AM> | ||
where | ||
AM: AggregateMap, | ||
{ | ||
pub(crate) fn new(filter: AttributeSetFilter, aggregate_map: AM) -> Self { | ||
Self { | ||
filter, | ||
aggregate_map, | ||
time: AggregateTimeInitiator::default(), | ||
} | ||
} | ||
|
||
fn init_time(&self) -> AggregateTime { | ||
if let Temporality::Delta = AM::TEMPORALITY { | ||
self.time.delta() | ||
} else { | ||
self.time.cumulative() | ||
} | ||
} | ||
} | ||
|
||
impl<AM> AggregateCollector for Collector<AM> | ||
where | ||
AM: AggregateMap, | ||
{ | ||
const TEMPORALITY: Temporality = AM::TEMPORALITY; | ||
|
||
type Aggr = AM::Aggr; | ||
|
||
fn measure(&self, value: <AM::Aggr as Aggregator>::PreComputedValue, attributes: &[KeyValue]) { | ||
self.filter.apply(attributes, |filtered_attrs| { | ||
self.aggregate_map.measure(value, filtered_attrs); | ||
}); | ||
} | ||
|
||
fn collect<InitAggregate, F>( | ||
&self, | ||
aggregate: &InitAggregate, | ||
dest: Option<&mut dyn Aggregation>, | ||
create_point: F, | ||
) -> (usize, Option<Box<dyn Aggregation>>) | ||
where | ||
InitAggregate: InitAggregationData, | ||
F: FnMut( | ||
Vec<KeyValue>, | ||
&AM::Aggr, | ||
) -> <InitAggregate::Aggr as AggregationDataPoints>::DataPoint, | ||
{ | ||
let time = self.init_time(); | ||
let s_data = dest.and_then(|d| d.as_mut().downcast_mut::<InitAggregate::Aggr>()); | ||
let mut new_agg = if s_data.is_none() { | ||
Some(aggregate.create_new(time)) | ||
} else { | ||
None | ||
}; | ||
let s_data = s_data.unwrap_or_else(|| new_agg.as_mut().expect("present if s_data is none")); | ||
aggregate.reset_existing(s_data, time); | ||
self.aggregate_map | ||
.collect_data_points(s_data.points(), create_point); | ||
|
||
( | ||
s_data.points().len(), | ||
new_agg.map(|a| Box::new(a) as Box<_>), | ||
) | ||
} | ||
} | ||
|
||
/// At the moment use [`ValueMap`] under the hood (which support both Delta and Cumulative), to implement `AggregateMap` for Delta temporality | ||
/// Later this could be improved to support only Delta temporality | ||
pub(crate) struct DeltaValueMap<A>(ValueMap<A>) | ||
where | ||
A: Aggregator; | ||
|
||
impl<A> DeltaValueMap<A> | ||
where | ||
A: Aggregator, | ||
{ | ||
pub(crate) fn new(config: A::InitConfig) -> Self { | ||
Self(ValueMap::new(config)) | ||
} | ||
} | ||
|
||
impl<A> AggregateMap for DeltaValueMap<A> | ||
where | ||
A: Aggregator, | ||
<A as Aggregator>::InitConfig: Send + Sync, | ||
{ | ||
const TEMPORALITY: Temporality = Temporality::Delta; | ||
|
||
type Aggr = A; | ||
|
||
fn measure( | ||
&self, | ||
value: <Self::Aggr as Aggregator>::PreComputedValue, | ||
attributes: &[KeyValue], | ||
) { | ||
self.0.measure(value, attributes); | ||
} | ||
|
||
fn collect_data_points<DP, MapFn>(&self, dest: &mut Vec<DP>, mut map_fn: MapFn) | ||
where | ||
MapFn: FnMut(Vec<KeyValue>, &Self::Aggr) -> DP, | ||
{ | ||
self.0 | ||
.collect_and_reset(dest, |attributes, aggr| map_fn(attributes, &aggr)); | ||
} | ||
} | ||
|
||
/// At the moment use [`ValueMap`] under the hood (which support both Delta and Cumulative), to implement `AggregateMap` for Cumulative temporality | ||
/// Later this could be improved to support only Cumulative temporality | ||
pub(crate) struct CumulativeValueMap<A>(ValueMap<A>) | ||
where | ||
A: Aggregator; | ||
|
||
impl<A> CumulativeValueMap<A> | ||
where | ||
A: Aggregator, | ||
{ | ||
pub(crate) fn new(config: A::InitConfig) -> Self { | ||
Self(ValueMap::new(config)) | ||
} | ||
} | ||
|
||
impl<A> AggregateMap for CumulativeValueMap<A> | ||
where | ||
A: Aggregator, | ||
<A as Aggregator>::InitConfig: Send + Sync, | ||
{ | ||
const TEMPORALITY: Temporality = Temporality::Cumulative; | ||
|
||
type Aggr = A; | ||
|
||
fn measure( | ||
&self, | ||
value: <Self::Aggr as Aggregator>::PreComputedValue, | ||
attributes: &[KeyValue], | ||
) { | ||
self.0.measure(value, attributes); | ||
} | ||
|
||
fn collect_data_points<DP, MapFn>(&self, dest: &mut Vec<DP>, map_fn: MapFn) | ||
where | ||
MapFn: FnMut(Vec<KeyValue>, &Self::Aggr) -> DP, | ||
{ | ||
self.0.collect_readonly(dest, map_fn); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's getting a bit too confusing with these new coupled traits. Could we separate the concerns here? Could we update
Collector
trait to only have collect related methods andAggregateMap
trait to only have update related methods? You could then keep animpl
of both the traits as fields ofExpoHistogram
struct.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's possible to have this separation, basically this whole things is like an onion.
Starting from the center:
trait AggregateMap
- implement the core data structure optimized for updating AND collecting aggregates for specific temporality (e.g.DeltaValueMap
, andCumulativeValueMap
)trait AggregateCollector
- provides filtering attributes AND collecting impl specificDataPoints
intodyn Aggregation
(including time initialization for specific temporality). There's only one implementation, -Collector
. The reason we have this trait, is becauseCollector
itself is also generic (currently only overtrait AggregateMap
, but it might also be generic over "aggregate-filter"), which makes trait bounds for implementations to be very verbose). Important property of this implementation is that it has all common code for aggregations.Measure
andComputeAggregation
traits, which are used by SDK. Also implementsInitAggregationData
which is used byAggregateCollector
to make whole collection phase reusable.The thing that I like about this is that 1) and 2) (e.g. impls for
trait AggregateMap
andtrait AggregateCollector
) is common code for absolutely all aggregates. The only aggregate specific logic is in three traits:Measure
,ComputeAggregation
,InitAggregationData
.Regarding splitting...:
trait AggregateMap
cannot be split, because it's implementation has optimized internal structure that only it knows how to update and collecttrait AggregateCollector
depends ontrait AggregateMap
, it also cannot be split into few instances.Histogram::bounds
field), so splitting concrete implementation not optimal as well.AggregateFns
implementation is probably most efficient way to do it. (e.g. Arc and clone, so one instance implements Measure,- anotherComputeAggregation
).BTW, I agree that this gets a bit confusing, but I think better naming would solve the problem here... but that's the hardest part :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mm... maybe I have one more idea.
Instead of having specific aggregation "the final thing" (which implements
Measure
andComputeAggregation
). I could make thatCollector
is the final thing, and specific aggregations would have a trait which will be used to provide specific functionality...I will probably create separate revision for that, (so we could compare both at the same time).
Ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#2530 Same thing,- new design.
IMO it turned out very good, I like this new design much more.
If you have same opinion, then we can close this PR.