|
| 1 | +use std::{marker::PhantomData, sync::Arc}; |
| 2 | + |
| 3 | +use opentelemetry::KeyValue; |
| 4 | + |
| 5 | +use crate::metrics::{ |
| 6 | + data::{Aggregation, AggregationDataPoints}, |
| 7 | + Temporality, |
| 8 | +}; |
| 9 | + |
| 10 | +use super::{ |
| 11 | + aggregate::{AggregateTime, AttributeSetFilter}, |
| 12 | + AggregateFns, AggregateTimeInitiator, Aggregator, ComputeAggregation, Measure, Number, |
| 13 | + ValueMap, |
| 14 | +}; |
| 15 | + |
| 16 | +/// Aggregate measurements for attribute sets and collect these aggregates into data points for specific temporality |
| 17 | +pub(crate) trait AggregateMap: Send + Sync + 'static { |
| 18 | + const TEMPORALITY: Temporality; |
| 19 | + type Aggr: Aggregator; |
| 20 | + |
| 21 | + fn measure(&self, value: <Self::Aggr as Aggregator>::PreComputedValue, attributes: &[KeyValue]); |
| 22 | + |
| 23 | + fn collect_data_points<DP, MapFn>(&self, dest: &mut Vec<DP>, map_fn: MapFn) |
| 24 | + where |
| 25 | + MapFn: FnMut(Vec<KeyValue>, &Self::Aggr) -> DP; |
| 26 | +} |
| 27 | + |
| 28 | +/// This trait provides aggregation specific functionality |
| 29 | +pub(crate) trait AggregationImpl<T>: Send + Sync + 'static { |
| 30 | + // an implementation that knows how to aggregate a measurement |
| 31 | + type Aggr: Aggregator; |
| 32 | + // an implementation that stores collected aggregation data |
| 33 | + type AggrData: Aggregation + AggregationDataPoints; |
| 34 | + |
| 35 | + fn precompute(&self, value: T) -> <Self::Aggr as Aggregator>::PreComputedValue; |
| 36 | + fn new_aggregation_data(&self, temporality: Temporality, time: AggregateTime) |
| 37 | + -> Self::AggrData; |
| 38 | + fn reset_aggregation_data( |
| 39 | + &self, |
| 40 | + existing: &mut Self::AggrData, |
| 41 | + temporality: Temporality, |
| 42 | + time: AggregateTime, |
| 43 | + ); |
| 44 | + fn build_create_points_fn( |
| 45 | + &self, |
| 46 | + ) -> impl FnMut(Vec<KeyValue>, &Self::Aggr) -> <Self::AggrData as AggregationDataPoints>::DataPoint; |
| 47 | +} |
| 48 | + |
| 49 | +pub(crate) fn create_aggregation<A, AM, T>( |
| 50 | + aggregation: A, |
| 51 | + aggregate_map: AM, |
| 52 | + filter: AttributeSetFilter, |
| 53 | +) -> AggregateFns<T> |
| 54 | +where |
| 55 | + AM: AggregateMap, |
| 56 | + A: AggregationImpl<T, Aggr = AM::Aggr>, |
| 57 | + T: Number, |
| 58 | +{ |
| 59 | + let fns = Arc::new(AggregionFnsImpl { |
| 60 | + filter, |
| 61 | + aggregation, |
| 62 | + aggregate_map, |
| 63 | + time: AggregateTimeInitiator::default(), |
| 64 | + _marker: Default::default(), |
| 65 | + }); |
| 66 | + AggregateFns { |
| 67 | + collect: fns.clone(), |
| 68 | + measure: fns, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +struct AggregionFnsImpl<A, AM, T> { |
| 73 | + filter: AttributeSetFilter, |
| 74 | + aggregation: A, |
| 75 | + aggregate_map: AM, |
| 76 | + time: AggregateTimeInitiator, |
| 77 | + _marker: PhantomData<T>, |
| 78 | +} |
| 79 | + |
| 80 | +impl<A, AM, T> Measure<T> for AggregionFnsImpl<A, AM, T> |
| 81 | +where |
| 82 | + A: AggregationImpl<T>, |
| 83 | + AM: AggregateMap<Aggr = A::Aggr>, |
| 84 | + T: Number, |
| 85 | +{ |
| 86 | + fn call(&self, measurement: T, attrs: &[KeyValue]) { |
| 87 | + self.filter.apply(attrs, |filtered_attrs| { |
| 88 | + let precomputed = self.aggregation.precompute(measurement); |
| 89 | + self.aggregate_map.measure(precomputed, filtered_attrs); |
| 90 | + }); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<A, AM, T> ComputeAggregation for AggregionFnsImpl<A, AM, T> |
| 95 | +where |
| 96 | + A: AggregationImpl<T>, |
| 97 | + AM: AggregateMap<Aggr = A::Aggr>, |
| 98 | + T: Number, |
| 99 | +{ |
| 100 | + fn call(&self, dest: Option<&mut dyn Aggregation>) -> (usize, Option<Box<dyn Aggregation>>) { |
| 101 | + let time = if let Temporality::Delta = AM::TEMPORALITY { |
| 102 | + self.time.delta() |
| 103 | + } else { |
| 104 | + self.time.cumulative() |
| 105 | + }; |
| 106 | + let mut s_data = dest.and_then(|d| d.as_mut().downcast_mut::<A::AggrData>()); |
| 107 | + let mut new_agg = match s_data.as_mut() { |
| 108 | + Some(existing) => { |
| 109 | + self.aggregation |
| 110 | + .reset_aggregation_data(existing, AM::TEMPORALITY, time); |
| 111 | + None |
| 112 | + } |
| 113 | + None => Some(self.aggregation.new_aggregation_data(AM::TEMPORALITY, time)), |
| 114 | + }; |
| 115 | + let s_data = s_data.unwrap_or_else(|| new_agg.as_mut().expect("present if s_data is none")); |
| 116 | + |
| 117 | + let create_points_fn = self.aggregation.build_create_points_fn(); |
| 118 | + self.aggregate_map |
| 119 | + .collect_data_points(s_data.points(), create_points_fn); |
| 120 | + |
| 121 | + ( |
| 122 | + s_data.points().len(), |
| 123 | + new_agg.map(|a| Box::new(a) as Box<dyn Aggregation>), |
| 124 | + ) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/// At the moment use [`ValueMap`] under the hood (which support both Delta and Cumulative), to implement `AggregateMap` for Delta temporality |
| 129 | +/// Later this could be improved to support only Delta temporality |
| 130 | +pub(crate) struct DeltaValueMap<A>(ValueMap<A>) |
| 131 | +where |
| 132 | + A: Aggregator; |
| 133 | + |
| 134 | +impl<A> DeltaValueMap<A> |
| 135 | +where |
| 136 | + A: Aggregator, |
| 137 | +{ |
| 138 | + pub(crate) fn new(config: A::InitConfig) -> Self { |
| 139 | + Self(ValueMap::new(config)) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +impl<A> AggregateMap for DeltaValueMap<A> |
| 144 | +where |
| 145 | + A: Aggregator, |
| 146 | + <A as Aggregator>::InitConfig: Send + Sync, |
| 147 | +{ |
| 148 | + const TEMPORALITY: Temporality = Temporality::Delta; |
| 149 | + |
| 150 | + type Aggr = A; |
| 151 | + |
| 152 | + fn measure( |
| 153 | + &self, |
| 154 | + value: <Self::Aggr as Aggregator>::PreComputedValue, |
| 155 | + attributes: &[KeyValue], |
| 156 | + ) { |
| 157 | + self.0.measure(value, attributes); |
| 158 | + } |
| 159 | + |
| 160 | + fn collect_data_points<DP, MapFn>(&self, dest: &mut Vec<DP>, mut map_fn: MapFn) |
| 161 | + where |
| 162 | + MapFn: FnMut(Vec<KeyValue>, &Self::Aggr) -> DP, |
| 163 | + { |
| 164 | + self.0 |
| 165 | + .collect_and_reset(dest, |attributes, aggr| map_fn(attributes, &aggr)); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/// At the moment use [`ValueMap`] under the hood (which support both Delta and Cumulative), to implement `AggregateMap` for Cumulative temporality |
| 170 | +/// Later this could be improved to support only Cumulative temporality |
| 171 | +pub(crate) struct CumulativeValueMap<A>(ValueMap<A>) |
| 172 | +where |
| 173 | + A: Aggregator; |
| 174 | + |
| 175 | +impl<A> CumulativeValueMap<A> |
| 176 | +where |
| 177 | + A: Aggregator, |
| 178 | +{ |
| 179 | + pub(crate) fn new(config: A::InitConfig) -> Self { |
| 180 | + Self(ValueMap::new(config)) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +impl<A> AggregateMap for CumulativeValueMap<A> |
| 185 | +where |
| 186 | + A: Aggregator, |
| 187 | + <A as Aggregator>::InitConfig: Send + Sync, |
| 188 | +{ |
| 189 | + const TEMPORALITY: Temporality = Temporality::Cumulative; |
| 190 | + |
| 191 | + type Aggr = A; |
| 192 | + |
| 193 | + fn measure( |
| 194 | + &self, |
| 195 | + value: <Self::Aggr as Aggregator>::PreComputedValue, |
| 196 | + attributes: &[KeyValue], |
| 197 | + ) { |
| 198 | + self.0.measure(value, attributes); |
| 199 | + } |
| 200 | + |
| 201 | + fn collect_data_points<DP, MapFn>(&self, dest: &mut Vec<DP>, map_fn: MapFn) |
| 202 | + where |
| 203 | + MapFn: FnMut(Vec<KeyValue>, &Self::Aggr) -> DP, |
| 204 | + { |
| 205 | + self.0.collect_readonly(dest, map_fn); |
| 206 | + } |
| 207 | +} |
0 commit comments