Metrics is a batteries-included instrumentation ecosystem for Rust, allowing you to quickly and easily instrument your libraries and applications.
With a pay-for-what-you-use approach, Metrics has nearly zero cost when not in use, and is fully optimized for the lowest possible overhead when enabled. Confidently instrument your application without fear of production impact.
Instrumentation should be easy to integrate and easy to use. Metrics provides an opinionated API to help you avoid wasting time when getting started, without locking you into a specific monitoring system or time-series database. Stop wasting time sorting through and implementing different instrumentation libraries: use Metrics, and do it once.
Metrics provides a common framework for instrumentation. Using a Metrics-instrumented library in your application? Automatically get access to those metrics with no extra configuration. The more libraries that use Metrics, the richer the instrumentation gets for the entire Rust ecosystem. Use an instrumentation library that rewards collaboration.
Here is a code snippet to show how to use the metrics crate both as a library and application author.
1 use std::time::Instant; 2 use metrics::{counter, histogram}; 3 use metrics_exporter_prometheus::PrometheusBuilder; 4 5 // It's easy to instrument code: 6 fn process_request() { 7 counter!("requests_received").increment(1); 8 9 let start = Instant::now(); 10 11 // Do some work here... 12 13 histogram!("process_request_duration").record(start.elapsed()); 14 } 15 16 // And it's easy to start collecting metrics: 17 fn main() { 18 // Run a Prometheus scrape endpoint on 127.0.0.1:9000. 19 let _ = PrometheusBuilder::new() 20 .install() 21 .expect("failed to install prometheus exporter"); 22 } 23NORMALsrc/main.rsrustutf-8[unix]100% ☰ 23/23 ln : 1