#metrics #prometheus #open-telemetry #query #axum-server

autometrics

Easily add metrics to your code that actually help you spot and debug issues in production. Built on Prometheus and OpenTelemetry.

16 releases (2 stable)

1.0.1 Feb 12, 2024
1.0.0 Dec 1, 2023
0.6.0 Aug 8, 2023
0.5.0 Jun 2, 2023
0.3.2 Mar 22, 2023

#30 in Debugging

Download history 2406/week @ 2024-01-19 2494/week @ 2024-01-26 3228/week @ 2024-02-02 2748/week @ 2024-02-09 2405/week @ 2024-02-16 2920/week @ 2024-02-23 2805/week @ 2024-03-01 2980/week @ 2024-03-08 2922/week @ 2024-03-15 2970/week @ 2024-03-22 2268/week @ 2024-03-29 2833/week @ 2024-04-05 2913/week @ 2024-04-12 2805/week @ 2024-04-19 3111/week @ 2024-04-26 3823/week @ 2024-05-03

13,408 downloads per month
Used in 6 crates

MIT/Apache

125KB
2K SLoC

GitHub_headerImage

Documentation Crates.io Discord Shield

Metrics are a powerful and cost-efficient tool for understanding the health and performance of your code in production. But it's hard to decide what metrics to track and even harder to write queries to understand the data.

Autometrics provides a macro that makes it trivial to instrument any function with the most useful metrics: request rate, error rate, and latency. It standardizes these metrics and then generates powerful Prometheus queries based on your function details to help you quickly identify and debug issues in production.

Benefits

  • #[autometrics] macro adds useful metrics to any function or impl block, without you thinking about what metrics to collect
  • 💡 Generates powerful Prometheus queries to help quickly identify and debug issues in production
  • 🔗 Injects links to live Prometheus charts directly into each function's doc comments
  • 📊 Grafana dashboards work without configuration to visualize the performance of functions & SLOs
  • 🔍 Correlates your code's version with metrics to help identify commits that introduced errors or latency
  • 📏 Standardizes metrics across services and teams to improve debugging
  • ⚖️ Function-level metrics provide useful granularity without exploding cardinality
  • ⚡ Minimal runtime overhead

Advanced Features

See autometrics.dev for more details on the ideas behind autometrics.

Example Axum App

Autometrics isn't tied to any web framework, but this shows how you can use the library in an Axum server.

use std::error::Error;
use autometrics::{autometrics, prometheus_exporter};
use axum::{routing::*, Router};
use std::net::Ipv4Addr;
use tokio::net::TcpListener;

// Instrument your functions with metrics
#[autometrics]
pub async fn create_user() -> Result<(), ()> {
    Ok(())
}

// Export the metrics to Prometheus
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
    prometheus_exporter::init();

    let app = Router::new()
        .route("/users", post(create_user))
        .route(
            "/metrics",
            get(|| async { prometheus_exporter::encode_http_response() }),
        );


    let listener = TcpListener::bind((Ipv4Addr::from([127, 0, 0, 1]), 0)).await?;
    axum::serve(listener, app).await?;
    Ok(())
}

Quickstart

See the Github repo README to quickly add autometrics to your project.

Contributing

Issues, feature suggestions, and pull requests are very welcome!

If you are interested in getting involved:

Dependencies

~3–20MB
~285K SLoC