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
use crate::common::{Identifier, Kind, Measurement, ValueHandle, ValueSnapshot};
use crate::config::Configuration;
use crate::data::Snapshot;
use crate::registry::ScopeRegistry;
use arc_swap::{ptr_eq, ArcSwap};
use im::hashmap::HashMap;
use metrics_core::Observer;
use quanta::Clock;
use std::ops::Deref;
use std::sync::Arc;

#[derive(Debug)]
pub(crate) struct MetricRegistry {
    scope_registry: Arc<ScopeRegistry>,
    metrics: ArcSwap<HashMap<Identifier, ValueHandle>>,
    config: Configuration,
    clock: Clock,
}

impl MetricRegistry {
    pub fn new(scope_registry: Arc<ScopeRegistry>, config: Configuration, clock: Clock) -> Self {
        MetricRegistry {
            scope_registry,
            metrics: ArcSwap::new(Arc::new(HashMap::new())),
            config,
            clock,
        }
    }

    pub fn get_or_register(&self, id: Identifier) -> ValueHandle {
        loop {
            match self.metrics.lease().deref().get(&id) {
                Some(handle) => return handle.clone(),
                None => {
                    let value_handle = match id.kind() {
                        Kind::Counter => ValueHandle::counter(),
                        Kind::Gauge => ValueHandle::gauge(),
                        Kind::Histogram => ValueHandle::histogram(
                            self.config.histogram_window,
                            self.config.histogram_granularity,
                            self.clock.clone(),
                        ),
                        Kind::Proxy => ValueHandle::proxy(),
                    };

                    let metrics_ptr = self.metrics.lease();
                    let mut metrics = metrics_ptr.deref().clone();
                    match metrics.insert(id.clone(), value_handle.clone()) {
                        // Somebody else beat us to it, loop.
                        Some(_) => continue,
                        None => {
                            // If we weren't able to cleanly update the map, then try again.
                            let old = self
                                .metrics
                                .compare_and_swap(&metrics_ptr, Arc::new(metrics));
                            if !ptr_eq(old, metrics_ptr) {
                                continue;
                            }
                        }
                    }

                    return value_handle;
                }
            }
        }
    }

    pub fn snapshot(&self) -> Snapshot {
        let mut values = Vec::new();

        let metrics = self.metrics.load().deref().clone();
        for (id, value) in metrics.into_iter() {
            let (key, scope_handle, _) = id.into_parts();
            let scope = self.scope_registry.get(scope_handle);

            match value.snapshot() {
                ValueSnapshot::Single(measurement) => {
                    let key = key.map_name(|name| scope.into_string(name));
                    values.push((key, measurement));
                }
                ValueSnapshot::Multiple(mut measurements) => {
                    // Tack on the key name that this proxy was registered with to the scope so
                    // that we can clone _that_, and then scope our individual measurements.
                    let (base_key, labels) = key.into_parts();
                    let scope = scope.clone().add_part(base_key);

                    for (subkey, measurement) in measurements.drain(..) {
                        let scope = scope.clone();
                        let mut subkey = subkey.map_name(|name| scope.into_string(name));
                        subkey.add_labels(labels.clone());
                        values.push((subkey, measurement));
                    }
                }
            }
        }

        Snapshot::new(values)
    }

    pub fn observe<O: Observer>(&self, observer: &mut O) {
        let metrics = self.metrics.load().deref().clone();
        for (id, value) in metrics.into_iter() {
            let (key, scope_handle, _) = id.into_parts();
            let scope = self.scope_registry.get(scope_handle);

            let observe = |observer: &mut O, key, measurement| match measurement {
                Measurement::Counter(value) => observer.observe_counter(key, value),
                Measurement::Gauge(value) => observer.observe_gauge(key, value),
                Measurement::Histogram(stream) => stream.decompress_with(|values| {
                    observer.observe_histogram(key.clone(), values);
                }),
            };

            match value.snapshot() {
                ValueSnapshot::Single(measurement) => {
                    let key = key.map_name(|name| scope.into_string(name));
                    observe(observer, key, measurement);
                }
                ValueSnapshot::Multiple(mut measurements) => {
                    // Tack on the key name that this proxy was registered with to the scope so
                    // that we can clone _that_, and then scope our individual measurements.
                    let (base_key, labels) = key.into_parts();
                    let scope = scope.clone().add_part(base_key);

                    for (subkey, measurement) in measurements.drain(..) {
                        let scope = scope.clone();
                        let mut subkey = subkey.map_name(|name| scope.into_string(name));
                        subkey.add_labels(labels.clone());
                        observe(observer, subkey, measurement);
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        Clock, Configuration, Identifier, Kind, Measurement, MetricRegistry, ScopeRegistry,
    };
    use crate::data::{Counter, Gauge, Histogram};
    use metrics_core::{Key, Label};
    use metrics_util::StreamingIntegers;
    use std::mem;
    use std::sync::Arc;

    #[test]
    fn test_snapshot() {
        // Get our registry.
        let sr = Arc::new(ScopeRegistry::new());
        let config = Configuration::mock();
        let (clock, _) = Clock::mock();
        let mr = Arc::new(MetricRegistry::new(sr, config, clock));

        // Set some metrics.
        let cid = Identifier::new("counter", 0, Kind::Counter);
        let counter: Counter = mr.get_or_register(cid).into();
        counter.record(15);

        let gid = Identifier::new("gauge", 0, Kind::Gauge);
        let gauge: Gauge = mr.get_or_register(gid).into();
        gauge.record(89);

        let hid = Identifier::new("histogram", 0, Kind::Histogram);
        let histogram: Histogram = mr.get_or_register(hid).into();
        histogram.record_value(89);

        let pid = Identifier::new("proxy", 0, Kind::Proxy);
        let proxy = mr.get_or_register(pid);
        proxy.update_proxy(|| vec![(Key::from_name("counter"), Measurement::Counter(13))]);

        let mut snapshot = mr.snapshot().into_measurements();
        snapshot.sort_by_key(|(k, _)| k.name());

        let mut expected = vec![
            (Key::from_name("counter"), Measurement::Counter(15)),
            (Key::from_name("gauge"), Measurement::Gauge(89)),
            (
                Key::from_name("histogram"),
                Measurement::Histogram(StreamingIntegers::new()),
            ),
            (Key::from_name("proxy.counter"), Measurement::Counter(13)),
        ];
        expected.sort_by_key(|(k, _)| k.name());

        assert_eq!(snapshot.len(), expected.len());
        for rhs in expected {
            let lhs = snapshot.remove(0);
            assert_eq!(lhs.0, rhs.0);
            assert_eq!(mem::discriminant(&lhs.1), mem::discriminant(&rhs.1));
        }
    }

    #[test]
    fn test_snapshot_with_labels() {
        // Get our registry.
        let sr = Arc::new(ScopeRegistry::new());
        let config = Configuration::mock();
        let (clock, _) = Clock::mock();
        let mr = Arc::new(MetricRegistry::new(sr, config, clock));

        let labels = vec![Label::new("type", "test")];

        // Set some metrics.
        let cid = Identifier::new(("counter", labels.clone()), 0, Kind::Counter);
        let counter: Counter = mr.get_or_register(cid).into();
        counter.record(15);

        let gid = Identifier::new(("gauge", labels.clone()), 0, Kind::Gauge);
        let gauge: Gauge = mr.get_or_register(gid).into();
        gauge.record(89);

        let hid = Identifier::new(("histogram", labels.clone()), 0, Kind::Histogram);
        let histogram: Histogram = mr.get_or_register(hid).into();
        histogram.record_value(89);

        let pid = Identifier::new(("proxy", labels.clone()), 0, Kind::Proxy);
        let proxy = mr.get_or_register(pid);
        proxy.update_proxy(|| vec![(Key::from_name("counter"), Measurement::Counter(13))]);

        let mut snapshot = mr.snapshot().into_measurements();
        snapshot.sort_by_key(|(k, _)| k.name());

        let mut expected = vec![
            (
                Key::from_name_and_labels("counter", labels.clone()),
                Measurement::Counter(15),
            ),
            (
                Key::from_name_and_labels("gauge", labels.clone()),
                Measurement::Gauge(89),
            ),
            (
                Key::from_name_and_labels("histogram", labels.clone()),
                Measurement::Histogram(StreamingIntegers::new()),
            ),
            (
                Key::from_name_and_labels("proxy.counter", labels.clone()),
                Measurement::Counter(13),
            ),
        ];
        expected.sort_by_key(|(k, _)| k.name());

        assert_eq!(snapshot.len(), expected.len());
        for rhs in expected {
            let lhs = snapshot.remove(0);
            assert_eq!(lhs.0, rhs.0);
            assert_eq!(mem::discriminant(&lhs.1), mem::discriminant(&rhs.1));
        }
    }
}