diff --git a/docs/4-implementation/swarm/telemetry.md b/docs/4-implementation/swarm/telemetry.md index 3e4c543da..5d4f0b41c 100644 --- a/docs/4-implementation/swarm/telemetry.md +++ b/docs/4-implementation/swarm/telemetry.md @@ -237,7 +237,12 @@ fails if a socket refuses for 30 seconds, or if a route answers `000`, `502` or `504`, or lacks its own `X-Swarm-Otel-Route` response header, for 30 seconds. The gateway sets that header, named after the route, on every response from a route, so a collector that's up but not listening, or an nginx that doesn't -have the route, shows as a failed unit. Any status other than those passes, +have the route, shows as a failed unit. On a fresh swarm the unit fails at +activation, because the collector opens its sockets only once the operator has +unsealed the store. A timer, `swarm-otel-sockets-recheck`, starts the unit again +every minute while it is failed, so it turns `active` by itself once the +collector listens. systemd never restarts the unit on its own: a switch would +wait on the pending start for as long as the collector is down. Any status other than those passes, the collector's 4xx included. A switch leaves a window of about 20 seconds in which nginx and the container are out of step: the container stops before nginx restarts and starts about 18 seconds after it. Senders retry through that window. @@ -267,7 +272,7 @@ swarm-otel-sockets` on the host shows whether the receiver sockets are there; `machinectl shell swarm-otel /run/current-system/sw/bin/journalctl -u opentelemetry-collector` shows why the collector didn't start. A collector that keeps retrying never reaches `failed`, so `systemctl --failed` doesn't list it. -The signals are `swarm-otel-sockets` failing and the Grafana alert +The signals are `swarm-otel-sockets` not `active` and the Grafana alert `no log records from `, which fires after about 15 minutes without a log record stamped with that hive's `sender`. diff --git a/nix/host-modules/swarm-otel.nix b/nix/host-modules/swarm-otel.nix index 5e9e6a3b8..ec90b1f40 100644 --- a/nix/host-modules/swarm-otel.nix +++ b/nix/host-modules/swarm-otel.nix @@ -900,6 +900,33 @@ in ''; }; + # On a fresh swarm the collector opens its sockets only after authelia + # answers, which is after the operator unseals the store, long after the + # check's one attempt at activation. The check stays failed, which is what + # the switch reports, so it is not given a restart of its own: a oneshot's + # start job stays pending through an automatic restart, and the switch + # would wait on it for as long as the collector is down. This timer starts + # the check again each minute while, and only while, it is failed. The + # restart does not block, so a check that fails again leaves nothing + # waiting. + systemd.services.swarm-otel-sockets-recheck = { + description = "start the swarm collector socket check again while it is failed"; + serviceConfig = { + Type = "oneshot"; + # Exit 1 (the check is not failed) skips the start and is no failure. + ExecCondition = "${pkgs.systemd}/bin/systemctl is-failed --quiet swarm-otel-sockets.service"; + ExecStart = "${pkgs.systemd}/bin/systemctl restart --no-block swarm-otel-sockets.service"; + }; + }; + systemd.timers.swarm-otel-sockets-recheck = { + description = "start the swarm collector socket check again while it is failed"; + wantedBy = [ "timers.target" ]; + timerConfig = { + OnActiveSec = "1min"; + OnUnitActiveSec = "1min"; + }; + }; + assertions = [ { # Shaped after ./swarm-grafana.nix's `haveClientIdentity` assertion — diff --git a/nix/module-eval/swarm-services-switch.nix b/nix/module-eval/swarm-services-switch.nix index c227d3366..2c349fe94 100644 --- a/nix/module-eval/swarm-services-switch.nix +++ b/nix/module-eval/swarm-services-switch.nix @@ -1230,6 +1230,31 @@ let && lib.elem "container@swarm-otel.service" u.partOf && lib.elem "multi-user.target" u.wantedBy; } + { + # On a fresh swarm the collector opens its sockets only after the store + # is unsealed, long after the check's one attempt at activation. The check + # itself is never restarted by systemd: a oneshot's start job stays + # pending through an automatic restart, and a switch would wait on it for + # as long as the collector is down. A timer starts it again, only while it + # is failed (`ExecCondition`) and without blocking. Without the condition + # a healthy check would restart every minute; without `timers.target` the + # timer would never run. + name = "a failed receiver-socket check is started again by a timer, and the check is not restarted by systemd"; + ok = + let + host = serviceContainersWithCi; + check = host.systemd.services.swarm-otel-sockets; + re = host.systemd.services.swarm-otel-sockets-recheck; + t = host.systemd.timers.swarm-otel-sockets-recheck; + in + !(check.serviceConfig ? Restart) + && re.serviceConfig.Type == "oneshot" + && lib.hasInfix "is-failed --quiet swarm-otel-sockets.service" re.serviceConfig.ExecCondition + && lib.hasInfix "restart --no-block swarm-otel-sockets.service" re.serviceConfig.ExecStart + && lib.elem "timers.target" t.wantedBy + && t.timerConfig.OnUnitActiveSec == "1min" + && t.timerConfig ? OnActiveSec; + } { # `DynamicUser` forces `ProtectSystem=strict`, which leaves `/run` # read-only unless the path is in `ReadWritePaths`. The controls: the diff --git a/nix/vm-tests/full-setup.py b/nix/vm-tests/full-setup.py index 6159669c4..fdfcad627 100644 --- a/nix/vm-tests/full-setup.py +++ b/nix/vm-tests/full-setup.py @@ -226,6 +226,19 @@ with subtest("the swarm collector's client secret reaches the host"): "swarm-bao-otel-oidc", ) +with subtest("the receiver-socket check is active once the collector has recovered"): + # The collector opens its sockets only after authelia answers, which on a fresh swarm is + # after the unseal. `swarm-otel-sockets` runs at activation, long before that, so its first + # attempt fails; `swarm-otel-sockets-recheck` has to start it again, or it stays failed (and + # the only red unit on the swarm) after ingest works. The timer fires each minute and an + # attempt takes up to 30 s, so the wait covers several rounds. + settle( + '[ "$(systemctl is-active swarm-otel-sockets.service)" = active ]', + 600, + "swarm-otel-sockets", + "swarm-otel", + ) + with subtest("step 2: swarmctl user add"): machine.wait_until_succeeds( "swarmctl user add mara --display-name Mara --email mara@example.com --group admins "