metrique-aggregation 0.1.11

Library for working with unit of work metrics - aggregation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#![deny(clippy::arithmetic_side_effects)]

//! Histogram types for aggregating multiple observations into distributions.
//!
//! When emitting high-frequency metrics, you often want to aggregate multiple observations
//! into a single metric entry rather than emitting each one individually. This module provides
//! histogram types that collect observations and emit them as distributions.
//!
//! # When to use histograms
//!
//! Use histograms when you have many observations of the same metric within a single unit of work:
//!
//! - A distributed query that fans out to multiple backend services
//! - Processing a batch of items where you want to track per-item latency
//! - Any operation that generates multiple measurements you want to aggregate
//!
//! For most applications, [sampling](https://github.com/awslabs/metrique/blob/main/docs/sampling.md)
//! is a better approach than aggregation. Consider histograms when you need precise distributions
//! for high-frequency events.
//!
//! # Example
//!
//! ```
//! use metrique::unit_of_work::metrics;
//! use metrique_aggregation::histogram::Histogram;
//! use metrique_writer::unit::Millisecond;
//! use std::time::Duration;
//!
//! #[metrics(rename_all = "PascalCase")]
//! struct QueryMetrics {
//!     query_id: String,
//!
//!     #[metrics(unit = Millisecond)]
//!     backend_latency: Histogram<Duration>,
//! }
//!
//! fn execute_query(query_id: String) {
//!     let mut metrics = QueryMetrics {
//!         query_id,
//!         backend_latency: Histogram::default(),
//!     };
//!
//!     // Record multiple observations
//!     metrics.backend_latency.add_value(Duration::from_millis(45));
//!     metrics.backend_latency.add_value(Duration::from_millis(67));
//!     metrics.backend_latency.add_value(Duration::from_millis(52));
//!
//!     // When metrics drops, emits a single entry with the distribution
//! }
//! ```
//!
//! # Choosing an aggregation strategy
//!
//! By default, histograms use [`ExponentialAggregationStrategy`]. To use a different strategy,
//! specify it as the second type parameter:
//!
//! ```
//! use metrique_aggregation::histogram::{Histogram, SortAndMerge};
//! use std::time::Duration;
//!
//! let histogram: Histogram<Duration, SortAndMerge> = Histogram::new(SortAndMerge::new());
//! ```
//!
//! ## ExponentialAggregationStrategy (default)
//!
//! Uses exponential bucketing with ~6.25% error. This is the best choice for most use cases:
//!
//! - Provides consistent relative precision across wide value ranges
//! - Memory efficient with fixed bucket count (464 buckets)
//! - Fast recording and draining operations
//!
//! Use this when you need good precision across values that span multiple orders of magnitude
//! (e.g., latencies from microseconds to seconds).
//!
//! ## AtomicExponentialAggregationStrategy
//!
//! Thread-safe version of exponential bucketing. Use with [`crate::histogram::SharedHistogram`] when you need
//! to record values from multiple threads concurrently:
//!
//! ```
//! use metrique_aggregation::histogram::{SharedHistogram, AtomicExponentialAggregationStrategy};
//! use std::time::Duration;
//!
//! let histogram: SharedHistogram<Duration, AtomicExponentialAggregationStrategy> =
//!     SharedHistogram::new(AtomicExponentialAggregationStrategy::new());
//! ```
//!
//! ## SortAndMerge
//!
//! Stores all observations exactly and sorts them on emission:
//!
//! - Perfect precision - no bucketing error
//! - Memory usage grows with observation count
//! - Slower drain operation due to sorting
//!
//! Use this when you need exact values and have a bounded number of observations (typically < 1000).

use histogram::Config;
use metrique_core::CloseValue;
use metrique_writer::{Distribution, MetricFlags, MetricValue, Observation, Value, ValueWriter};
use ordered_float::OrderedFloat;
use smallvec::SmallVec;
use std::{borrow::Borrow, marker::PhantomData};

use crate::traits::AggregateValue;

/// Strategy for aggregating observations in a histogram.
///
/// Implementations determine how values are stored and converted to observations
/// when the histogram is closed.
pub trait AggregationStrategy {
    /// Record a single observation.
    fn record(&mut self, value: f64) {
        self.record_many(value, 1);
    }

    /// Record multiple observations of the same value.
    fn record_many(&mut self, value: f64, count: u64);

    /// Drain all observations and return them as a vector.
    ///
    /// This resets the strategy's internal state.
    fn drain(&mut self) -> Vec<Observation>;
}

/// Thread-safe strategy for aggregating observations in a histogram.
///
/// Like [`AggregationStrategy`] but allows recording values through a shared reference.
pub trait SharedAggregationStrategy {
    /// Record a single observation.
    fn record(&self, value: f64) {
        self.record_many(value, 1);
    }

    /// Record multiple observations of the same value through a shared reference.
    fn record_many(&self, value: f64, count: u64);

    /// Drain all observations and return them as a vector.
    ///
    /// This resets the strategy's internal state.
    fn drain(&self) -> Vec<Observation>;
}

/// A histogram that collects multiple observations and emits them as a distribution.
///
/// Use this when you have many observations of the same metric within a single unit of work.
/// The histogram aggregates values in memory and emits them as a single metric entry.
///
/// If you want to preserve all values instead of bucketing them, use `Histogram<T, SortAndMerge>` as
/// the strategy.
///
/// Requires `&mut self` to add values. For thread-safe access, use [`SharedHistogram`].
pub struct Histogram<T, S = ExponentialAggregationStrategy> {
    strategy: S,
    _value: PhantomData<T>,
}

impl<T, S: AggregationStrategy> Histogram<T, S> {
    /// Create a new histogram with the given aggregation strategy.
    pub fn new(strategy: S) -> Self {
        Self {
            strategy,
            _value: PhantomData,
        }
    }

    /// Add a value to the histogram.
    ///
    /// The value is converted to observations using the metric value's implementation,
    /// then recorded in the aggregation strategy.
    pub fn add_value(&mut self, value: impl Borrow<T>)
    where
        T: MetricValue,
    {
        let value = value.borrow();
        struct Capturer<'a, S>(&'a mut S);
        impl<'b, S: AggregationStrategy> ValueWriter for Capturer<'b, S> {
            fn string(self, _value: &str) {}
            fn metric<'a>(
                self,
                distribution: impl IntoIterator<Item = Observation>,
                _unit: metrique_writer::Unit,
                _dimensions: impl IntoIterator<Item = (&'a str, &'a str)>,
                _flags: MetricFlags<'_>,
            ) {
                for obs in distribution {
                    match obs {
                        Observation::Unsigned(v) => self.0.record(v as f64),
                        Observation::Floating(v) => self.0.record(v),
                        Observation::Repeated { total, occurrences } => {
                            if occurrences > 0 {
                                let avg = total / occurrences as f64;
                                self.0.record_many(avg, occurrences);
                            }
                        }
                        _ => {}
                    }
                }
            }
            fn error(self, _error: metrique_writer::ValidationError) {}
        }

        let capturer = Capturer(&mut self.strategy);
        value.write(capturer);
    }
}

impl<T, S: Default + AggregationStrategy> Default for Histogram<T, S> {
    fn default() -> Self {
        Self::new(S::default())
    }
}

impl<T: MetricValue, S: AggregationStrategy> CloseValue for Histogram<T, S> {
    type Closed = HistogramClosed<T>;

    fn close(mut self) -> Self::Closed {
        HistogramClosed {
            observations: self.strategy.drain(),
            _value: PhantomData,
        }
    }
}

/// Thread-safe histogram that collects multiple observations and emits them as a distribution.
///
/// Like [`Histogram`] but allows adding values through a shared reference, making it
/// suitable for concurrent access patterns.
pub struct SharedHistogram<T, S = AtomicExponentialAggregationStrategy> {
    strategy: S,
    _value: PhantomData<T>,
}

impl<T, S: Default> Default for SharedHistogram<T, S> {
    fn default() -> Self {
        Self {
            strategy: Default::default(),
            _value: Default::default(),
        }
    }
}

impl<T, S: SharedAggregationStrategy> SharedHistogram<T, S> {
    /// Create a new atomic histogram with the given aggregation strategy.
    pub fn new(strategy: S) -> Self {
        Self {
            strategy,
            _value: PhantomData,
        }
    }

    /// Add a value to the histogram through a shared reference.
    ///
    /// The value is converted to observations using the metric value's implementation,
    /// then recorded in the aggregation strategy.
    pub fn add_value(&self, value: T)
    where
        T: MetricValue,
    {
        struct Capturer<'a, S>(&'a S);
        impl<'b, S: SharedAggregationStrategy> ValueWriter for Capturer<'b, S> {
            fn string(self, _value: &str) {}
            fn metric<'a>(
                self,
                distribution: impl IntoIterator<Item = Observation>,
                _unit: metrique_writer::Unit,
                _dimensions: impl IntoIterator<Item = (&'a str, &'a str)>,
                _flags: MetricFlags<'_>,
            ) {
                for obs in distribution {
                    match obs {
                        Observation::Unsigned(v) => self.0.record(v as f64),
                        Observation::Floating(v) => self.0.record(v),
                        Observation::Repeated { total, occurrences } => {
                            if occurrences > 0 {
                                let avg = total / occurrences as f64;
                                self.0.record_many(avg, occurrences);
                            }
                        }
                        _ => {}
                    }
                }
            }
            fn error(self, _error: metrique_writer::ValidationError) {}
        }

        let capturer = Capturer(&self.strategy);
        value.write(capturer);
    }
}

impl<T: MetricValue, S: SharedAggregationStrategy> CloseValue for SharedHistogram<T, S> {
    type Closed = HistogramClosed<T>;

    fn close(self) -> Self::Closed {
        HistogramClosed {
            observations: self.strategy.drain(),
            _value: PhantomData,
        }
    }
}

/// Closed histogram value containing aggregated observations.
///
/// This is the result of closing a histogram and is emitted as a metric distribution.
pub struct HistogramClosed<T> {
    observations: Vec<Observation>,
    _value: PhantomData<T>,
}

impl<T> Value for HistogramClosed<T>
where
    T: MetricValue,
{
    fn write(&self, writer: impl ValueWriter) {
        use metrique_writer::unit::UnitTag;
        writer.metric(
            self.observations.iter().copied(),
            T::Unit::UNIT,
            [],
            MetricFlags::upcast(&Distribution),
        )
    }
}

impl<T> MetricValue for HistogramClosed<T>
where
    T: MetricValue,
{
    type Unit = T::Unit;
}

const SCALING_FACTOR: f64 = (1 << 10) as f64;

fn scale_up(v: impl Into<f64>) -> f64 {
    v.into() * SCALING_FACTOR
}

fn scale_down(v: impl Into<f64>) -> f64 {
    v.into() / SCALING_FACTOR
}

/// Exponential bucketing strategy using the histogram crate.
///
/// This uses 976 buckets and supports values from 0 to u64::MAX. Values greater than u64::MAX are truncated to u64::MAX.
/// Scaling factor for converting floating point values to integers for histogram bucketing.
/// 2^10 = 1024, providing 3 decimal places of precision.
///
/// Uses exponential bucketing with configurable precision. Default configuration
/// uses 4-bit mantissa precision (16 buckets per order of magnitude, ~6.25% error).
pub struct ExponentialAggregationStrategy {
    inner: histogram::Histogram,
}

impl ExponentialAggregationStrategy {
    /// Create a new exponential aggregation strategy with default configuration.
    pub fn new() -> Self {
        let config = default_histogram_config();
        Self {
            inner: histogram::Histogram::with_config(&config),
        }
    }
}

impl Default for ExponentialAggregationStrategy {
    fn default() -> Self {
        Self::new()
    }
}

fn default_histogram_config() -> Config {
    Config::new(4, 64).expect("known good")
}

impl AggregationStrategy for ExponentialAggregationStrategy {
    fn record_many(&mut self, value: f64, count: u64) {
        // the inner histogram drops data above u64::MAX in our default configuration
        let value = scale_up(value);
        self.inner
            .add(value.min(u64::MAX as f64) as u64, count)
            .ok();
    }

    fn drain(&mut self) -> Vec<Observation> {
        let snapshot = std::mem::replace(
            &mut self.inner,
            histogram::Histogram::with_config(&default_histogram_config()),
        );
        snapshot
            .iter()
            .filter(|bucket| bucket.count() > 0)
            .map(|bucket| {
                let range = bucket.range();
                let midpoint = range.start().midpoint(*range.end());
                let midpoint = scale_down(midpoint as f64);
                Observation::Repeated {
                    total: midpoint * bucket.count() as f64,
                    occurrences: bucket.count(),
                }
            })
            .collect()
    }
}

/// Strategy that stores all observations and sorts them on emission.
///
/// This preserves all observations exactly but uses more memory than bucketing strategies.
/// This uses a `SmallVec` (default size 32, memory usage of 256 bytes) to avoid allocations for small numbers of observations.
///
/// The const generic `N` controls the inline capacity before heap allocation.
#[derive(Default)]
pub struct SortAndMerge<const N: usize = 32> {
    values: SmallVec<[f64; N]>,
}

impl<const N: usize> SortAndMerge<N> {
    /// Create a new sort-and-merge strategy.
    pub fn new() -> Self {
        Self {
            values: SmallVec::new(),
        }
    }
}

impl<const N: usize> AggregationStrategy for SortAndMerge<N> {
    fn record_many(&mut self, value: f64, count: u64) {
        self.values
            .extend(std::iter::repeat_n(value, count as usize));
    }

    fn drain(&mut self) -> Vec<Observation> {
        self.values.sort_by_key(|v| OrderedFloat(*v));
        let mut observations = Vec::new();
        let mut iter = self.values.iter().copied().filter(|v| !v.is_nan());

        if let Some(first) = iter.next() {
            let mut current_value = first;
            let mut current_count: u64 = 1;

            for value in iter {
                if value == current_value {
                    current_count = current_count.saturating_add(1);
                } else {
                    observations.push(Observation::Repeated {
                        total: current_value * current_count as f64,
                        occurrences: current_count,
                    });
                    current_value = value;
                    current_count = 1;
                }
            }

            observations.push(Observation::Repeated {
                total: current_value * current_count as f64,
                occurrences: current_count,
            });
        }

        self.values.clear();
        observations
    }
}

/// Thread-safe exponential bucketing strategy using atomic counters.
///
/// This uses 976 buckets and supports values from 0 to u64::MAX. Values greater than u64::MAX are truncated to u64::MAX.
///
/// Like [`ExponentialAggregationStrategy`] but uses atomic operations to allow concurrent
/// recording from multiple threads.
pub struct AtomicExponentialAggregationStrategy {
    inner: histogram::AtomicHistogram,
}

impl AtomicExponentialAggregationStrategy {
    /// Create a new atomic exponential aggregation strategy with default configuration.
    pub fn new() -> Self {
        Self {
            inner: histogram::AtomicHistogram::with_config(&default_histogram_config()),
        }
    }
}

impl Default for AtomicExponentialAggregationStrategy {
    fn default() -> Self {
        Self::new()
    }
}

impl SharedAggregationStrategy for AtomicExponentialAggregationStrategy {
    fn record_many(&self, value: f64, count: u64) {
        let value = scale_up(value);
        self.inner
            .add(value.min(u64::MAX as f64) as u64, count)
            .ok();
    }

    fn drain(&self) -> Vec<Observation> {
        self.inner
            .drain()
            .iter()
            .filter(|bucket| bucket.count() > 0)
            .map(|bucket| {
                let range = bucket.range();
                let midpoint = range.start().midpoint(*range.end());
                let midpoint = scale_down(midpoint as f64);
                Observation::Repeated {
                    total: midpoint * bucket.count() as f64,
                    occurrences: bucket.count(),
                }
            })
            .collect()
    }
}

/// AggregateValue implementation for Histogram
impl<T, S> AggregateValue<T> for Histogram<T, S>
where
    T: MetricValue,
    S: AggregationStrategy + Default,
{
    type Aggregated = Histogram<T, S>;

    fn insert(accum: &mut Self::Aggregated, value: T) {
        accum.add_value(value);
    }
}

/// AggregateValue implementation for merging closed histograms into a histogram.
///
/// This enables aggregating structs that already contain `Histogram` fields —
/// when the source is closed, each `Histogram<T>` becomes a `HistogramClosed<T>`,
/// and this impl replays those observations into the accumulator histogram.
impl<T, S> AggregateValue<HistogramClosed<T>> for Histogram<T, S>
where
    T: MetricValue,
    S: AggregationStrategy + Default,
{
    type Aggregated = Histogram<T, S>;

    fn insert(accum: &mut Self::Aggregated, value: HistogramClosed<T>) {
        for obs in value.observations {
            match obs {
                Observation::Repeated { total, occurrences } => {
                    if occurrences > 0 {
                        accum
                            .strategy
                            .record_many(total / occurrences as f64, occurrences);
                    }
                }
                Observation::Unsigned(v) => accum.strategy.record(v as f64),
                Observation::Floating(v) => accum.strategy.record(v),
                _ => {}
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use assert2::check;
    use metrique_writer::Observation;

    use crate::histogram::{
        AggregationStrategy, AtomicExponentialAggregationStrategy, ExponentialAggregationStrategy,
        SharedAggregationStrategy, default_histogram_config, scale_down, scale_up,
    };

    #[test]
    fn test_histogram_max_values() {
        let v = f64::MAX;
        let mut strat = ExponentialAggregationStrategy::new();
        strat.record(v);
        check!(
            strat.drain()
                == vec![Observation::Repeated {
                    // value is truncated to u64::MAX
                    total: 1.7732923532771328e16,
                    occurrences: 1,
                }]
        );
    }

    #[test]
    fn test_atomic_histogram_max_values() {
        let v = f64::MAX;
        let strat = AtomicExponentialAggregationStrategy::new();
        strat.record(v);
        check!(
            strat.drain()
                == vec![Observation::Repeated {
                    // value is truncated to u64::MAX
                    total: 1.7732923532771328e16,
                    occurrences: 1,
                }]
        );
    }

    #[test]
    fn num_buckets() {
        check!(default_histogram_config().total_buckets() == 976);
    }

    #[test]
    fn test_scaling() {
        let x = 0.001;
        check!(scale_down(scale_up(x)) == x);
    }
}