From ac2999252ba357d0b1ace1229b03ac9ee46c866f Mon Sep 17 00:00:00 2001 From: maia Date: Mon, 30 Nov 2026 12:56:17 +0100 Subject: [PATCH] grafana: the identity-mint alert selects the controller's pod records On k8s/v1 the swarm-controller runs as a pod. Its logs reach the store through the pod-log receiver with `service.name` "controller" and no `_SYSTEMD_UNIT`, so `hh-agent-identity-mint-failed`, which selected the unit and grouped by the host journal's `F_AGENT`, could never match: a failed agent identity mint fired nothing. Select `service.name`, and read the agent out of the stdout line with `extract "agent= step="`: the controller logs `agent= step= error=` as tracing's text format after the message, and the pod-log receiver keeps the whole line as `_msg`. The summary names the `agent` label and the `error=` text. The review classifier doc's example query changes the same way. A controller test renders the failure through the stdout format and pins that order. A module-eval case pins the rule's selector, the extract and the grouping, and that no rule selects the controller's unit any more. --- .../swarm/review-classifier.md | 12 ++-- nix/host-modules/swarm-grafana.nix | 14 ++-- nix/module-eval/grafana.nix | 29 ++++++++ swarm-controller/src/deploy/jobq.rs | 66 ++++++++++++++++++- 4 files changed, 110 insertions(+), 11 deletions(-) diff --git a/docs/4-implementation/swarm/review-classifier.md b/docs/4-implementation/swarm/review-classifier.md index 76d3b78df..b3c29f9b7 100644 --- a/docs/4-implementation/swarm/review-classifier.md +++ b/docs/4-implementation/swarm/review-classifier.md @@ -697,15 +697,17 @@ and 500 when it can't read the list, both as problem+json. Each recorded run is also one `info` event from `swarm-controller` with the message `review classifier run finished`. The event carries the same fields, -with `rows` as JSON. It reaches VictoriaLogs through the host journal, where -each field arrives as `F_` and the name in capitals (`F_OUTCOME`, `F_REPO`), so -a run whose write failed is still there: +with `rows` as JSON. The controller runs as a pod, so the event is a line of +its stdout, and it reaches VictoriaLogs through the pod-log receiver with +`service.name` `controller`. The fields are `name=value` text after the message +in `_msg`, not fields of the record (`outcome=post-failed`, `repo=...`), so a +run whose write failed is still there: ```console -$ swarm-logs query '_time:7d _SYSTEMD_UNIT:="swarm-controller.service" _msg:="review classifier run finished" | fields _time, F_TRIGGER, F_REPO, F_PR, F_OUTCOME, F_DETAIL, F_REVIEW_URL' --format json +$ swarm-logs query '_time:7d "service.name":="controller" _msg:"review classifier run finished" | fields _time, _msg' --format json ``` -Add a filter such as `F_OUTCOME:="post-failed"` before the `|` to narrow it. +Add a phrase filter such as `_msg:"outcome=post-failed"` before the `|` to narrow it. ## Observability and cost diff --git a/nix/host-modules/swarm-grafana.nix b/nix/host-modules/swarm-grafana.nix index 375e1cbff..d30e8ec2d 100644 --- a/nix/host-modules/swarm-grafana.nix +++ b/nix/host-modules/swarm-grafana.nix @@ -957,15 +957,21 @@ let # `MINT_IDENTITY_FAILED` in swarm-controller's jobq.rs, # logged once per failed `MintAgentIdentity` job node, at # agent creation or on a certificate renewal; the window - # is how long the rule stays firing after it. Host - # journal records carry the tracing fields as `F_`. + # is how long the rule stays firing after it. The + # controller runs as a pod, so the record is its stdout + # line, selected by the `service.name` the pod-log + # receiver stamps (./k8s/pod-logs.nix). That line is + # tracing's text format, so the agent is `agent=` in + # `_msg`, not a field: `extract` makes it one, and the + # pattern matches `MINT_IDENTITY_FAILED`'s fields in the + # order jobq.rs logs them. (logCountRule { uid = "hh-agent-identity-mint-failed"; title = "agent identity mint failed"; windowSeconds = 900; - expr = ''_time:15m _SYSTEMD_UNIT:="swarm-controller.service" _msg:"swarm jobq: mint_agent_identity failed" | stats by (F_AGENT) count() as failures''; + expr = ''_time:15m "service.name":="controller" _msg:"swarm jobq: mint_agent_identity failed" | extract "agent= step=" | stats by (agent) count() as failures''; for = "0s"; - summary = "swarm-controller failed to mint agent {{ $labels.F_AGENT }}'s store identity; the reason is the `F_ERROR` field of the swarm-controller log line"; + summary = "swarm-controller failed to mint agent {{ $labels.agent }}'s store identity; the reason is the `error=` text at the end of the controller's log line"; }) # `hive-archive-backup` (nix/host-modules/hive-archive-backup.nix) # prints one `archive backup failed: ` line per agent diff --git a/nix/module-eval/grafana.nix b/nix/module-eval/grafana.nix index 807247755..37b980e51 100644 --- a/nix/module-eval/grafana.nix +++ b/nix/module-eval/grafana.nix @@ -736,6 +736,35 @@ let && lib.all (e: !(lib.hasInfix "hive=~" e) && lib.hasInfix "\"host.name\"=~\"$host\"" e) exprs && lib.any (e: lib.hasInfix "system.cpu.time" e) exprs; } + { + # The controller runs as a pod, whose log records carry `service.name` + # and no `_SYSTEMD_UNIT`, and whose stdout line holds the agent as + # `agent=` text, not as a field. A rule that selects the unit never + # matches, so a failed mint never fires. The pattern's literal is the + # field order the controller's test pins on the real line. + # 🔬 Invert-proof: put `_SYSTEMD_UNIT:="swarm-controller.service"` back. + name = "the identity-mint alert selects the controller's pod records and reads the agent out of the line"; + ok = + let + groups = + grafanaHere.services.hyperhive.deploy.grafana.rendered.provision.alerting.rules.settings.groups; + rules = lib.concatMap (g: g.rules) groups; + rule = lib.findFirst (r: r.uid == "hh-agent-identity-mint-failed") null rules; + expr = (lib.head rule.data).model.expr; + unitSelected = lib.filter ( + r: lib.any (d: lib.hasInfix "swarm-controller.service" (d.model.expr or "")) r.data + ) rules; + in + rule != null + && lib.hasInfix ''"service.name":="controller"'' expr + && lib.hasInfix ''_msg:"swarm jobq: mint_agent_identity failed"'' expr + && lib.hasInfix ''| extract "agent= step=" |'' expr + && lib.hasInfix "stats by (agent) count()" expr + && !(lib.hasInfix "_SYSTEMD_UNIT" expr) + && !(lib.hasInfix "F_AGENT" expr) + && lib.hasInfix "$labels.agent" rule.annotations.summary + && unitSelected == [ ]; + } { # Each hive's collector gets its own absence rule, keyed on the client id # the swarm collector stamps as `sender`, so a hive that sends nothing diff --git a/swarm-controller/src/deploy/jobq.rs b/swarm-controller/src/deploy/jobq.rs index ea9057a19..de3861d98 100644 --- a/swarm-controller/src/deploy/jobq.rs +++ b/swarm-controller/src/deploy/jobq.rs @@ -470,8 +470,9 @@ pub(crate) async fn forge_node( /// Logged at `warn`, with the agent, the step and the error, when /// [`mint_identity`] fails. The Grafana rule `hh-agent-identity-mint-failed` -/// (`nix/host-modules/swarm-grafana.nix`) matches this exact text: change both -/// together. +/// (`nix/host-modules/swarm-grafana.nix`) matches this exact text, and reads +/// the agent out of the `agent= step=` that follows it on the pod's +/// stdout line: change both together. const MINT_IDENTITY_FAILED: &str = "swarm jobq: mint_agent_identity failed"; /// The `MintAgentIdentity` arm, lifted out so `run_node_work` stays under @@ -4647,6 +4648,67 @@ mod mint_identity_tests { assert_eq!(field("error"), error); } + /// A buffer the stdout format writes into, standing in for the pod's stdout. + #[derive(Clone, Default)] + struct Stdout(std::sync::Arc>>); + + impl std::io::Write for Stdout { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.0.lock().expect("unpoisoned").extend_from_slice(buf); + Ok(buf.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + + impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for Stdout { + type Writer = Self; + + fn make_writer(&'a self) -> Self { + self.clone() + } + } + + /// In the pod the controller's line reaches the log store as text: the + /// pod-log receiver keeps the line as `_msg` and sets no field for the + /// agent. `hh-agent-identity-mint-failed` in + /// `nix/host-modules/swarm-grafana.nix` reads the agent out of that text + /// with `extract "agent= step="`, so the line has to carry + /// `agent= step=` straight after the message, in that order, + /// and the error after them. + #[tokio::test] + async fn the_mint_failure_line_on_stdout_reads_agent_then_step_then_error() { + let stdout = Stdout::default(); + let subscriber = tracing_subscriber::fmt() + .with_ansi(false) + .with_writer(stdout.clone()) + .finish(); + let _guard = tracing::subscriber::set_default(subscriber); + + let Outcome::Failed(error) = mint_identity(None, "a/b").await else { + panic!("an illegal agent name cannot be minted"); + }; + + let written = String::from_utf8(stdout.0.lock().expect("unpoisoned").clone()) + .expect("the line is UTF-8"); + let line = written.lines().next().expect("one line was written"); + let message = + format!("swarm jobq: mint_agent_identity failed agent=a/b step=setup error={error}"); + assert!(line.contains(&message), "{line}"); + let after_message = line + .split_once("swarm jobq: mint_agent_identity failed") + .expect("the message is on the line") + .1; + let (agent, _) = after_message + .trim_start() + .strip_prefix("agent=") + .and_then(|rest| rest.split_once(" step=")) + .expect("what the alert's extract reads"); + assert_eq!(agent, "a/b"); + } + fn declared_as( agents: &[(&str, swarm_wire::wanted::AgentState)], ) -> swarm_wire::wanted::HiveWanted {