@@ -27,15 +27,6 @@ pub(crate) trait Measure<T>: Send + Sync + 'static {
27
27
fn call ( & self , measurement : T , attrs : & [ KeyValue ] ) ;
28
28
}
29
29
30
- impl < F , T > Measure < T > for F
31
- where
32
- F : Fn ( T , & [ KeyValue ] ) + Send + Sync + ' static ,
33
- {
34
- fn call ( & self , measurement : T , attrs : & [ KeyValue ] ) {
35
- self ( measurement, attrs)
36
- }
37
- }
38
-
39
30
/// Stores the aggregate of measurements into the aggregation and returns the number
40
31
/// of aggregate data-points output.
41
32
pub ( crate ) trait ComputeAggregation : Send + Sync + ' static {
@@ -47,15 +38,23 @@ pub(crate) trait ComputeAggregation: Send + Sync + 'static {
47
38
fn call ( & self , dest : Option < & mut dyn Aggregation > ) -> ( usize , Option < Box < dyn Aggregation > > ) ;
48
39
}
49
40
50
- impl < T > ComputeAggregation for T
41
+ /// Separate `measure` and `collect` functions for an aggregate.
42
+ pub ( crate ) struct AggregateFns < T > {
43
+ pub ( crate ) measure : Arc < dyn Measure < T > > ,
44
+ pub ( crate ) collect : Arc < dyn ComputeAggregation > ,
45
+ }
46
+
47
+ /// Creates aggregate functions out of aggregate instance
48
+ impl < A , T > From < A > for AggregateFns < T >
51
49
where
52
- T : Fn ( Option < & mut dyn Aggregation > ) -> ( usize , Option < Box < dyn Aggregation > > )
53
- + Send
54
- + Sync
55
- + ' static ,
50
+ A : Measure < T > + ComputeAggregation ,
56
51
{
57
- fn call ( & self , dest : Option < & mut dyn Aggregation > ) -> ( usize , Option < Box < dyn Aggregation > > ) {
58
- self ( dest)
52
+ fn from ( value : A ) -> Self {
53
+ let inst = Arc :: new ( value) ;
54
+ Self {
55
+ measure : inst. clone ( ) ,
56
+ collect : inst,
57
+ }
59
58
}
60
59
}
61
60
@@ -144,30 +143,18 @@ impl<T: Number> AggregateBuilder<T> {
144
143
}
145
144
146
145
/// Builds a last-value aggregate function input and output.
147
- pub ( crate ) fn last_value ( & self ) -> ( impl Measure < T > , impl ComputeAggregation ) {
148
- let lv = Arc :: new ( LastValue :: new ( self . temporality , self . filter . clone ( ) ) ) ;
149
- ( lv. clone ( ) , lv)
146
+ pub ( crate ) fn last_value ( & self ) -> AggregateFns < T > {
147
+ LastValue :: new ( self . temporality , self . filter . clone ( ) ) . into ( )
150
148
}
151
149
152
150
/// Builds a precomputed sum aggregate function input and output.
153
- pub ( crate ) fn precomputed_sum (
154
- & self ,
155
- monotonic : bool ,
156
- ) -> ( impl Measure < T > , impl ComputeAggregation ) {
157
- let s = Arc :: new ( PrecomputedSum :: new (
158
- self . temporality ,
159
- self . filter . clone ( ) ,
160
- monotonic,
161
- ) ) ;
162
-
163
- ( s. clone ( ) , s)
151
+ pub ( crate ) fn precomputed_sum ( & self , monotonic : bool ) -> AggregateFns < T > {
152
+ PrecomputedSum :: new ( self . temporality , self . filter . clone ( ) , monotonic) . into ( )
164
153
}
165
154
166
155
/// Builds a sum aggregate function input and output.
167
- pub ( crate ) fn sum ( & self , monotonic : bool ) -> ( impl Measure < T > , impl ComputeAggregation ) {
168
- let s = Arc :: new ( Sum :: new ( self . temporality , self . filter . clone ( ) , monotonic) ) ;
169
-
170
- ( s. clone ( ) , s)
156
+ pub ( crate ) fn sum ( & self , monotonic : bool ) -> AggregateFns < T > {
157
+ Sum :: new ( self . temporality , self . filter . clone ( ) , monotonic) . into ( )
171
158
}
172
159
173
160
/// Builds a histogram aggregate function input and output.
@@ -176,16 +163,15 @@ impl<T: Number> AggregateBuilder<T> {
176
163
boundaries : Vec < f64 > ,
177
164
record_min_max : bool ,
178
165
record_sum : bool ,
179
- ) -> ( impl Measure < T > , impl ComputeAggregation ) {
180
- let h = Arc :: new ( Histogram :: new (
166
+ ) -> AggregateFns < T > {
167
+ Histogram :: new (
181
168
self . temporality ,
182
169
self . filter . clone ( ) ,
183
170
boundaries,
184
171
record_min_max,
185
172
record_sum,
186
- ) ) ;
187
-
188
- ( h. clone ( ) , h)
173
+ )
174
+ . into ( )
189
175
}
190
176
191
177
/// Builds an exponential histogram aggregate function input and output.
@@ -195,17 +181,16 @@ impl<T: Number> AggregateBuilder<T> {
195
181
max_scale : i8 ,
196
182
record_min_max : bool ,
197
183
record_sum : bool ,
198
- ) -> ( impl Measure < T > , impl ComputeAggregation ) {
199
- let h = Arc :: new ( ExpoHistogram :: new (
184
+ ) -> AggregateFns < T > {
185
+ ExpoHistogram :: new (
200
186
self . temporality ,
201
187
self . filter . clone ( ) ,
202
188
max_size,
203
189
max_scale,
204
190
record_min_max,
205
191
record_sum,
206
- ) ) ;
207
-
208
- ( h. clone ( ) , h)
192
+ )
193
+ . into ( )
209
194
}
210
195
}
211
196
@@ -221,7 +206,7 @@ mod tests {
221
206
222
207
#[ test]
223
208
fn last_value_aggregation ( ) {
224
- let ( measure, agg ) =
209
+ let AggregateFns { measure, collect } =
225
210
AggregateBuilder :: < u64 > :: new ( Temporality :: Cumulative , None ) . last_value ( ) ;
226
211
let mut a = Gauge {
227
212
data_points : vec ! [ GaugeDataPoint {
@@ -235,7 +220,7 @@ mod tests {
235
220
let new_attributes = [ KeyValue :: new ( "b" , 2 ) ] ;
236
221
measure. call ( 2 , & new_attributes[ ..] ) ;
237
222
238
- let ( count, new_agg) = agg . call ( Some ( & mut a) ) ;
223
+ let ( count, new_agg) = collect . call ( Some ( & mut a) ) ;
239
224
240
225
assert_eq ! ( count, 1 ) ;
241
226
assert ! ( new_agg. is_none( ) ) ;
@@ -247,7 +232,7 @@ mod tests {
247
232
#[ test]
248
233
fn precomputed_sum_aggregation ( ) {
249
234
for temporality in [ Temporality :: Delta , Temporality :: Cumulative ] {
250
- let ( measure, agg ) =
235
+ let AggregateFns { measure, collect } =
251
236
AggregateBuilder :: < u64 > :: new ( temporality, None ) . precomputed_sum ( true ) ;
252
237
let mut a = Sum {
253
238
data_points : vec ! [
@@ -274,7 +259,7 @@ mod tests {
274
259
let new_attributes = [ KeyValue :: new ( "b" , 2 ) ] ;
275
260
measure. call ( 3 , & new_attributes[ ..] ) ;
276
261
277
- let ( count, new_agg) = agg . call ( Some ( & mut a) ) ;
262
+ let ( count, new_agg) = collect . call ( Some ( & mut a) ) ;
278
263
279
264
assert_eq ! ( count, 1 ) ;
280
265
assert ! ( new_agg. is_none( ) ) ;
@@ -289,7 +274,8 @@ mod tests {
289
274
#[ test]
290
275
fn sum_aggregation ( ) {
291
276
for temporality in [ Temporality :: Delta , Temporality :: Cumulative ] {
292
- let ( measure, agg) = AggregateBuilder :: < u64 > :: new ( temporality, None ) . sum ( true ) ;
277
+ let AggregateFns { measure, collect } =
278
+ AggregateBuilder :: < u64 > :: new ( temporality, None ) . sum ( true ) ;
293
279
let mut a = Sum {
294
280
data_points : vec ! [
295
281
SumDataPoint {
@@ -315,7 +301,7 @@ mod tests {
315
301
let new_attributes = [ KeyValue :: new ( "b" , 2 ) ] ;
316
302
measure. call ( 3 , & new_attributes[ ..] ) ;
317
303
318
- let ( count, new_agg) = agg . call ( Some ( & mut a) ) ;
304
+ let ( count, new_agg) = collect . call ( Some ( & mut a) ) ;
319
305
320
306
assert_eq ! ( count, 1 ) ;
321
307
assert ! ( new_agg. is_none( ) ) ;
@@ -330,7 +316,7 @@ mod tests {
330
316
#[ test]
331
317
fn explicit_bucket_histogram_aggregation ( ) {
332
318
for temporality in [ Temporality :: Delta , Temporality :: Cumulative ] {
333
- let ( measure, agg ) = AggregateBuilder :: < u64 > :: new ( temporality, None )
319
+ let AggregateFns { measure, collect } = AggregateBuilder :: < u64 > :: new ( temporality, None )
334
320
. explicit_bucket_histogram ( vec ! [ 1.0 ] , true , true ) ;
335
321
let mut a = Histogram {
336
322
data_points : vec ! [ HistogramDataPoint {
@@ -354,7 +340,7 @@ mod tests {
354
340
let new_attributes = [ KeyValue :: new ( "b" , 2 ) ] ;
355
341
measure. call ( 3 , & new_attributes[ ..] ) ;
356
342
357
- let ( count, new_agg) = agg . call ( Some ( & mut a) ) ;
343
+ let ( count, new_agg) = collect . call ( Some ( & mut a) ) ;
358
344
359
345
assert_eq ! ( count, 1 ) ;
360
346
assert ! ( new_agg. is_none( ) ) ;
@@ -373,7 +359,7 @@ mod tests {
373
359
#[ test]
374
360
fn exponential_histogram_aggregation ( ) {
375
361
for temporality in [ Temporality :: Delta , Temporality :: Cumulative ] {
376
- let ( measure, agg ) = AggregateBuilder :: < u64 > :: new ( temporality, None )
362
+ let AggregateFns { measure, collect } = AggregateBuilder :: < u64 > :: new ( temporality, None )
377
363
. exponential_bucket_histogram ( 4 , 20 , true , true ) ;
378
364
let mut a = ExponentialHistogram {
379
365
data_points : vec ! [ ExponentialHistogramDataPoint {
@@ -406,7 +392,7 @@ mod tests {
406
392
let new_attributes = [ KeyValue :: new ( "b" , 2 ) ] ;
407
393
measure. call ( 3 , & new_attributes[ ..] ) ;
408
394
409
- let ( count, new_agg) = agg . call ( Some ( & mut a) ) ;
395
+ let ( count, new_agg) = collect . call ( Some ( & mut a) ) ;
410
396
411
397
assert_eq ! ( count, 1 ) ;
412
398
assert ! ( new_agg. is_none( ) ) ;
0 commit comments