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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std::fmt;
use std::iter;
use std::sync::{Arc, Weak};
use std::time::Instant;

use {Collect, ErrorKind, Registry, Result};
use default_registry;
use atomic::{AtomicF64, AtomicU64};
use label::{Label, Labels, LabelsMut};
use metric::{Metric, MetricName, MetricValue};
use timestamp::{self, Timestamp, TimestampMut};

/// `Counter` is a cumulative metric that represents a single numerical value that only ever goes up.
///
/// Cloned counters share the same value.
///
/// # Examples
///
/// ```
/// use prometrics::metrics::CounterBuilder;
///
/// let mut counter = CounterBuilder::new("foo_total").namespace("example").finish().unwrap();
/// assert_eq!(counter.metric_name().to_string(), "example_foo_total");
/// assert_eq!(counter.value(), 0.0);
///
/// counter.increment();
/// assert_eq!(counter.value(), 1.0);
/// ```
#[derive(Debug, Clone)]
pub struct Counter(Arc<Inner>);
impl Counter {
    /// Makes a new `Counter` instance.
    ///
    /// Note that it is recommended to create this via `CounterBuilder`.
    pub fn new(name: &str) -> Result<Self> {
        CounterBuilder::new(name).finish()
    }

    /// Returns the name of this counter.
    pub fn metric_name(&self) -> &MetricName {
        &self.0.name
    }

    /// Returns the help of this counter.
    pub fn help(&self) -> Option<&str> {
        self.0.help.as_ref().map(|h| h.as_ref())
    }

    /// Returns the labels of this counter.
    pub fn labels(&self) -> &Labels {
        &self.0.labels
    }

    /// Returns the mutable labels of this counter.
    pub fn labels_mut(&mut self) -> LabelsMut {
        LabelsMut::new(&self.0.labels, None)
    }

    /// Returns the timestamp of this counter.
    pub fn timestamp(&self) -> &Timestamp {
        &self.0.timestamp
    }

    /// Returns the mutable timestamp of this counter.
    pub fn timestamp_mut(&mut self) -> TimestampMut {
        TimestampMut::new(&self.0.timestamp)
    }

    /// Returns the value of this counter.
    #[inline]
    pub fn value(&self) -> f64 {
        self.0.value.get()
    }

    /// Increments this counter.
    #[inline]
    pub fn increment(&self) {
        self.0.value.increment()
    }

    /// Adds `count` to this counter.
    #[inline]
    pub fn add(&self, count: f64) -> Result<()> {
        track_assert!(count >= 0.0, ErrorKind::InvalidInput, "count={}", count);
        self.0.value.add(count);
        Ok(())
    }

    /// Adds `count` to this counter.
    #[inline]
    pub fn add_u64(&self, count: u64) {
        self.0.value.add_u64(count);
    }

    /// Measures the exeuction time of `f` and adds its duration to the counter in seconds.
    #[inline]
    pub fn time<F, T>(&self, f: F) -> T
    where
        F: FnOnce() -> T,
    {
        let start = Instant::now();
        let result = f();
        let elapsed = timestamp::duration_to_seconds(start.elapsed());
        self.add(elapsed).expect("Never fails");
        result
    }

    /// Returns a collector for this counter.
    pub fn collector(&self) -> CounterCollector {
        CounterCollector(Arc::downgrade(&self.0))
    }
}
impl fmt::Display for Counter {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.metric_name())?;
        if !self.labels().is_empty() {
            write!(f, "{}", self.labels())?;
        }
        write!(f, " {}", MetricValue(self.value()))?;
        if let Some(timestamp) = self.timestamp().get() {
            write!(f, " {}", timestamp)?;
        }
        Ok(())
    }
}

/// `Counter` builder.
#[derive(Debug)]
pub struct CounterBuilder {
    namespace: Option<String>,
    subsystem: Option<String>,
    name: String,
    help: Option<String>,
    labels: Vec<(String, String)>,
    registries: Vec<Registry>,
}
impl CounterBuilder {
    /// Makes a builder for counters named `name`.
    pub fn new(name: &str) -> Self {
        CounterBuilder {
            namespace: None,
            subsystem: None,
            name: name.to_string(),
            help: None,
            labels: Vec::new(),
            registries: Vec::new(),
        }
    }

    /// Sets the namespace part of the metric name of this.
    pub fn namespace(&mut self, namespace: &str) -> &mut Self {
        self.namespace = Some(namespace.to_string());
        self
    }

    /// Sets the subsystem part of the metric name of this.
    pub fn subsystem(&mut self, subsystem: &str) -> &mut Self {
        self.subsystem = Some(subsystem.to_string());
        self
    }

    /// Sets the help of this.
    pub fn help(&mut self, help: &str) -> &mut Self {
        self.help = Some(help.to_string());
        self
    }

    /// Adds a label.
    ///
    /// Note that `name` will be validated in the invocation of the `finish` method.
    pub fn label(&mut self, name: &str, value: &str) -> &mut Self {
        self.labels.retain(|l| l.0 != name);
        self.labels.push((name.to_string(), value.to_string()));
        self.labels.sort();
        self
    }

    /// Adds a registry to which the resulting counters will be registered.
    pub fn registry(&mut self, registry: Registry) -> &mut Self {
        self.registries.push(registry);
        self
    }

    /// Adds the default registry.
    pub fn default_registry(&mut self) -> &mut Self {
        self.registry(default_registry())
    }

    /// Builds a counter.
    ///
    /// # Errors
    ///
    /// This method will return `Err(_)` if any of the name of the metric or labels is malformed.
    pub fn finish(&self) -> Result<Counter> {
        let name = track!(MetricName::new(
            self.namespace.as_ref().map(AsRef::as_ref),
            self.subsystem.as_ref().map(AsRef::as_ref),
            &self.name,
        ))?;
        let labels = track!(
            self.labels
                .iter()
                .map(|&(ref name, ref value)| track!(Label::new(name, value)))
                .collect::<Result<_>>()
        )?;
        let inner = Inner {
            name,
            labels: Labels::new(labels),
            help: self.help.clone(),
            timestamp: Timestamp::new(),
            value: Value::new(),
        };
        let counter = Counter(Arc::new(inner));
        for r in &self.registries {
            r.register(counter.collector());
        }
        Ok(counter)
    }
}

/// `Collect` trait implmentation for `Counter`.
#[derive(Debug)]
pub struct CounterCollector(Weak<Inner>);
impl Collect for CounterCollector {
    type Metrics = iter::Once<Metric>;
    fn collect(&mut self) -> Option<Self::Metrics> {
        self.0
            .upgrade()
            .map(|inner| iter::once(Metric::Counter(Counter(inner))))
    }
}

#[derive(Debug)]
struct Inner {
    name: MetricName,
    labels: Labels,
    help: Option<String>,
    timestamp: Timestamp,
    value: Value,
}

#[derive(Debug)]
struct Value {
    f64: AtomicF64,
    u64: AtomicU64,
}
impl Value {
    fn new() -> Self {
        Value {
            f64: AtomicF64::new(0.0),
            u64: AtomicU64::new(0),
        }
    }

    #[inline]
    fn get(&self) -> f64 {
        self.f64.get() + self.u64.get() as f64
    }

    #[inline]
    fn increment(&self) {
        self.u64.inc();
    }

    #[inline]
    fn add(&self, count: f64) {
        let floor = count.floor() as u64;
        let ceil = count.ceil() as u64;
        if floor == ceil {
            self.u64.add(floor);
        } else {
            self.f64.update(|v| v + count);
        }
    }

    #[inline]
    fn add_u64(&self, count: u64) {
        self.u64.add(count);
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn it_works() {
        let mut counter = track_try_unwrap!(
            CounterBuilder::new("foo_total")
                .namespace("test")
                .subsystem("counter")
                .finish()
        );
        assert_eq!(counter.metric_name().to_string(), "test_counter_foo_total");
        assert_eq!(counter.value(), 0.0);

        counter.increment();
        assert_eq!(counter.value(), 1.0);

        counter.add(3.45).unwrap();
        assert_eq!(counter.value(), 4.45);

        counter.add(2.0).unwrap();
        assert_eq!(counter.value(), 6.45);

        counter.add_u64(2);
        assert_eq!(counter.value(), 8.45);

        assert_eq!(counter.to_string(), "test_counter_foo_total 8.45");
        counter.labels_mut().insert("bar", "baz").unwrap();
        assert_eq!(
            counter.to_string(),
            r#"test_counter_foo_total{bar="baz"} 8.45"#
        );
    }
}