Autometrics is an observability micro-framework built for developers.
The Rust library provides a macro that makes it easy to instrument any function with the most useful metrics: request rate, error rate, and latency. Autometrics uses instrumented function names to generate Prometheus queries so you don’t need to hand-write complicated PromQL.
To make it easy for you to spot and debug issues in production, Autometrics inserts links to live charts directly into each function’s doc comments and provides dashboards that work out of the box. It also enables you to create powerful alerts based on Service-Level Objectives (SLOs) directly in your source code. Lastly, Autometrics writes queries that correlate your software’s version info with anomalies in the metrics to help you quickly identify commits that introduced bugs or latency.
```rust use autometrics::autometrics; use axum::{http::StatusCode, routing::*, Router, Server};
// 1. Instrument your functions with metrics
pub async fn create_user() -> Result<(), ()> { Ok(()) }
// 2. Export your metrics to Prometheus pub async fn getmetrics() -> (StatusCode, String) { match autometrics::encodeglobalmetrics() { Ok(metrics) => (StatusCode::OK, metrics), Err(err) => (StatusCode::INTERNALSERVER_ERROR, format!("{:?}", err)) } }
// 3. Initialize the metrics collector in your main function
pub async fn main() { let exporter = autometrics::globalmetrics_exporter();
let app = Router::new() .route("/users", post(createuser)) .route("/metrics", get(getmetrics)); Server::bind(&([127, 0, 0, 1], 0).into()) .serve(app.intomakeservice()); } ```
#[autometrics]
macro instruments any function or impl
block to track the most useful metricsopentelemetry
, prometheus
, or metrics
)See Why Autometrics? for more details on the ideas behind autometrics.
Autometrics makes it easy to spot versions and commits that introduce errors or latency.
It produces a build_info
metric and uses the following labels to expose the version info of your app to Prometheus:
| Label | Compile-Time Environment Variables | Default |
|---|---|---|
| version
| AUTOMETRICS_VERSION
or CARGO_PKG_VERSION
| CARGO_PKG_VERSION
(set by cargo by default) |
| commit
| AUTOMETRICS_COMMIT
or VERGEN_GIT_COMMIT
| ""
|
| branch
| AUTOMETRICS_BRANCH
or VERGEN_GIT_BRANCH
| ""
|
vergen
to set the Git details```toml
[build-dependencies] vergen = { version = "8.1", features = ["git", "gitcl"] } ```
rust
// build.rs
fn main() {
vergen::EmitBuilder::builder()
.git_sha(true)
.git_branch()
.emit()
.expect("Unable to generate build info");
}
Autometrics inserts Prometheus query links into function documentation. By default, the links point to http://localhost:9090
but you can configure it to use a custom URL using an environment variable in your build.rs
file:
```rust // build.rs
fn main() { // Reload Rust analyzer after changing the Prometheus URL to regenerate the links let prometheusurl = "https://your-prometheus-url.example"; println!("cargo:rustc-env=PROMETHEUSURL={prometheus_url}"); } ```
prometheus-exporter
- exports a Prometheus metrics collector and exporter (compatible with any of the Metrics Libraries)custom-objective-latency
- by default, Autometrics only supports a fixed set of latency thresholds for objectives. Enable this to use custom latency thresholds. Note, however, that the custom latency must match one of the buckets configured for your histogram or the alerts will not work. This is not currently compatible with the prometheus
or prometheus-exporter
feature.custom-objective-percentile
by default, Autometrics only supports a fixed set of objective percentiles. Enable this to use a custom percentile. Note, however, that using custom percentiles requires generating a different recording and alerting rules file using the CLI + Sloth (see here).Configure the crate that autometrics will use to produce metrics by using one of the following feature flags:
opentelemetry
(enabled by default) - use the opentelemetry crate for producing metricsmetrics
- use the metrics crate for producing metricsprometheus
- use the prometheus crate for producing metrics