use super::gauges::Gauges;
#[derive(Debug, Clone)]
pub struct Labels {
pub workspace: String,
pub namespace: String,
}
impl Labels {
fn render(&self) -> String {
format!(
"{{workspace=\"{}\",namespace=\"{}\"}}",
escape(&self.workspace),
escape(&self.namespace)
)
}
}
fn escape(s: &str) -> String {
s.replace('\\', "\\\\").replace('"', "\\\"").replace('\n', " ")
}
fn withheld_code(s: Option<&str>) -> u64 {
match s {
None => 0,
Some("parked-412") => 1,
Some("unreadable") => 2,
Some(_) => 99,
}
}
fn source_code(s: &str) -> u64 {
match s {
"sentinel" => 1,
"sentinel-deferred" => 2,
"cadence" => 3,
"drain" => 4,
_ => 99,
}
}
fn series(g: &Gauges) -> Vec<(&'static str, &'static str, u64)> {
vec![
(
"flint_lean_rpo_seconds",
"seconds since the last boundary was installed. Elapsed time, not exposure: an \
idle healthy workspace has nothing at risk and a growing value — pair it with \
flint_lean_withheld_reason before alerting",
g.rpo_secs,
),
(
"flint_lean_withheld_reason",
"why visibility is withheld: 0=none 1=parked-412 2=unreadable",
withheld_code(g.withheld_reason.as_deref()),
),
(
"flint_lean_sentinel_budget_remaining",
"work units left in this hour's sentinel budget (D3.1). Zero means honors \
defer to the floor — the workspace is running at exactly cadence behavior",
g.sentinel_budget_remaining,
),
(
"flint_lean_last_boundary_source",
"which clock installed the last boundary: 1=sentinel 2=sentinel-deferred \
3=cadence 4=drain, 0=none yet",
g.last_boundary.as_ref().map(|b| source_code(&b.source)).unwrap_or(0),
),
(
"flint_lean_last_boundary_seq",
"the manifest seq the last boundary installed",
g.last_boundary.as_ref().map(|b| b.seq).unwrap_or(0),
),
(
"flint_lean_last_boundary_timestamp_seconds",
"unix time of the last boundary",
g.last_boundary.as_ref().map(|b| b.unix).unwrap_or(0),
),
(
"flint_lean_auth_paused_since_timestamp_seconds",
"unix time of the first renewal the store refused with 401/403 since the last \
successful one; 0 = not paused. A credential, token or clock fault — never \
contention and never a deposal, and retrying does not fix it. It is exported \
as a timestamp rather than a duration because a paused holder cannot renew, \
and a holder that cannot renew is what a challenger reads as DEAD: alert on \
time() - this > the takeover threshold, which is the window in which a live \
writer gets deposed (design 6.3)",
g.auth_paused_since_unix.unwrap_or(0),
),
(
"flint_lean_updated_timestamp_seconds",
"unix time these gauges were last refreshed. Refreshed on EVERY tick, news or \
not, so an idle-but-healthy workspace is distinguishable from a dead one",
g.updated_unix,
),
(
"flint_lean_last_durable_timestamp_seconds",
"unix time of the last durable write (gauges.last_durable_unix)",
g.last_durable_unix,
),
]
}
pub fn render(g: &Gauges, labels: &Labels) -> String {
let l = labels.render();
let mut out = String::new();
for (name, help, value) in series(g) {
out.push_str(&format!("# HELP {name} {help}\n# TYPE {name} gauge\n{name}{l} {value}\n"));
}
out
}
pub const COVERED_FIELDS: &[&str] = &[
"rpo_secs",
"withheld_reason",
"sentinel_budget_remaining",
"last_boundary",
"updated_unix",
"last_durable_unix",
"auth_paused_since_unix",
];
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct MetricsPosture {
pub enabled: bool,
pub port: u16,
pub bound: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
pub const POSTURE_FILE: &str = "metrics.json";
impl super::Syncer {
pub fn save_metrics_posture(&self, p: &MetricsPosture) -> super::LeanResult<()> {
let bytes = serde_json::to_vec_pretty(p)
.map_err(|e| super::LeanError::State(format!("metrics posture: {e}")))?;
super::control::write_atomic(&self.cfg.state_dir().join(POSTURE_FILE), &bytes)
}
pub fn load_metrics_posture(&self) -> Option<MetricsPosture> {
serde_json::from_slice(&std::fs::read(self.cfg.state_dir().join(POSTURE_FILE)).ok()?).ok()
}
}