axum_prometheus/utils.rs
1//! Utilities for getting metric names at runtime, and other helpers.
2use http::Method;
3
4use crate::{
5 AXUM_HTTP_REQUESTS_DURATION_SECONDS, AXUM_HTTP_REQUESTS_PENDING, AXUM_HTTP_REQUESTS_TOTAL,
6 AXUM_HTTP_RESPONSE_BODY_SIZE, PREFIXED_HTTP_REQUESTS_DURATION_SECONDS,
7 PREFIXED_HTTP_REQUESTS_PENDING, PREFIXED_HTTP_REQUESTS_TOTAL, PREFIXED_HTTP_RESPONSE_BODY_SIZE,
8};
9
10/// Standard HTTP request duration buckets measured in seconds. The default buckets are tailored to broadly
11/// measure the response time of a network service. Most likely, however, you will be required to define
12/// buckets customized to your use case.
13pub const SECONDS_DURATION_BUCKETS: &[f64; 11] = &[
14 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
15];
16
17pub(super) const fn as_label(method: &Method) -> &'static str {
18 match *method {
19 Method::OPTIONS => "OPTIONS",
20 Method::GET => "GET",
21 Method::POST => "POST",
22 Method::PUT => "PUT",
23 Method::DELETE => "DELETE",
24 Method::HEAD => "HEAD",
25 Method::TRACE => "TRACE",
26 Method::CONNECT => "CONNECT",
27 Method::PATCH => "PATCH",
28 _ => "",
29 }
30}
31
32/// The name of the requests total metric. By default, it's the same as [`AXUM_HTTP_REQUESTS_TOTAL`], but
33/// can be changed via the [`with_prefix`] function.
34///
35/// [`with_prefix`]: crate::MetricLayerBuilder::with_prefix
36pub fn requests_total_name() -> &'static str {
37 PREFIXED_HTTP_REQUESTS_TOTAL
38 .get()
39 .map_or(AXUM_HTTP_REQUESTS_TOTAL, |s| s.as_str())
40}
41
42/// The name of the requests duration metric. By default, it's the same as [`AXUM_HTTP_REQUESTS_DURATION_SECONDS`], but
43/// can be changed via the [`with_prefix`] function.
44///
45/// [`with_prefix`]: crate::MetricLayerBuilder::with_prefix
46pub fn requests_duration_name() -> &'static str {
47 PREFIXED_HTTP_REQUESTS_DURATION_SECONDS
48 .get()
49 .map_or(AXUM_HTTP_REQUESTS_DURATION_SECONDS, |s| s.as_str())
50}
51
52/// The name of the requests pending metric. By default, it's the same as [`AXUM_HTTP_REQUESTS_PENDING`], but
53/// can be changed via the [`with_prefix`] function.
54///
55/// [`with_prefix`]: crate::MetricLayerBuilder::with_prefix
56pub fn requests_pending_name() -> &'static str {
57 PREFIXED_HTTP_REQUESTS_PENDING
58 .get()
59 .map_or(AXUM_HTTP_REQUESTS_PENDING, |s| s.as_str())
60}
61
62/// The name of the response body size metric. By default, it's the same as [`AXUM_HTTP_RESPONSE_BODY_SIZE`], but
63/// can be changed via the [`with_prefix`] function.
64///
65/// [`with_prefix`]: crate::MetricLayerBuilder::with_prefix
66pub fn response_body_size_name() -> &'static str {
67 PREFIXED_HTTP_RESPONSE_BODY_SIZE
68 .get()
69 .map_or(AXUM_HTTP_RESPONSE_BODY_SIZE, |s| s.as_str())
70}