-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.rs
More file actions
40 lines (35 loc) · 1.26 KB
/
metrics.rs
File metadata and controls
40 lines (35 loc) · 1.26 KB
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
use axum::response::IntoResponse;
use prometheus::{register_counter_vec, register_histogram_vec, CounterVec, Encoder, HistogramVec, TextEncoder};
use std::sync::OnceLock;
static HTTP_REQUESTS_TOTAL: OnceLock<CounterVec> = OnceLock::new();
static HTTP_REQUEST_DURATION_SECONDS: OnceLock<HistogramVec> = OnceLock::new();
pub fn init() {
HTTP_REQUESTS_TOTAL.get_or_init(|| {
register_counter_vec!(
"csf_http_requests_total",
"Total HTTP requests",
&["method", "path", "status"]
)
.expect("failed to register csf_http_requests_total")
});
HTTP_REQUEST_DURATION_SECONDS.get_or_init(|| {
register_histogram_vec!(
"csf_http_request_duration_seconds",
"HTTP request duration in seconds",
&["method", "path"]
)
.expect("failed to register csf_http_request_duration_seconds")
});
}
pub async fn metrics_handler() -> impl IntoResponse {
let encoder = TextEncoder::new();
let metric_families = prometheus::gather();
let mut buffer = Vec::new();
encoder
.encode(&metric_families, &mut buffer)
.expect("failed to encode metrics");
(
[(axum::http::header::CONTENT_TYPE, "text/plain; version=0.0.4")],
buffer,
)
}