Add default-level metrics and warn-level diagnostics to investigate
ServerTailnet reachability failures causing agent-specific 502s, without
restarting coderd or enabling debug logging.
- Enrich request logs and PTY/chatd warnings with agent ID, failure
reason, wait duration, coordinator state, WireGuard handshake and
byte counts, and DERP regions.
- Add metrics for unreachable attempts by reason, reachability latency,
embedded DERP connections, and offline app/terminal requests.
- Distinguish reachability failures from subsequent TCP dial failures,
cap reverse proxy dials at one minute, and exclude expected chatd
readiness polls and superseded-agent dials from unreachable counts.
- Report missing handshakes as zero time instead of 1970, also fixing
`coder ping`.
Part of PLAT-680, a sub-issue of PLAT-360.
Fixes CLI-9
The `goleak` check in `cli` has been flaking on an idle HTTP connection.
It turns out that's coming from `coder server` shutting down, not the
OIDC links command.
`API.Close` cancels the API context well before it closes the server
tailnet, and since #14680 the embedded DERP region dialer returns `nil`
once that context is done. Unfortunately, `nil` tells `derphttp` to dial
the region over the network instead. So the server dials its own access
URL (`example.com` in tests), gets a 404, falls back to WebSocket, and
our tailscale fork leaves that failed WebSocket connection open forever.
This has only been flaking since #29462, which has the server tailnet
fetch the DERP map straight away rather than after the first 5 second
tick. Before that, most server tests had finished before the tailnet
ever connected to DERP, so there was nothing to reconnect on shutdown.
To fix, the embedded region now gets an already closed conn once
shutdown starts, so the DERP handshake fails and magicsock just backs
off until the tailnet closes. I reproduced the leak by pointing a server
tailnet's embedded region at `example.com` and cancelling the context;
it fails `goleak` without this change and passes with it.
chore of #14729
Refactors the `ServerTailnet` to use `tailnet.Controller` so that we reuse logic around reconnection and handling control messages, instead of reimplementing. This unifies our "client" use of the tailscale API across CLI, coderd, and wsproxy.
I think this will resolve#12136 but lets get a proper test at the system level before closing.
Before this change, we only register the node callback at start of day for the server tailnet. If the coordinator changes, like we know happens when we are licensed for the PGCoordinator, we close the connection to the old coord, and open a new one to the new coord.
The callback is designed to direct the updates to the new coordinator, but there is nothing that specifically triggers it to fire after we connect to the new coordinator.
If we have STUN, then period re-STUNs will generally get it to fire eventually, but without STUN it we could go indefinitely without a callback.
This PR changes the servertailnet to re-register the callback each time we reconnect to the coordinator. Registering a callback (even if it's the same callback) triggers an immediate call with our node information, so the new coordinator will have it.
Adds support to `ServerTailnet` to set all peers lost before attempting to reconnect to the coordinator. In practice, this only really affects `wsproxy` since coderd has a local connection to the coordinator that only goes down if we're shutting down or change licenses.