| Crates.io | metrics-helper-macros |
| lib.rs | metrics-helper-macros |
| version | 0.1.5 |
| created_at | 2026-01-06 17:05:59.993274+00 |
| updated_at | 2026-01-08 15:56:22.727654+00 |
| description | Proc-macros for idiomatic Prometheus metrics instrumentation in Rust |
| homepage | |
| repository | https://github.com/jdcasale/metrics-helper |
| max_upload_size | |
| id | 2026306 |
| size | 61,399 |
Proc-macros for idiomatic Prometheus metrics instrumentation in Rust.
#[instrument] - Attribute macro for automatic function instrumentationResult[dependencies]
metrics-helper-macros = "0.1"
metrics = "0.24"
use metrics_helper_macros::instrument;
#[instrument(
counter = "db_queries_total",
histogram = "db_query_duration_seconds",
error_counter = "db_query_errors_total",
)]
async fn query_database() -> Result<Data, DbError> {
// Your code here...
}
Labels can be static (fixed values) or dynamic (captured from function parameters):
#[instrument(
counter = "http_requests_total",
histogram = "http_request_duration_seconds",
labels(
service = "api", // static label
method, // dynamic: captured from function param
endpoint, // dynamic: captured from function param
),
)]
async fn handle_request(method: &str, endpoint: &str, body: Bytes) -> Response {
// method and endpoint values are captured as label values
}
This produces metrics like:
http_requests_total{service="api", method="GET", endpoint="/users"} 1
http_request_duration_seconds{service="api", method="GET", endpoint="/users"} 0.023
| Attribute | Description |
|---|---|
counter |
Counter to increment on each call |
histogram |
Histogram to record call duration (in seconds) |
error_counter |
Counter to increment when function returns Err |
labels(...) |
Labels to attach to all metrics |
key = "value" - Fixed string valuekey - Captures the value of a function parameter with the same name (must implement Display)Displaymetrics crate must be available in your dependency treeerror_counter to work, the function must return a Result typeMIT