1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
/*! Prometheus instrumentation for Rocket applications. # Usage Add this crate to your `Cargo.toml`: ```toml [dependencies] rocket_prometheus = "0.8.0" ``` Then attach and mount a `PrometheusMetrics` instance to your Rocket app: ```rust use rocket_prometheus::PrometheusMetrics; let prometheus = PrometheusMetrics::new(); # if false { 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 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: ```rust #![feature(proc_macro_hygiene, decl_macro)] #[macro_use] extern crate rocket; use once_cell::sync::Lazy; use rocket::http::RawStr; 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: &RawStr) -> String { NAME_COUNTER.with_label_values(&[name]).inc(); format!("Hello, {}!", name) } fn main() { let prometheus = PrometheusMetrics::new(); prometheus .registry() .register(Box::new(NAME_COUNTER.clone())) .unwrap(); # if false { rocket::ignite() .attach(prometheus.clone()) .mount("/", routes![hello]) .mount("/metrics", prometheus) .launch(); # } } ``` */ #![deny(missing_docs)] #![forbid(unsafe_code)] use std::{env, time::Instant}; use prometheus::{opts, Encoder, HistogramVec, IntCounterVec, Registry, TextEncoder}; use rocket::{ fairing::{Fairing, Info, Kind}, handler::Outcome, http::{ContentType, Method}, response::Content, Data, Handler, Request, Response, Route, }; /// Re-export Prometheus so users can use it without having to explicitly /// add a specific version to their dependencies, which can result in /// mysterious compiler error messages. pub use prometheus; /// Environment variable used to configure the namespace of metrics exposed /// by `PrometheusMetrics`. const NAMESPACE_ENV_VAR: &str = "ROCKET_PROMETHEUS_NAMESPACE"; #[derive(Clone)] #[must_use = "must be attached and mounted to a Rocket instance"] /// 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: /// /// ```rust /// use rocket_prometheus::PrometheusMetrics; /// /// let prometheus = PrometheusMetrics::new(); /// # if false { /// rocket::ignite() /// .attach(prometheus.clone()) /// .mount("/metrics", prometheus) /// .launch(); /// # } /// ``` /// /// Metrics will then be available on the "/metrics" endpoint: /// /// ```shell /// $ 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 /// ``` pub struct PrometheusMetrics { // Standard metrics tracked by the fairing. http_requests_total: IntCounterVec, http_requests_duration_seconds: HistogramVec, // The registry used by the fairing. registry: Registry, } impl PrometheusMetrics { /// Get the registry used by this fairing. /// /// You can use this to register further metrics, /// causing them to be exposed along with the default metrics /// on the `PrometheusMetrics` handler. /// /// ```rust /// 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())); /// ``` #[must_use] pub const fn registry(&self) -> &Registry { &self.registry } } impl PrometheusMetrics { /// Create a new `PrometheusMetrics`. pub fn new() -> Self { Self::with_registry(Registry::new()) } /// 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`. pub fn with_default_registry() -> Self { Self::with_registry(prometheus::default_registry().clone()) } /// Create a new `PrometheusMetrics` with a custom `Registry`. pub fn with_registry(registry: Registry) -> Self { let namespace = env::var(NAMESPACE_ENV_VAR).unwrap_or_else(|_| "rocket".into()); let http_requests_total_opts = opts!("http_requests_total", "Total number of HTTP requests") .namespace(namespace.clone()); let http_requests_total = IntCounterVec::new(http_requests_total_opts, &["endpoint", "method", "status"]) .unwrap(); registry .register(Box::new(http_requests_total.clone())) .unwrap(); let http_requests_duration_seconds_opts = opts!( "http_requests_duration_seconds", "HTTP request duration in seconds for all requests" ) .namespace(namespace); let http_requests_duration_seconds = HistogramVec::new( http_requests_duration_seconds_opts.into(), &["endpoint", "method", "status"], ) .unwrap(); registry .register(Box::new(http_requests_duration_seconds.clone())) .unwrap(); PrometheusMetrics { http_requests_total, http_requests_duration_seconds, registry, } } } impl Default for PrometheusMetrics { fn default() -> Self { Self::new() } } /// Value stored in request-local state to measure response time. #[derive(Copy, Clone)] struct TimerStart(Option<Instant>); impl Fairing for PrometheusMetrics { fn info(&self) -> Info { Info { name: "Prometheus metric collection", kind: Kind::Request | Kind::Response, } } fn on_request(&self, request: &mut Request, _: &Data) { request.local_cache(|| TimerStart(Some(Instant::now()))); } fn on_response(&self, request: &Request, response: &mut Response) { // Don't touch metrics if the request didn't match a route. if request.route().is_none() { return; } let endpoint = request.route().unwrap().uri.to_string(); let method = request.method().as_str(); let status = response.status().code.to_string(); self.http_requests_total .with_label_values(&[&endpoint, method, &status]) .inc(); let start_time = request.local_cache(|| TimerStart(None)); if let Some(duration) = start_time.0.map(|st| st.elapsed()) { let duration_secs = duration.as_secs_f64(); self.http_requests_duration_seconds .with_label_values(&[&endpoint, method, &status]) .observe(duration_secs); } } } impl Handler for PrometheusMetrics { fn handle<'r>(&self, req: &'r Request, _: Data) -> Outcome<'r> { // Gather the metrics. let mut buffer = vec![]; let encoder = TextEncoder::new(); encoder .encode(&self.registry.gather(), &mut buffer) .unwrap(); let body = String::from_utf8(buffer).unwrap(); Outcome::from( req, Content( ContentType::with_params( "text", "plain", &[("version", "0.0.4"), ("charset", "utf-8")], ), body, ), ) } } impl From<PrometheusMetrics> for Vec<Route> { fn from(other: PrometheusMetrics) -> Self { vec![Route::new(Method::Get, "/", other)] } }