Crates.io | autometrics |
lib.rs | autometrics |
version | 2.0.0 |
source | src |
created_at | 2023-01-27 09:21:45.462831 |
updated_at | 2024-07-25 13:46:28.625563 |
description | Easily add metrics to your code that actually help you spot and debug issues in production. Built on Prometheus and OpenTelemetry. |
homepage | https://autometrics.dev |
repository | https://github.com/autometrics-dev/autometrics-rs |
max_upload_size | |
id | 769519 |
size | 158,155 |
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.
#[autometrics]
macro adds useful metrics to any function or impl
block, without you thinking about what metrics to collectopentelemetry
, prometheus
, prometheus-client
or metrics
)See autometrics.dev for more details on the ideas behind autometrics.
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(())
}
See the Github repo README to quickly add autometrics
to your project.
Issues, feature suggestions, and pull requests are very welcome!
If you are interested in getting involved: