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
//! Static metrics are used to define metrics that share a single persistent metrics scope.
//! Because the scope never changes (it is "global"), all that needs to be provided by the
//! application is the metrics values.
//!
//! Compared to [ScopeMetrics], static metrics are easier to use and provide satisfactory metrics
//! in many applications.
//!
//! If multiple [AppMetrics] are defined, they'll each have their scope.
//!
use core::*;
use namespace::*;
use cache::*;
use async_queue::*;
use sample::*;
use core::Kind::*;

use std::sync::Arc;
use std::time::Duration;
use schedule::*;

// TODO define an 'AsValue' trait + impl for supported number types, then drop 'num' crate
pub use num::ToPrimitive;

/// Wrap the metrics backend to provide an application-friendly interface.
/// Open a metric scope to share across the application.
#[deprecated(since = "0.5.0", note = "Use `app_metrics` instead.")]
pub fn metrics<M, IC>(chain: IC) -> AppMetrics<M>
    where
        M: Clone + Send + Sync + 'static,
        IC: Into<Chain<M>>,
{
    app_metrics(chain)
}


/// Wrap the metrics backend to provide an application-friendly interface.
/// Open a metric scope to share across the application.
pub fn app_metrics<M, IC>(chain: IC) -> AppMetrics<M>
where
    M: Clone + Send + Sync + 'static,
    IC: Into<Chain<M>>,
{
    let chain = chain.into();
    let static_scope = chain.open_scope(false);
    AppMetrics {
        scope: static_scope,
        chain: Arc::new(chain),
    }
}

impl<M> From<Chain<M>> for AppMetrics<M> {
    fn from(chain: Chain<M>) -> AppMetrics<M> {
        let static_scope = chain.open_scope(false);
        AppMetrics {
            scope: static_scope,
            chain: Arc::new(chain),
        }
    }
}

/// A monotonic counter metric.
/// Since value is only ever increased by one, no value parameter is provided,
/// preventing programming errors.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct AppMarker<M> {
    metric: M,
    #[derivative(Debug = "ignore")] scope: ControlScopeFn<M>,
}

impl<M> AppMarker<M> {
    /// Record a single event occurence.
    pub fn mark(&self) {
        self.scope.write(&self.metric, 1);
    }
}

/// A counter that sends values to the metrics backend
#[derive(Derivative)]
#[derivative(Debug)]
pub struct AppCounter<M> {
    metric: M,
    #[derivative(Debug = "ignore")] scope: ControlScopeFn<M>,
}

impl<M> AppCounter<M> {
    /// Record a value count.
    pub fn count<V>(&self, count: V)
    where
        V: ToPrimitive,
    {
        self.scope.write(&self.metric, count.to_u64().unwrap());
    }
}

/// A gauge that sends values to the metrics backend
#[derive(Derivative)]
#[derivative(Debug)]
pub struct AppGauge<M> {
    metric: M,
    #[derivative(Debug = "ignore")] scope: ControlScopeFn<M>,
}

impl<M> AppGauge<M> {
    /// Record a value point for this gauge.
    pub fn value<V>(&self, value: V)
    where
        V: ToPrimitive,
    {
        self.scope.write(&self.metric, value.to_u64().unwrap());
    }
}

/// A timer that sends values to the metrics backend
/// Timers can record time intervals in multiple ways :
/// - with the time! macrohich wraps an expression or block with start() and stop() calls.
/// - with the time(Fn) methodhich wraps a closure with start() and stop() calls.
/// - with start() and stop() methodsrapping around the operation to time
/// - with the interval_us() method, providing an externally determined microsecond interval
#[derive(Derivative)]
#[derivative(Debug)]
pub struct AppTimer<M> {
    metric: M,
    #[derivative(Debug = "ignore")] scope: ControlScopeFn<M>,
}

impl<M> AppTimer<M> {
    /// Record a microsecond interval for this timer
    /// Can be used in place of start()/stop() if an external time interval source is used
    pub fn interval_us<V>(&self, interval_us: V) -> V
    where
        V: ToPrimitive,
    {
        self.scope.write(&self.metric, interval_us.to_u64().unwrap());
        interval_us
    }

    /// Obtain a opaque handle to the current time.
    /// The handle is passed back to the stop() method to record a time interval.
    /// This is actually a convenience method to the TimeHandle::now()
    /// Beware, handles obtained here are not bound to this specific timer instance
    /// _for now_ but might be in the future for safety.
    /// If you require safe multi-timer handles, get them through TimeType::now()
    pub fn start(&self) -> TimeHandle {
        TimeHandle::now()
    }

    /// Record the time elapsed since the start_time handle was obtained.
    /// This call can be performed multiple times using the same handle,
    /// reporting distinct time intervals each time.
    /// Returns the microsecond interval value that was recorded.
    pub fn stop(&self, start_time: TimeHandle) -> u64 {
        let elapsed_us = start_time.elapsed_us();
        self.interval_us(elapsed_us)
    }

    /// Record the time taken to execute the provided closure
    pub fn time<F, R>(&self, operations: F) -> R
    where
        F: FnOnce() -> R,
    {
        let start_time = self.start();
        let value: R = operations();
        self.stop(start_time);
        value
    }
}

/// Variations of this should also provide control of the metric recording scope.
#[derive(Derivative, Clone)]
#[derivative(Debug)]
pub struct AppMetrics<M> {
    chain: Arc<Chain<M>>,
    #[derivative(Debug = "ignore")] scope: ControlScopeFn<M>,
}

impl<M> AppMetrics<M>
where
    M: Clone + Send + Sync + 'static,
{
    /// Get an event counter of the provided name.
    pub fn marker<AS: AsRef<str>>(&self, name: AS) -> AppMarker<M> {
        let metric = self.chain.define_metric(Marker, name.as_ref(), 1.0);
        AppMarker {
            metric,
            scope: self.scope.clone(),
        }
    }

    /// Get a counter of the provided name.
    pub fn counter<AS: AsRef<str>>(&self, name: AS) -> AppCounter<M> {
        let metric = self.chain.define_metric(Counter, name.as_ref(), 1.0);
        AppCounter {
            metric,
            scope: self.scope.clone(),
        }
    }

    /// Get a timer of the provided name.
    pub fn timer<AS: AsRef<str>>(&self, name: AS) -> AppTimer<M> {
        let metric = self.chain.define_metric(Timer, name.as_ref(), 1.0);
        AppTimer {
            metric,
            scope: self.scope.clone(),
        }
    }

    /// Get a gauge of the provided name.
    pub fn gauge<AS: AsRef<str>>(&self, name: AS) -> AppGauge<M> {
        let metric = self.chain.define_metric(Gauge, name.as_ref(), 1.0);
        AppGauge {
            metric,
            scope: self.scope.clone(),
        }
    }

    /// Forcefully flush the backing metrics scope.
    /// This is usually not required since static metrics use auto flushing scopes.
    /// The effect, if any, of this method depends on the selected metrics backend.
    pub fn flush(&self) {
        self.scope.flush();
    }

    /// Schedule for the metrics aggregated of buffered by downstream metrics sinks to be
    /// sent out at regular intervals.
    pub fn flush_every(&self, period: Duration) -> CancelHandle {
        let scope = self.scope.clone();
        schedule(period, move || scope.flush())
    }
}

impl<M: Send + Sync + Clone + 'static> WithNamespace for AppMetrics<M> {
    fn with_name<IN: Into<Namespace>>(&self, names: IN) -> Self {
        AppMetrics {
            chain: Arc::new(self.chain.with_name(names)),
            scope: self.scope.clone(),
        }
    }
}

impl<M: Send + Sync + Clone + 'static> WithCache for AppMetrics<M> {
    fn with_cache(&self, cache_size: usize) -> Self {
        AppMetrics {
            chain: Arc::new(self.chain.with_cache(cache_size)),
            scope: self.scope.clone(),
        }
    }
}

impl<M: Send + Sync + Clone + 'static> WithSamplingRate for AppMetrics<M> {
    fn with_sampling_rate(&self, sampling_rate: Rate) -> Self {
        AppMetrics {
            chain: Arc::new(self.chain.with_sampling_rate(sampling_rate)),
            scope: self.scope.clone(),
        }
    }
}

impl<M: Send + Sync + Clone + 'static> WithAsyncQueue for AppMetrics<M> {
    fn with_async_queue(&self, queue_size: usize) -> Self {
        AppMetrics {
            chain: Arc::new(self.chain.with_async_queue(queue_size)),
            scope: self.scope.clone(),
        }
    }
}

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

    use ::*;
    use test;

    #[bench]
    fn time_bench_direct_dispatch_event(b: &mut test::Bencher) {
        let sink = aggregate(summary, to_void());
        let metrics = app_metrics(sink);
        let marker = metrics.marker("aaa");
        b.iter(|| test::black_box(marker.mark()));
    }

}