GitHub_headerImage

Documentation Crates.io Discord Shield

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.

Example

```rust use autometrics::autometrics; use axum::{http::StatusCode, routing::*, Router, Server};

// 1. Instrument your functions with metrics

[autometrics]

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

[tokio::main]

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()); } ```

Features

See Why Autometrics? for more details on the ideas behind autometrics.

Identifying commits that introduced problems

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 | "" |

Using vergen to set the Git details

```toml

Cargo.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"); }

Configuring Autometrics

Custom Prometheus URL

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}"); } ```

Feature flags

Metrics Libraries

Configure the crate that autometrics will use to produce metrics by using one of the following feature flags: