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
//! Cache metric definitions.

use core::*;
use std::sync::{Arc, RwLock};
use lru_cache::LRUCache;

/// Cache metrics to prevent them from being re-defined on every use.
/// Use of this should be transparent, this has no effect on the values.
/// Stateful sinks (i.e. Aggregate) may naturally cache their definitions.
pub trait WithCache
where
    Self: Sized,
{
    /// Cache metrics to prevent them from being re-defined on every use.
    fn with_cache(&self, cache_size: usize) -> Self;
}

// TODO add selfmetrics cache stats

impl<M: Send + Sync + Clone + 'static> WithCache for Chain<M> {
    fn with_cache(&self, cache_size: usize) -> Self {
        self.mod_metric(|next| {
            let cache: RwLock<LRUCache<String, M>> =
                RwLock::new(LRUCache::with_capacity(cache_size));
            Arc::new(move |kind, name, rate| {
                let mut cache = cache.write().expect("Locking metric cache");
                let name_str = String::from(name);

                // FIXME lookup should use straight &str
                if let Some(value) = cache.get(&name_str) {
                    return value.clone();
                }

                let new_value = (next)(kind, name, rate).clone();
                cache.insert(name_str, new_value.clone());
                new_value
            })
        })
    }
}

/// Cache metrics to prevent them from being re-defined on every use.
/// Use of this should be transparent, this has no effect on the values.
/// Stateful sinks (i.e. Aggregate) may naturally cache their definitions.
#[deprecated(since = "0.5.0", note = "Use `with_cache` instead.")]
pub fn cache<M, IC>(cache_size: usize, chain: IC) -> Chain<M>
where
    M: Clone + Send + Sync + 'static,
    IC: Into<Chain<M>>,
{
    let chain = chain.into();
    chain.with_cache(cache_size)
}