Fills in the `pubsub` section of the deployment health report on the
server. Stacked on #31620.
- New `pubsub.ReportingPubsub` interface: `Pubsub` plus
`HealthReport(ctx) healthsdk.PubsubReport`. The `Pubsub` interface
itself is unchanged.
- The PostgreSQL, NATS and in-memory backends implement it. Each reports
its backend name (`postgres`, `nats` or `memory`) and its connection
state:
- **PostgreSQL:** tracks the `pq` listener's connected state from its
event callback. When it's not connected, the report has severity
`warning` and `EPS01`.
- **NATS:** warns with `EPS02` if the embedded server isn't `Running()`,
and with `EPS01` for each publish or subscribe connection whose
`Status()` isn't `CONNECTED`.
- **In-memory:** always `ok`.
- `coderd.Options.Pubsub` is now a `ReportingPubsub`.
`ReplicaSyncPubsub` stays a plain `Pubsub`.
- `coderd/healthcheck` has a new `Pubsub` check, which counts toward
`healthy` and the overall severity like the other sections.
`formatHealthcheck` applies the dismissed setting for it.
- New health codes `EPS01` (not connected) and `EPS02` (embedded NATS
server not running), documented in
`docs/admin/monitoring/health-check.md`.
- Test helpers follow the type change: `coderdtest.Options.Pubsub` and
the pubsub returned by `dbtestutil.NewDB` are now `ReportingPubsub`, and
a few test wrappers embed `ReportingPubsub` instead of `Pubsub`.
<details>
<summary>Decision log</summary>
- `HealthReport` returns a `healthsdk.PubsubReport` rather than filling
one in place. This matches the `healthcheck.Checker` methods. `ctx` is
unused for now.
- The healthcheck layer sets `Dismissed`, so backends don't need to know
about health settings.
- Problems are reported as warnings, not errors, so they show on the
health page without marking the deployment unhealthy. Both backends
reconnect on their own.
- Postgres connection state comes from the listener event callback, not
from `pq.Listener.Ping`, so the health check doesn't do database I/O.
Closing the pubsub also marks it disconnected.
- NATS `conn` gains `Status()`, which `*natsgo.Conn` already implements.
The pools are immutable after construction, so `HealthReport` reads them
without locking.
- The docs say the pubsub section appears in the JSON response, because
the health UI doesn't show it until a later PR in this stack.
- psmock is not regenerated: none of its users pass it to coderd.
</details>
---
Generated by Coder Agents on behalf of @spikecurtis.
Route embedded NATS server logs through Coder's slog logger by
installing a `natsserver.Logger` adapter with `SetLoggerV2` before
starting the server. Add coverage for severity mapping, formatting,
startup logging, and debug filtering, and allow expected NATS errors in
the existing failure-path and teardown tests.
Refs GRU-98.
<details>
<summary>Implementation decisions</summary>
- Preserve the caller's logger fields and add the `server` logger name.
- Let slog filter debug output; leave protocol and system-account
tracing disabled to avoid logging message payloads.
- Map NATS fatal messages to slog `Critical` so adding logging does not
introduce process termination.
- Restrict test log allowances to the embedded server's expected errors
rather than ignoring all error logs.
</details>
Generated by Coder Agents on behalf of @spikecurtis.
`TestPubsub/CloseIdempotent` in `coderd/nats` flakes in CI with
"embedded nats server not ready within 10s" (for example in
[`test-go-pg-17` on
main](https://github.com/coder/coder/actions/runs/39412671012/job/118594196090)).
It is the only subtest that calls `nats.New` with `nats.Options{}`
directly. With `ClusterPort` unset, `buildServerOptions` falls back to
the fixed production cluster port 6222. Test packages run in parallel,
and `cli` `TestServer/SpammyLogs` runs `coder server --cluster-host
127.0.0.1`, which holds 127.0.0.1:6222. When the route listener cannot
bind, nats-server logs nothing under `NoLog`, and `ReadyForConnections`
waits out the 10 s `readyTimeout`.
The fix gives the subtest `ClusterPort: natsserver.RANDOM_PORT`, the
option the `newPubsub` helper already sets for every other subtest. It
is a one-line, test-only change.
## Testing
- Red on the merge base: with 127.0.0.1:6222 held by a plain listener,
`TestPubsub/CloseIdempotent` fails after 10.01 s with the CI message.
- Green on this branch: with the port held, `go test ./coderd/nats/...`
passes.
- `go test ./coderd/nats/... -count=3` and `make pre-commit` pass.
- Remote dogfood UAT ran on the tested SHA 451c4ecd16 and passed. It
repeated red and green with 127.0.0.1:6222 and 0.0.0.0:6222 held, ran
`-count=5` with no port held, and showed that an overlay whose second
`Close` returns an error makes the subtest fail, so it still checks
idempotent `Close`.
Polish and the test audit made no changes, so both stayed inside the
implementation diff (`coderd/nats/pubsub_test.go`).
Draft #30759 carries the same one-line edit as a drive-by, so whichever
lands second merges cleanly.
> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.