17 releases (9 breaking)

0.10.0 Nov 20, 2023
0.10.0-rc.3 Jun 23, 2022
0.10.0-rc.2 May 14, 2022
0.10.0-rc.1 Feb 10, 2022
0.3.0 Jun 25, 2019

#174 in Network programming

Download history 1244/week @ 2023-12-17 452/week @ 2023-12-24 1314/week @ 2023-12-31 2043/week @ 2024-01-07 1875/week @ 2024-01-14 2033/week @ 2024-01-21 3056/week @ 2024-01-28 2174/week @ 2024-02-04 1540/week @ 2024-02-11 2292/week @ 2024-02-18 1836/week @ 2024-02-25 1665/week @ 2024-03-03 2666/week @ 2024-03-10 2904/week @ 2024-03-17 2492/week @ 2024-03-24 1795/week @ 2024-03-31

9,909 downloads per month
Used in 2 crates

MIT/Apache

26KB
220 lines

Rocket Prometheus

Build Status docs.rs crates.io

Prometheus instrumentation for Rocket applications.

Usage

Add this crate to your Cargo.toml alongside Rocket 0.5.0:

[dependencies]
rocket = "0.5.0"
rocket_prometheus = "0.10.0"

Then attach and mount a PrometheusMetrics instance to your Rocket app:

use rocket_prometheus::PrometheusMetrics;

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

This will expose metrics like this at the /metrics endpoint of your application:

$ curl localhost:8000/metrics
# HELP rocket_http_requests_duration_seconds HTTP request duration in seconds for all requests
# TYPE rocket_http_requests_duration_seconds histogram
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
# HELP rocket_http_requests_total Total number of HTTP requests
# TYPE rocket_http_requests_total counter
rocket_http_requests_total{endpoint="/metrics",method="GET",status="200"} 2

Metrics

By default this crate 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.

Custom Metrics

Further metrics can be tracked by registering them with the registry of the PrometheusMetrics instance:

#[macro_use]
use once_cell::sync::Lazy;
use rocket::{get, launch, routes};
use rocket_prometheus::{
    prometheus::{opts, IntCounterVec},
    PrometheusMetrics,
};

static NAME_COUNTER: Lazy<IntCounterVec> = Lazy::new(|| {
    IntCounterVec::new(opts!("name_counter", "Count of names"), &["name"])
        .expect("Could not create NAME_COUNTER")
});

#[get("/hello/<name>")]
pub fn hello(name: &str) -> String {
    NAME_COUNTER.with_label_values(&[name]).inc();
    format!("Hello, {}!", name)
}

#[launch]
fn launch() -> _ {
    let prometheus = PrometheusMetrics::new();
    prometheus
        .registry()
        .register(Box::new(NAME_COUNTER.clone()))
        .unwrap();
    rocket::build()
        .attach(prometheus.clone())
        .mount("/", routes![hello])
        .mount("/metrics", prometheus)
}

Dependencies

~16–52MB
~831K SLoC