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) 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(),
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(),
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(),
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    #![allow(clippy::unwrap_used)]
57
58    use std::sync::LazyLock;
59
60    use prometheus::{register_int_gauge_vec, IntGaugeVec};
61
62    /// Labels for the metrics.
63    const METRIC_LABELS: [&str; 2] = ["api_host_names", "service_id"];
64
65    /// Health `live` endpoint indicator whether its healthy or not
66    pub(crate) static LIVE_INDICATOR: LazyLock<IntGaugeVec> = LazyLock::new(|| {
67        register_int_gauge_vec!(
68            "health_live_indicator",
69            "Health `live` endpoint indicator whether its healthy or not, returns the response `204` or something else.",
70            &METRIC_LABELS
71        )
72        .unwrap()
73    });
74
75    /// Health `started` endpoint indicator whether its healthy or not
76    pub(crate) static STARTED_INDICATOR: LazyLock<IntGaugeVec> = LazyLock::new(|| {
77        register_int_gauge_vec!(
78                "health_started_indicator",
79                "Health `started` endpoint indicator whether its healthy or not, returns the response `204` or something else.",
80                &METRIC_LABELS
81            )
82            .unwrap()
83    });
84
85    /// Health `ready` endpoint indicator whether its healthy or not
86    pub(crate) static READY_INDICATOR: LazyLock<IntGaugeVec> = LazyLock::new(|| {
87        register_int_gauge_vec!(
88                    "health_ready_indicator",
89                    "Health `ready` endpoint indicator whether its healthy or not, returns the response `204` or something else.",
90                    &METRIC_LABELS
91                )
92                .unwrap()
93    });
94}