Skip to main content

PrometheusMetrics

Struct PrometheusMetrics 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn routes(&self) -> Router

Creates a /metrics route for this collector.

Source

pub fn routes_at(&self, path: &'static str) -> Router

Creates a metrics route at a custom path.

Source

pub fn render(&self) -> String

Renders metrics in Prometheus text exposition format.

The output includes nidus_http_requests_total, nidus_http_request_duration_seconds_count, nidus_http_request_duration_seconds_sum, nidus_http_in_flight_requests, and nidus_http_errors_total.

Trait Implementations§

Source§

impl Clone for PrometheusMetrics

Source§

fn clone(&self) -> PrometheusMetrics

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PrometheusMetrics

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PrometheusMetrics

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl HttpMetricsHook for PrometheusMetrics

Source§

fn on_request(&self, method: &Method, route: Option<&str>)

Records that a request entered the service.
Source§

fn on_response( &self, method: &Method, route: Option<&str>, status: StatusCode, latency: Duration, )

Records that a response left the service.
Source§

fn on_error(&self, method: &Method, route: Option<&str>, latency: Duration)

Records that the inner service returned an error before producing a response.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Provider for T
where T: Send + Sync + 'static,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more