Rocket Prometheus

Build Status

Prometheus instrumentation for Rocket applications.

Usage

Add this crate to your Cargo.toml:

toml [dependencies] rocket_prometheus = "0.1"

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

```rust use rocket_prometheus::PrometheusMetrics;

fn main() { let prometheus = PrometheusMetrics::new(); rocket::ignite() .attach(prometheus.clone()) .mount("/metrics", prometheus) .launch(); } ```

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

```shell $ curl localhost:8000/metrics

HELP rockethttprequestsdurationseconds HTTP request duration in seconds for all requests

TYPE rockethttprequestsdurationseconds histogram

rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="0.005"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="0.01"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="0.025"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="0.05"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="0.1"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="0.25"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="0.5"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="1"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="2.5"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="5"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="10"} 2 rockethttprequestsdurationsecondsbucket{endpoint="/metrics",method="GET",status="200",le="+Inf"} 2 rockethttprequestsdurationsecondssum{endpoint="/metrics",method="GET",status="200"} 0.0011045669999999999 rockethttprequestsdurationsecondscount{endpoint="/metrics",method="GET",status="200"} 2

HELP rockethttprequests_total Total number of HTTP requests

TYPE rockethttprequests_total counter

rockethttprequests_total{endpoint="/metrics",method="GET",status="200"} 2 ```

Metrics

By default this crate tracks two metrics:

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:

```rust

![feature(procmacrohygiene, decl_macro)]

[macro_use]

extern crate rocket;

use lazystatic::lazystatic; use prometheus::{opts, IntCounterVec}; use rocket::http::RawStr; use rocket_prometheus::PrometheusMetrics;

lazystatic! { static ref NAMECOUNTER: IntCounterVec = IntCounterVec::new(opts!("name_counter", "Count of names"), &["name"]).unwrap(); }

[get("/hello/")]

pub fn hello(name: &RawStr) -> String { NAMECOUNTER.withlabel_values(&[name]).inc(); format!("Hello, {}!", name) }

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