basics/
basics.rs

1//! An app demonstrating the basics of the metrics front-end.
2//! Defines metrics of each kind and use them to print values to the console in multiple ways.
3
4use dipstick::{Input, InputScope, Prefixed, Stream, time};
5use std::io;
6use std::thread::sleep;
7use std::time::Duration;
8
9fn main() {
10    // for this demo, print metric values to the console
11    let app_metrics = Stream::write_to(io::stdout()).metrics();
12
13    // metrics can be predefined by type and name
14    let counter = app_metrics.counter("counter_a");
15    let level = app_metrics.level("level_a");
16    let timer = app_metrics.timer("timer_b");
17
18    // metrics can also be declared and used ad-hoc (use output.cache() if this happens often)
19    app_metrics.counter("just_once").count(4);
20
21    // metric names can be prepended with a common prefix
22    let prefixed_metrics = app_metrics.named("subsystem");
23    let event = prefixed_metrics.marker("event_c");
24    let gauge = prefixed_metrics.gauge("gauge_d");
25
26    // each kind of metric has a different method name to prevent errors
27    counter.count(11);
28    level.adjust(-4);
29    level.adjust(5);
30
31    gauge.value(22);
32    gauge.value(-24);
33
34    event.mark();
35
36    // time can be measured multiple equivalent ways:
37    // in microseconds (used internally)
38    timer.interval_us(35573);
39
40    // using the time! macro
41    time!(timer, sleep(Duration::from_millis(5)));
42
43    // using a closure
44    timer.time(|| sleep(Duration::from_millis(5)));
45
46    // using a "time handle"
47    let start_time = timer.start();
48    sleep(Duration::from_millis(5));
49    timer.stop(start_time);
50}