Skip to main content

ferrobridge_operator_api/
health.rs

1// SPDX-FileCopyrightText: Cadasto B.V.
2// SPDX-License-Identifier: BUSL-1.1
3
4//! The health documents: `GET /health/info`, the build facts, pins and
5//! lanes, and `GET /health/readiness`, the state of every indicator.
6
7use std::collections::BTreeMap;
8
9use serde::{Deserialize, Serialize};
10
11/// What `GET /health/info` answers with.
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(deny_unknown_fields)]
14pub struct Info {
15    /// The product name.
16    pub product: String,
17    /// The product version.
18    pub version: String,
19    /// The build facts.
20    pub build: Build,
21    /// The pins the process serves, in the order the banner prints them.
22    pub pins: Vec<Pin>,
23    /// The lanes this deployment configures, in banner order.
24    pub lanes: Vec<Lane>,
25}
26
27/// The build facts.
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
29#[serde(deny_unknown_fields)]
30pub struct Build {
31    /// The full git commit, or `unknown`.
32    pub commit: String,
33    /// The abbreviated git commit, or `unknown`.
34    pub commit_short: String,
35    /// The build instant, RFC 3339 UTC.
36    pub built_at: String,
37    /// The compiler.
38    pub rustc: String,
39}
40
41/// One pin the process serves: a name and the version it is at.
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43#[serde(deny_unknown_fields)]
44pub struct Pin {
45    /// What is pinned, as the pin matrix names it.
46    pub name: String,
47    /// The version, as the implementing crate states it.
48    pub version: String,
49}
50
51/// One lane.
52#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
53#[serde(deny_unknown_fields)]
54pub struct Lane {
55    /// The lane's name, as the banner names it.
56    pub name: String,
57    /// Whether the configuration switches it on.
58    pub enabled: bool,
59    /// The socket address a face with a listener of its own binds.
60    #[serde(default, skip_serializing_if = "Option::is_none")]
61    pub listen: Option<String>,
62    /// Whether that listener accepts connections now.
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    pub up: Option<bool>,
65}
66
67/// The state of one indicator, or of the aggregate.
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
69#[serde(rename_all = "lowercase")]
70pub enum State {
71    /// The subsystem answered.
72    Up,
73    /// The subsystem did not answer.
74    Down,
75}
76
77/// The outcome of one indicator check.
78#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
79#[serde(deny_unknown_fields)]
80pub struct IndicatorState {
81    /// Whether the subsystem answered.
82    pub state: State,
83    /// Why it did not, when it did not. Never a body and never a credential.
84    #[serde(default, skip_serializing_if = "Option::is_none")]
85    pub detail: Option<String>,
86}
87
88impl IndicatorState {
89    /// Returns the state of a subsystem that answered.
90    #[must_use]
91    pub const fn up() -> Self {
92        Self {
93            state: State::Up,
94            detail: None,
95        }
96    }
97
98    /// Returns the state of a subsystem that did not answer, with the reason.
99    #[must_use]
100    pub fn down(detail: impl Into<String>) -> Self {
101        Self {
102            state: State::Down,
103            detail: Some(detail.into()),
104        }
105    }
106}
107
108/// What `GET /health/readiness` answers with, `200` while the aggregate is
109/// up and `503` while it is down.
110#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
111#[serde(deny_unknown_fields)]
112pub struct Readiness {
113    /// The aggregate: down while any indicator is down.
114    pub state: State,
115    /// Every indicator by name, in name order.
116    pub indicators: BTreeMap<String, IndicatorState>,
117}