pub struct PrometheusMetrics { /* private fields */ }
Expand description

Fairing and Handler implementing request instrumentation.

By default this tracks two metrics:

  • rocket_http_requests_total (labels: endpoint, method, status): the total number of HTTP requests handled by Rocket.
  • rocket_http_requests_duration_seconds (labels: endpoint, method, status): the request duration for all HTTP requests handled by Rocket.

The rocket prefix of these metrics can be changed by setting the ROCKET_PROMETHEUS_NAMESPACE environment variable.

Usage

Simply attach and mount a PrometheusMetrics instance to your Rocket app as for a normal fairing / handler:

use rocket_prometheus::PrometheusMetrics;

#[rocket::launch]
fn launch() -> _ {
    let prometheus = PrometheusMetrics::new();
    rocket::build()
        .attach(prometheus.clone())
        .mount("/metrics", prometheus)
}

Metrics will then be available on the “/metrics” endpoint:

$ curl localhost:8000/metrics
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.005"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.01"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.025"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.05"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.1"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.25"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.5"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="1"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="2.5"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="5"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="10"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="+Inf"} 2
rocket_http_requests_duration_seconds_sum{endpoint="/metrics",method="GET",status="200"} 0.0011045669999999999
rocket_http_requests_duration_seconds_count{endpoint="/metrics",method="GET",status="200"} 2
rocket_http_requests_total{endpoint="/metrics",method="GET",status="200"} 2

Implementations

Create a new PrometheusMetrics.

Create a new PrometheusMetrics with a custom Registry.

Create a new PrometheusMetrics using the default Prometheus Registry.

This will cause the fairing to include metrics created by the various prometheus macros, e.g. register_int_counter.

Get the registry used by this fairing to track additional metrics.

You can use this to register further metrics, causing them to be exposed along with the default metrics on the PrometheusMetrics handler.

Note that the http_requests_total and http_requests_duration_seconds metrics are not included in this registry.

use once_cell::sync::Lazy;
use prometheus::{opts, IntCounter};
use rocket_prometheus::PrometheusMetrics;

static MY_COUNTER: Lazy<IntCounter> = Lazy::new(|| {
    IntCounter::new("my_counter", "A counter I use a lot")
        .expect("Could not create counter")
});

let prometheus = PrometheusMetrics::new();
prometheus.registry().register(Box::new(MY_COUNTER.clone()));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

Returns an Info structure containing the name and Kind of this fairing. The name can be any arbitrary string. Kind must be an ord set of Kind variants. Read more

The request callback. Read more

The response callback. Read more

The ignite callback. Returns Ok if ignition should proceed and Err if ignition and launch should be aborted. Read more

The liftoff callback. Read more

Performs the conversion.

Called by Rocket when a Request with its associated Data should be handled by this handler. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

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

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

Performs the conversion.

Converts self into a collection.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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