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
//! Maintain aggregated metrics for deferred reporting,
//!
use core::*;
use scores::*;
use publish::*;

use std::fmt::Debug;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// Aggregate metrics in memory.
/// Depending on the type of metric, count, sum, minimum and maximum of values will be tracked.
/// Needs to be connected to a publish to be useful.
/// ```
/// use dipstick::*;
/// let sink = aggregate(4, summary, to_stdout());
/// let metrics = global_metrics(sink);
/// metrics.marker("my_event").mark();
/// metrics.marker("my_event").mark();
/// ```
pub fn aggregate<E, M>(stat_fn: E, to_chain: Chain<M>) -> Chain<Aggregate>
where
    E: Fn(Kind, &str, ScoreType) -> Option<(Kind, Vec<&str>, Value)> + Send + Sync + 'static,
    M: Clone + Send + Sync + Debug + 'static,
{
    let metrics = Arc::new(RwLock::new(HashMap::new()));
    let metrics0 = metrics.clone();

    let publish = Arc::new(Publisher::new(stat_fn, to_chain));

    Chain::new(
        move |kind, name, _rate| {
            metrics
                .write()
                .unwrap()
                .entry(name.to_string())
                .or_insert_with(|| Arc::new(Scoreboard::new(kind, name.to_string())))
                .clone()
        },
        move |_buffered| {
            let metrics = metrics0.clone();
            let publish = publish.clone();
            ControlScopeFn::new(move |cmd| match cmd {
                ScopeCmd::Write(metric, value) => {
                    let metric: &Aggregate = metric;
                    metric.update(value)
                },
                ScopeCmd::Flush => {
                    let metrics = metrics.read().expect("Locking metrics scoreboards");
                    let snapshot = metrics.values().flat_map(|score| score.reset()).collect();
                    publish.publish(snapshot);
                }
            })
        },
    )
}

/// Central aggregation structure.
/// Since `AggregateKey`s themselves contain scores, the aggregator simply maintains
/// a shared list of metrics for enumeration when used as source.
#[derive(Debug, Clone)]
pub struct Aggregator {
    metrics: Arc<RwLock<HashMap<String, Arc<Scoreboard>>>>,
    publish: Arc<Publish>,
}

impl Aggregator {
    /// Build a new metric aggregation point with specified initial capacity of metrics to aggregate.
    pub fn with_capacity(size: usize, publish: Arc<Publish>) -> Aggregator {
        Aggregator {
            metrics: Arc::new(RwLock::new(HashMap::with_capacity(size))),
            publish: publish.clone(),
        }
    }

    /// Discard scores for ad-hoc metrics.
    pub fn cleanup(&self) {
        let orphans: Vec<String> = self.metrics.read().unwrap().iter()
            // is aggregator now the sole owner?
            .filter(|&(_k, v)| Arc::strong_count(v) == 1)
            .map(|(k, _v)| k.to_string())
            .collect();
        if !orphans.is_empty() {
            let mut remover = self.metrics.write().unwrap();
            orphans.iter().for_each(|k| {
                remover.remove(k);
            });
        }
    }
}

/// The type of metric created by the Aggregator.
pub type Aggregate = Arc<Scoreboard>;

#[cfg(feature = "bench")]
mod bench {

    use super::*;
    use test;
    use core::Kind::*;
    use output::*;

    #[bench]
    fn time_bench_write_event(b: &mut test::Bencher) {
        let sink = aggregate(summary, to_void());
        let metric = sink.define_metric(Marker, "event_a", 1.0);
        let scope = sink.open_scope(false);
        b.iter(|| test::black_box(scope.write(&metric, 1)));
    }

    #[bench]
    fn time_bench_write_count(b: &mut test::Bencher) {
        let sink = aggregate(summary, to_void());
        let metric = sink.define_metric(Counter, "count_a", 1.0);
        let scope = sink.open_scope(false);
        b.iter(|| test::black_box(scope.write(&metric, 1)));
    }

    #[bench]
    fn time_bench_read_event(b: &mut test::Bencher) {
        let sink = aggregate(summary, to_void());
        let metric = sink.define_metric(Marker, "marker_a", 1.0);
        b.iter(|| test::black_box(metric.reset()));
    }

    #[bench]
    fn time_bench_read_count(b: &mut test::Bencher) {
        let sink = aggregate(summary, to_void());
        let metric = sink.define_metric(Counter, "count_a", 1.0);
        b.iter(|| test::black_box(metric.reset()));
    }

}