cat_gateway/metrics/
health.rs

1//! Health endpoints `live`, `started` and `ready` analytics.
2
3use crate::{
4    service::api::health::{live_get, ready_get, started_get},
5    settings::Settings,
6};
7
8/// Updates health endpoints values.
9pub(crate) async fn update() {
10    let api_host_names = Settings::api_host_names().join(",");
11    let service_id = Settings::service_id();
12
13    if matches!(
14        live_get::endpoint().await,
15        live_get::AllResponses::With(live_get::Responses::NoContent)
16    ) {
17        reporter::LIVE_INDICATOR
18            .with_label_values(&[&api_host_names, service_id])
19            .set(1);
20    } else {
21        reporter::LIVE_INDICATOR
22            .with_label_values(&[&api_host_names, service_id])
23            .set(0);
24    }
25
26    if matches!(
27        ready_get::endpoint().await,
28        ready_get::AllResponses::With(ready_get::Responses::NoContent)
29    ) {
30        reporter::READY_INDICATOR
31            .with_label_values(&[&api_host_names, service_id])
32            .set(1);
33    } else {
34        reporter::READY_INDICATOR
35            .with_label_values(&[&api_host_names, service_id])
36            .set(0);
37    }
38
39    if matches!(
40        started_get::endpoint().await,
41        started_get::AllResponses::With(started_get::Responses::NoContent)
42    ) {
43        reporter::STARTED_INDICATOR
44            .with_label_values(&[&api_host_names, service_id])
45            .set(1);
46    } else {
47        reporter::STARTED_INDICATOR
48            .with_label_values(&[&api_host_names, service_id])
49            .set(0);
50    }
51}
52
53/// All the related health endpoints reporting metrics to the Prometheus service are
54/// inside this module.
55pub(crate) mod reporter {
56    use std::sync::LazyLock;
57
58    use prometheus::{register_int_gauge_vec, IntGaugeVec};
59
60    /// Labels for the metrics.
61    const METRIC_LABELS: [&str; 2] = ["api_host_names", "service_id"];
62
63    /// Health `live` endpoint indicator whether its healthy or not
64    pub(crate) static LIVE_INDICATOR: LazyLock<IntGaugeVec> = LazyLock::new(|| {
65        register_int_gauge_vec!(
66            "health_live_indicator",
67            "Health `live` endpoint indicator whether its healthy or not, returns the response `204` or something else.",
68            &METRIC_LABELS
69        )
70        .unwrap()
71    });
72
73    /// Health `started` endpoint indicator whether its healthy or not
74    pub(crate) static STARTED_INDICATOR: LazyLock<IntGaugeVec> = LazyLock::new(|| {
75        register_int_gauge_vec!(
76                "health_started_indicator",
77                "Health `started` endpoint indicator whether its healthy or not, returns the response `204` or something else.",
78                &METRIC_LABELS
79            )
80            .unwrap()
81    });
82
83    /// Health `ready` endpoint indicator whether its healthy or not
84    pub(crate) static READY_INDICATOR: LazyLock<IntGaugeVec> = LazyLock::new(|| {
85        register_int_gauge_vec!(
86                    "health_ready_indicator",
87                    "Health `ready` endpoint indicator whether its healthy or not, returns the response `204` or something else.",
88                    &METRIC_LABELS
89                )
90                .unwrap()
91    });
92}