use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReadinessState {
Ready,
Warming,
Failed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HealthCategory {
Warming,
CollectorFailure,
NotServing,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct HealthResponse {
pub schema_version: u16,
pub state: ReadinessState,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub category: Option<HealthCategory>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub snapshot: Option<crate::StatusSnapshot>,
}
impl HealthResponse {
#[must_use]
pub fn ready(snapshot: crate::StatusSnapshot) -> Self {
Self {
schema_version: crate::SCHEMA_VERSION_V1,
state: ReadinessState::Ready,
category: None,
message: None,
snapshot: Some(snapshot),
}
}
#[must_use]
pub fn warming() -> Self {
Self::warming_with_message("collector warming up")
}
#[must_use]
pub fn warming_with_message(message: impl Into<String>) -> Self {
Self {
schema_version: crate::SCHEMA_VERSION_V1,
state: ReadinessState::Warming,
category: Some(HealthCategory::Warming),
message: Some(message.into()),
snapshot: None,
}
}
#[must_use]
pub fn failed(category: HealthCategory, message: impl Into<String>) -> Self {
Self {
schema_version: crate::SCHEMA_VERSION_V1,
state: ReadinessState::Failed,
category: Some(category),
message: Some(message.into()),
snapshot: None,
}
}
}