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
use crate::metrics::{AtomicNumber, Descriptor, MetricsError, Number, NumberKind, Result};
use crate::sdk::export::metrics::{Aggregator, Count, Max, Min, MinMaxSumCount, Sum};
use std::any::Any;
use std::cmp::Ordering;
use std::sync::{Arc, Mutex};

/// Create a new `MinMaxSumCountAggregator`
pub fn min_max_sum_count(descriptor: &Descriptor) -> MinMaxSumCountAggregator {
    let kind = descriptor.number_kind().clone();
    MinMaxSumCountAggregator {
        inner: Mutex::new(Inner { state: None }),
        kind,
    }
}

#[derive(Debug)]
struct Inner {
    state: Option<State>,
}

/// An `Aggregator` that aggregates events that form a distribution, keeping
/// only the min, max, sum, and count.
#[derive(Debug)]
pub struct MinMaxSumCountAggregator {
    inner: Mutex<Inner>,
    kind: NumberKind,
}

impl Min for MinMaxSumCountAggregator {
    fn min(&self) -> Result<Number> {
        self.inner.lock().map_err(From::from).map(|inner| {
            inner
                .state
                .as_ref()
                .map_or(0u64.into(), |state| state.min.load())
        })
    }
}

impl Max for MinMaxSumCountAggregator {
    fn max(&self) -> Result<Number> {
        self.inner.lock().map_err(From::from).map(|inner| {
            inner
                .state
                .as_ref()
                .map_or(0u64.into(), |state| state.max.load())
        })
    }
}

impl Sum for MinMaxSumCountAggregator {
    fn sum(&self) -> Result<Number> {
        self.inner.lock().map_err(From::from).map(|inner| {
            inner
                .state
                .as_ref()
                .map_or(0u64.into(), |state| state.sum.load())
        })
    }
}

impl Count for MinMaxSumCountAggregator {
    fn count(&self) -> Result<u64> {
        self.inner
            .lock()
            .map_err(From::from)
            .map(|inner| inner.state.as_ref().map_or(0u64, |state| state.count))
    }
}

impl MinMaxSumCount for MinMaxSumCountAggregator {}

impl Aggregator for MinMaxSumCountAggregator {
    fn update(&self, number: &Number, descriptor: &Descriptor) -> Result<()> {
        self.inner
            .lock()
            .map(|mut inner| {
                if let Some(state) = &mut inner.state {
                    let kind = descriptor.number_kind();

                    state.count = state.count.saturating_add(1);
                    state.sum.fetch_add(kind, number);
                    if number.partial_cmp(kind, &state.min.load()) == Some(Ordering::Less) {
                        state.min = number.to_atomic();
                    }
                    if number.partial_cmp(kind, &state.max.load()) == Some(Ordering::Greater) {
                        state.max = number.to_atomic();
                    }
                } else {
                    inner.state = Some(State {
                        count: 1,
                        sum: number.to_atomic(),
                        min: number.to_atomic(),
                        max: number.to_atomic(),
                    })
                }
            })
            .map_err(From::from)
    }

    fn synchronized_move(
        &self,
        other: &Arc<dyn Aggregator + Send + Sync>,
        _descriptor: &Descriptor,
    ) -> Result<()> {
        if let Some(other) = other.as_any().downcast_ref::<Self>() {
            self.inner.lock().map_err(From::from).and_then(|mut inner| {
                other.inner.lock().map_err(From::from).map(|mut oi| {
                    oi.state = inner.state.take();
                })
            })
        } else {
            Err(MetricsError::InconsistentAggregator(format!(
                "Expected {:?}, got: {:?}",
                self, other
            )))
        }
    }

    fn merge(&self, aggregator: &(dyn Aggregator + Send + Sync), desc: &Descriptor) -> Result<()> {
        if let Some(other) = aggregator.as_any().downcast_ref::<Self>() {
            self.inner.lock().map_err(From::from).and_then(|mut inner| {
                other.inner.lock().map_err(From::from).map(|oi| {
                    match (inner.state.as_mut(), oi.state.as_ref()) {
                        (None, Some(other_checkpoint)) => {
                            inner.state = Some(other_checkpoint.clone());
                        }
                        (Some(_), None) | (None, None) => (),
                        (Some(state), Some(other)) => {
                            state.count = state.count.saturating_add(other.count);
                            state.sum.fetch_add(desc.number_kind(), &other.sum.load());

                            let other_min = other.min.load();
                            let other_max = other.max.load();
                            if state.min.load().partial_cmp(desc.number_kind(), &other_min)
                                == Some(Ordering::Greater)
                            {
                                state.min.store(&other_min);
                            }
                            if state.max.load().partial_cmp(desc.number_kind(), &other_max)
                                == Some(Ordering::Less)
                            {
                                state.max.store(&other_max);
                            }
                        }
                    }
                })
            })
        } else {
            Err(MetricsError::InconsistentAggregator(format!(
                "Expected {:?}, got: {:?}",
                self, aggregator
            )))
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[derive(Clone, Debug)]
struct State {
    count: u64,
    sum: AtomicNumber,
    min: AtomicNumber,
    max: AtomicNumber,
}