pub struct PrometheusMetrics { /* private fields */ }Expand description
In-memory Prometheus-format HTTP metrics collector.
This collector stores counters and bounded duration histograms in process
memory and renders Prometheus text exposition. It is useful for small
services, examples, and tests; it is not a durable metrics store and values
reset on process restart. The default exclusions are /health/live,
/health/ready, and /metrics.
§Label cardinality
By default the collector records every distinct route label it observes, so
the caller is responsible for keeping cardinality bounded. Prefer route
patterns (e.g. "/users/{id}") over concrete paths. To harden against
accidental high-cardinality labels (which would grow memory without bound in
a long-running process), apply PrometheusMetrics::with_max_series: once
the configured number of distinct route labels has been admitted, every
further distinct label collapses into a single "<overflow>" route.
use axum::{Router, routing::get};
use nidus_http::middleware::{PrometheusMetrics, route_metrics_layer};
let metrics = PrometheusMetrics::new();
let app = Router::new()
.route("/users/{id}", get(show_user))
.route_layer(route_metrics_layer("/users/{id}", metrics.clone()))
.merge(metrics.routes());Implementations§
Source§impl PrometheusMetrics
impl PrometheusMetrics
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a Prometheus metrics collector with default internal route exclusions.
The collector is unbounded by default (every distinct route label is
recorded); use Self::with_max_series to cap label cardinality.
Sourcepub fn exclude_route(self, route: impl Into<String>) -> Self
pub fn exclude_route(self, route: impl Into<String>) -> Self
Adds a route pattern to exclude from recording.
Match the exact route label emitted by the metrics layer, such as a
static route supplied to route_metrics_layer or an Axum matched path.
Sourcepub fn with_max_series(self, max_series: usize) -> Self
pub fn with_max_series(self, max_series: usize) -> Self
Bounds the number of distinct route labels retained in memory.
The first max_series distinct route labels are recorded normally; any
further distinct label collapses into a single shared "<overflow>"
route. This prevents unbounded memory growth when a layer accidentally
emits high-cardinality labels (for example concrete request paths) while
still keeping the already-admitted routes intact. Without this cap the
collector records every distinct label it observes.
Sourcepub fn layer(&self) -> MetricsLayer<Self>
pub fn layer(&self) -> MetricsLayer<Self>
Creates a metrics layer backed by this collector.
The layer records request totals, errors, in-flight counts, and bounded
duration histograms. It does not expose a scrape endpoint; use
Self::routes or Self::routes_at for that.
Trait Implementations§
Source§impl Clone for PrometheusMetrics
impl Clone for PrometheusMetrics
Source§fn clone(&self) -> PrometheusMetrics
fn clone(&self) -> PrometheusMetrics
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more