cat_gateway/metrics/
endpoint.rs

1//! Metrics related to endpoint analytics.
2
3#![allow(clippy::unwrap_used)]
4
5use std::sync::LazyLock;
6
7use prometheus::{
8    register_histogram_vec, register_int_counter, register_int_counter_vec, HistogramVec,
9    IntCounter, IntCounterVec,
10};
11
12/// Labels for the metrics
13const METRIC_LABELS: [&str; 3] = ["endpoint", "method", "status_code"];
14/// Labels for the client metrics
15const CLIENT_METRIC_LABELS: [&str; 2] = ["client", "status_code"];
16
17// Prometheus Metrics maintained by the service
18
19/// HTTP Request duration histogram.
20pub(crate) static HTTP_REQ_DURATION_MS: LazyLock<HistogramVec> = LazyLock::new(|| {
21    register_histogram_vec!(
22        "http_request_duration_ms",
23        "Duration of HTTP requests in milliseconds",
24        &METRIC_LABELS
25    )
26    .unwrap()
27});
28
29/// HTTP Request CPU Time histogram.
30pub(crate) static HTTP_REQ_CPU_TIME_MS: LazyLock<HistogramVec> = LazyLock::new(|| {
31    register_histogram_vec!(
32        "http_request_cpu_time_ms",
33        "CPU Time of HTTP requests in milliseconds",
34        &METRIC_LABELS
35    )
36    .unwrap()
37});
38
39// No Tacho implemented to enable this.
40// static ref HTTP_REQUEST_RATE: GaugeVec = register_gauge_vec!(
41// "http_request_rate",
42// "Rate of HTTP requests per second",
43// &METRIC_LABELS
44// )
45// .unwrap();
46
47/// HTTP Request count histogram.
48pub(crate) static HTTP_REQUEST_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
49    register_int_counter_vec!(
50        "http_request_count",
51        "Number of HTTP requests",
52        &METRIC_LABELS
53    )
54    .unwrap()
55});
56
57/// Client Request Count histogram.
58pub(crate) static CLIENT_REQUEST_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
59    register_int_counter_vec!(
60        "client_request_count",
61        "Number of HTTP requests per client",
62        &CLIENT_METRIC_LABELS
63    )
64    .unwrap()
65});
66
67/// Metric counter to track the number of HTTP 404 Not Found responses.
68/// It is use for monitoring crawler activity or requests to invalid URLs.
69pub(crate) static NOT_FOUND_COUNT: LazyLock<IntCounter> = LazyLock::new(|| {
70    register_int_counter!("response_404", "Number of 404 not found response",).unwrap()
71});