witchcraft-metrics

A general-purpose metrics library

5 releases (2 stable)

1.0.1 Jul 19, 2022
1.0.0 Mar 16, 2022
0.2.0 Dec 1, 2020
0.1.1 Jan 21, 2020
0.1.0 Nov 21, 2019

#137 in Profiling

Download history 1721/week @ 2024-07-22 1684/week @ 2024-07-29 1808/week @ 2024-08-05 1732/week @ 2024-08-12 1421/week @ 2024-08-19 1282/week @ 2024-08-26 1423/week @ 2024-09-02 1335/week @ 2024-09-09 1577/week @ 2024-09-16 1446/week @ 2024-09-23 1573/week @ 2024-09-30 801/week @ 2024-10-07 76/week @ 2024-10-14 28/week @ 2024-10-21 15/week @ 2024-10-28 27/week @ 2024-11-04

146 downloads per month
Used in 4 crates (3 directly)

Apache-2.0

46KB
979 lines

A general-purpose metrics library.

The design of the crate is based fairly closely off of the Dropwizard Metrics library from the Java ecosystem.

Examples

use witchcraft_metrics::{MetricRegistry, MetricId, Metric};
use std::time::Duration;

// A `MetricRegistry` stores metrics.
let registry = MetricRegistry::new();

// Metrics are identified by an ID, which consists of a name and set of "tags"
let yaks_shaved = registry.counter(MetricId::new("shavings").with_tag("animal", "yak"));
// You can also pass a string directly for metric IDs that don't have tags
let request_timer = registry.timer("server.requests");

// do some work and record some values.
for yak in find_some_yaks() {
    shave_yak(yak);
    yaks_shaved.inc();
}

// Grab a snapshot of the metrics currently registered and print their values:
for (id, metric) in &registry.metrics() {
    match metric {
        Metric::Counter(counter) => println!("{:?} is a counter with value {}", id, counter.count()),
        Metric::Timer(timer) => {
            let nanos = timer.snapshot().value(0.99);
            let duration = Duration::from_nanos(nanos as u64);
            println!("{:?} is a timer with 99th percentile {:?}", id, duration);
        }
        _ => {}
    }
}

Dependencies

~1–5.5MB
~30K SLoC