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
pub mod expose;
pub mod implementation;
pub mod metrics;

mod helpers;

use std::collections::HashMap;
use std::rc::Rc;
use std::rc::Weak;
use std::cell::RefCell;
use std::fmt;
use std::error;

use implementation::Implementation;

pub struct Registry<'a> {
    metrics: HashMap<String, Weak<RefCell<Collectable + 'a>>>
}

impl <'a>Registry<'a> {
    pub fn new() -> Registry<'a> {
        Registry { metrics: HashMap::new() }
    }

    pub fn counter(&mut self, name: String, help: Option<String>, labels: Vec<String>)
                   -> Result<Rc<RefCell<Metric<metrics::Counter>>>, Error> {
        let m = Metric::new(Box::new(|| metrics::Counter::new()), name.clone(), help, labels);
        self.register(name, m)
    }

    pub fn summary(&mut self, name: String, help: Option<String>, labels: Vec<String>)
                   -> Result<Rc<RefCell<Metric<metrics::Summary>>>, Error> {
        let m = Metric::new(Box::new(|| metrics::Summary::new()), name.clone(), help, labels);
        self.register(name, m)
    }

    pub fn gauge(&mut self, name: String, help: Option<String>, labels: Vec<String>)
                 -> Result<Rc<RefCell<Metric<metrics::Gauge>>>, Error> {
        let m = Metric::new(Box::new(|| metrics::Gauge::new()), name.clone(), help, labels);
        self.register(name, m)
    }

    pub fn histogram(&mut self, name: String, help: Option<String>, labels: Vec<String>, buckets: Vec<f64>)
                     -> Result<Rc<RefCell<Metric<metrics::Histogram>>>, Error> {
        let (buckets, bucket_labels) = helpers::histogram::buckets_to_counts(buckets);
        let constructor = move|| metrics::Histogram::new(buckets.clone(), bucket_labels.clone());
        let m = Metric::new(Box::new(constructor), name.clone(), help, labels);
        self.register(name, m)
    }

    pub fn register<T: Implementation + 'a>(&mut self, name: String, metric: Metric<T>)
                                            -> Result<Rc<RefCell<Metric<T>>>, Error> {
        let m = Rc::new(RefCell::new(metric));
        if self.metrics.contains_key(&name) {
            return Err(Error::MetricAlreadyExists);
        }
        self.metrics.insert(name, Rc::downgrade(&m) as Weak<RefCell<Collectable>>);
        Ok(m)
    }

    pub fn expose(&mut self, format: expose::Format) -> expose::Formatted {
        match format {
            expose::Format::Text => helpers::expose::text(self)
        }
    }
}


pub struct Metric<T> {
    name: String,
    help: Option<String>,
    labels: Vec<String>,
    constructor: Box<Fn() -> T>,
    storage: HashMap<u64, WithLabels<T>>,
}

impl <T: Implementation>Metric<T> {
    fn new(constructor: Box<Fn () -> T>, name: String, help: Option<String>, labels: Vec<String>) -> Metric<T> {
        Metric {
            name: name,
            help: help,
            labels: labels,
            constructor: constructor,
            storage: HashMap::new(),
        }
    }

    pub fn labels(&mut self, labels: Labels) -> Result<&mut T, Error> {
        let hash = try!(helpers::hash_labels(&self.labels, labels));
        let constructor = &self.constructor;
        let res = &mut self.storage.entry(hash)
            .or_insert_with(|| {
                let labels = labels.iter()
                    .map(|&(k, v)| (k.to_string(), v.to_string()))
                    .collect::<Vec<(String, String)>>();
                WithLabels::new(constructor(), labels)
            }).implementation;
        Ok(res)
    }
}

struct WithLabels<T> {
    implementation: T,
    labels: Vec<(String, String)>
}

impl <T>WithLabels<T> {
    fn new(implementation: T, labels: Vec<(String, String)>) -> WithLabels<T> {
        WithLabels { implementation: implementation, labels: labels }
    }
}

struct Descr<'a> {
    name: &'a String,
    help: &'a Option<String>,
    metric_type: &'a str,
}

struct Value<'a> {
    labels: &'a Vec<(String, String)>,
    collected: implementation::Collected,
}

trait Collectable {
    fn descr(&self) -> Descr;
    fn values(&self) -> Vec<Value>;
}

impl <T: Implementation>Collectable for Metric<T> {
    fn descr(&self) -> Descr {
        Descr {
            name: &self.name,
            help: &self.help,
            metric_type: T::metric_type(),
        }
    }
    fn values(&self) -> Vec<Value> {
        self.storage.values().map(|v| {
            Value {
                labels: &v.labels,
                collected: v.implementation.collect()
            }
        }).collect::<Vec<Value>>()
    }
}

pub type Labels<'a> = &'a[(&'a str, &'a str)];

#[derive(Debug)]
pub enum Error {
    InconsistentLabels(String),
    MetricAlreadyExists
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::InconsistentLabels(ref reason) => write!(f, "Passed labels don't consistent with metric's declared metrics: {}", reason),
            Error::MetricAlreadyExists => write!(f, "Metric with this name already exists")
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::InconsistentLabels(_) => "InconsistentLabels",
            Error::MetricAlreadyExists => "Already exists"
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn counter() {
        let mut registry = Registry::new();
        let counter = registry.counter("main".to_string(), None, vec!["path".to_string()]).unwrap();
        {
            let mut counter = counter.borrow_mut();
            let mut c1 = counter.labels(&[("path", "root")]).unwrap();
            c1.inc();
            c1.inc();
        }
        let res = String::from_utf8(registry.expose(expose::Format::Text).body).unwrap();
        assert_eq!("# TYPE main counter\n\
                    main{path=\"root\"} 2\n", res)
    }

    #[test]
    fn histogram() {
        let mut registry = Registry::new();
        let histogram = registry.histogram("main".to_string(), None, vec!["path".to_string()], vec![1.0, 5.0, 10.0, 50.0, 100.0, 300.0]).unwrap();
        {
            let mut histogram = histogram.borrow_mut();
            let mut c1 = histogram.labels(&[("path", "root")]).unwrap();
            c1.observe(30.0);
            c1.observe(250.0);
        }
        let res = String::from_utf8(registry.expose(expose::Format::Text).body).unwrap();
        assert_eq!("# TYPE main histogram\n\
                    main{path=\"root\",le=\"1\"} 0\n\
                    main{path=\"root\",le=\"5\"} 0\n\
                    main{path=\"root\",le=\"10\"} 0\n\
                    main{path=\"root\",le=\"50\"} 1\n\
                    main{path=\"root\",le=\"100\"} 1\n\
                    main{path=\"root\",le=\"300\"} 2\n\
                    main_sum{path=\"root\"} 280\n\
                    main_count{path=\"root\"} 2\n", res);
    }

    #[test]
    fn summary() {
        let mut registry = Registry::new();
        let summary = registry.summary("main".to_string(), None, vec!["path".to_string()]).unwrap();
        {
            let mut summary = summary.borrow_mut();
            let mut c1 = summary.labels(&[("path", "root")]).unwrap();
            c1.observe(30.0);
            c1.observe(250.0);
        }
        let res = String::from_utf8(registry.expose(expose::Format::Text).body).unwrap();
        assert_eq!("# TYPE main summary\n\
                    main_sum{path=\"root\"} 280\n\
                    main_count{path=\"root\"} 2\n", res);
    }
}