Reporting metric sources
The Source trait represents some underlying data source that can be sampled to provide Metrics. You can sample sources directly, or combine them into a Reporter to sample all the sources of metrics in your application together:
#![allow(unused)]
fn main() {
extern crate emit;
use emit::metric::{Source as _, Sampler as _};
// Create some metric sources
let source_1 = emit::metric::source::from_fn(|sampler| {
sampler.metric(emit::count_metric!(
mdl: emit::path!("source_1"),
name: "bytes_written",
value: 1,
));
});
let source_2 = emit::metric::source::from_fn(|sampler| {
sampler.metric(emit::count_metric!(
mdl: emit::path!("source_2"),
name: "bytes_written",
value: 2,
));
});
// Collect them into a reporter
let mut reporter = emit::metric::Reporter::new();
reporter.add_source(source_1);
reporter.add_source(source_2);
// You'll probably want to run this task in your async runtime
// so it observes cancellation etc, but works for this illustration.
std::thread::spawn(move || {
loop {
// You could also use `sample_metrics` here instead of `emit_metrics`
// to do something else with the `Metric` values
reporter.emit_metrics(emit::runtime::shared());
std::thread::sleep(std::time::Duration::from_secs(30));
}
});
}
Normalization of timestamps
The Reporter type will attempt to normalize the extents of any metrics sampled from its sources. Normalization will:
- Take the current timestamp,
now, when sampling metrics. - If the metric sample has no extent, or has a point extent, it will be replaced with
now. - If the metric sample has a range extent, the end will be set to
nowand the start will benowminus the original length. If this would produce an invalid range then the original is kept.
When the std Cargo feature is enabled this will be done automatically. In other cases, normalization won’t happen unless it’s configured by Reporter::normalize_with_clock.
Normalization can be disabled by calling Reporter::without_normalization.