Struct dipstick::Timer

source ·
pub struct Timer { /* private fields */ }
Expand description

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

Implementations§

Record a microsecond interval for this timer Can be used in place of start()/stop() if an external time interval source is used

Examples found in repository?
examples/macro_proxy.rs (line 41)
35
36
37
38
39
40
41
42
43
44
45
fn main() {
    dipstick::Proxy::default_target(Stream::write_to_stdout().metrics());

    loop {
        ROOT_COUNTER.count(123);
        ANOTHER_COUNTER.count(456);
        ROOT_TIMER.interval_us(2000000);
        ROOT_GAUGE.value(34534);
        std::thread::sleep(Duration::from_millis(40));
    }
}
More examples
Hide additional examples
examples/graphite.rs (line 14)
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    let metrics = Graphite::send_to("localhost:2003")
        .expect("Connected")
        .named("my_app")
        .metrics();

    loop {
        metrics.counter("counter_a").count(123);
        metrics.timer("timer_a").interval_us(2000000);
        std::thread::sleep(Duration::from_millis(40));
    }
}
examples/prometheus_labels.rs (line 17)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    let metrics = Prometheus::push_to("http://localhost:9091/metrics/job/prometheus_example")
        .expect("Prometheus Socket")
        .named("my_app")
        .metrics();

    AppLabel::set("abc", "456");
    ThreadLabel::set("xyz", "123");

    loop {
        metrics.counter("counter_a").count(123);
        metrics.timer("timer_a").interval_us(2000000);
        std::thread::sleep(Duration::from_millis(40));
    }
}
examples/bucket2stdout.rs (line 25)
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
fn main() {
    let metrics = AtomicBucket::new().named("test");

    metrics.drain(Stream::write_to_stdout());

    metrics.flush_every(Duration::from_secs(3));

    let counter = metrics.counter("counter_a");
    let timer = metrics.timer("timer_a");
    let gauge = metrics.gauge("gauge_a");
    let marker = metrics.marker("marker_a");

    loop {
        // add counts forever, non-stop
        counter.count(11);
        counter.count(12);
        counter.count(13);

        timer.interval_us(11_000_000);
        timer.interval_us(12_000_000);
        timer.interval_us(13_000_000);

        gauge.value(11);
        gauge.value(12);
        gauge.value(13);

        marker.mark();
    }
}
examples/bucket_summary.rs (line 24)
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
fn main() {
    let app_metrics = AtomicBucket::new();
    app_metrics.drain(Stream::write_to_stdout());

    app_metrics.flush_every(Duration::from_secs(3));

    let counter = app_metrics.counter("counter_a");
    let timer = app_metrics.timer("timer_a");
    let gauge = app_metrics.gauge("gauge_a");
    let marker = app_metrics.marker("marker_a");

    loop {
        // add counts forever, non-stop
        counter.count(11);
        counter.count(12);
        counter.count(13);

        timer.interval_us(11_000_000);
        timer.interval_us(12_000_000);
        timer.interval_us(13_000_000);

        gauge.value(11);
        gauge.value(12);
        gauge.value(13);

        marker.mark();
    }
}
examples/multi_input.rs (line 22)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    // will output metrics to graphite and to stdout
    let different_type_metrics = MultiInput::new()
        .add_target(Graphite::send_to("localhost:2003").expect("Connecting"))
        .add_target(Stream::write_to_stdout())
        .metrics();

    // will output metrics twice, once with "both.yeah" prefix and once with "both.ouch" prefix.
    let same_type_metrics = MultiInput::new()
        .add_target(Stream::write_to_stderr().named("yeah"))
        .add_target(Stream::write_to_stderr().named("ouch"))
        .named("both")
        .metrics();

    loop {
        different_type_metrics.counter("counter_a").count(123);
        same_type_metrics.timer("timer_a").interval_us(2000000);
        std::thread::sleep(Duration::from_millis(400));
    }
}

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()

Examples found in repository?
examples/basics.rs (line 47)
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
fn main() {
    // for this demo, print metric values to the console
    let app_metrics = Stream::write_to(io::stdout()).metrics();

    // metrics can be predefined by type and name
    let counter = app_metrics.counter("counter_a");
    let level = app_metrics.level("level_a");
    let timer = app_metrics.timer("timer_b");

    // metrics can also be declared and used ad-hoc (use output.cache() if this happens often)
    app_metrics.counter("just_once").count(4);

    // metric names can be prepended with a common prefix
    let prefixed_metrics = app_metrics.named("subsystem");
    let event = prefixed_metrics.marker("event_c");
    let gauge = prefixed_metrics.gauge("gauge_d");

    // each kind of metric has a different method name to prevent errors
    counter.count(11);
    level.adjust(-4);
    level.adjust(5);

    gauge.value(22);
    gauge.value(-24);

    event.mark();

    // time can be measured multiple equivalent ways:
    // in microseconds (used internally)
    timer.interval_us(35573);

    // using the time! macro
    time!(timer, sleep(Duration::from_millis(5)));

    // using a closure
    timer.time(|| sleep(Duration::from_millis(5)));

    // using a "time handle"
    let start_time = timer.start();
    sleep(Duration::from_millis(5));
    timer.stop(start_time);
}

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.

Examples found in repository?
examples/basics.rs (line 49)
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
fn main() {
    // for this demo, print metric values to the console
    let app_metrics = Stream::write_to(io::stdout()).metrics();

    // metrics can be predefined by type and name
    let counter = app_metrics.counter("counter_a");
    let level = app_metrics.level("level_a");
    let timer = app_metrics.timer("timer_b");

    // metrics can also be declared and used ad-hoc (use output.cache() if this happens often)
    app_metrics.counter("just_once").count(4);

    // metric names can be prepended with a common prefix
    let prefixed_metrics = app_metrics.named("subsystem");
    let event = prefixed_metrics.marker("event_c");
    let gauge = prefixed_metrics.gauge("gauge_d");

    // each kind of metric has a different method name to prevent errors
    counter.count(11);
    level.adjust(-4);
    level.adjust(5);

    gauge.value(22);
    gauge.value(-24);

    event.mark();

    // time can be measured multiple equivalent ways:
    // in microseconds (used internally)
    timer.interval_us(35573);

    // using the time! macro
    time!(timer, sleep(Duration::from_millis(5)));

    // using a closure
    timer.time(|| sleep(Duration::from_millis(5)));

    // using a "time handle"
    let start_time = timer.start();
    sleep(Duration::from_millis(5));
    timer.stop(start_time);
}

Record the time taken to execute the provided closure

Examples found in repository?
examples/basics.rs (line 44)
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
fn main() {
    // for this demo, print metric values to the console
    let app_metrics = Stream::write_to(io::stdout()).metrics();

    // metrics can be predefined by type and name
    let counter = app_metrics.counter("counter_a");
    let level = app_metrics.level("level_a");
    let timer = app_metrics.timer("timer_b");

    // metrics can also be declared and used ad-hoc (use output.cache() if this happens often)
    app_metrics.counter("just_once").count(4);

    // metric names can be prepended with a common prefix
    let prefixed_metrics = app_metrics.named("subsystem");
    let event = prefixed_metrics.marker("event_c");
    let gauge = prefixed_metrics.gauge("gauge_d");

    // each kind of metric has a different method name to prevent errors
    counter.count(11);
    level.adjust(-4);
    level.adjust(5);

    gauge.value(22);
    gauge.value(-24);

    event.mark();

    // time can be measured multiple equivalent ways:
    // in microseconds (used internally)
    timer.interval_us(35573);

    // using the time! macro
    time!(timer, sleep(Duration::from_millis(5)));

    // using a closure
    timer.time(|| sleep(Duration::from_millis(5)));

    // using a "time handle"
    let start_time = timer.start();
    sleep(Duration::from_millis(5));
    timer.stop(start_time);
}

Methods from Deref<Target = InputMetric>§

Collect a new value for this metric.

Examples found in repository?
examples/raw_log.rs (line 16)
10
11
12
13
14
15
16
17
pub fn raw_write() {
    // setup dual metric channels
    let metrics_log = dipstick::Log::to_log().metrics();

    // define and send metrics using raw channel API
    let counter = metrics_log.new_metric("count_a".into(), dipstick::InputKind::Counter);
    counter.write(1, labels![]);
}

Returns the unique identifier of this metric.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.