Skip to content

feat: log and record DERP/direct connection path per agent connection - #30665

Closed
code-asher wants to merge 2 commits into
mainfrom
agent-conn-path-logging
Closed

code-asher wants to merge 2 commits into
mainfrom
agent-conn-path-logging

Conversation

@code-asher

Copy link
Copy Markdown
Member

Log and record whether each agent connection uses DERP or a direct path, so connection problems can be correlated per connection and per client session.

  • tailnet: connections accepted from a Conn listener are now *tailnet.PeerConn. The sending peer is resolved once, when the connection is accepted, from the source address. WireGuard only accepts a source address from the peer it belongs to, so the result is authoritative and doesn't depend on later IP lookups. PeerConn exposes PeerID(), Path() and WatchPath(). Watchers share one 1s poller per Conn, which runs only while something is subscribed. PeerConnFromConn unwraps wrappers that implement NetConn(), which the agent's HTTP-upgraded conns now do.
  • CLI: watchConnChange now logs DERP/direct changes with client_session_id.
  • agent: SSH connections (including VS Code and JetBrains) and reconnecting PTY connections log initial connection path and connection path changed, including peer_id, endpoint and derp_region. They use the existing per-connection loggers, which already carry remote_addr and client_session_id. Connection reports now carry connect_path on both connect and disconnect, and disconnect_path on disconnect. Agent API version is bumped to v2.13.
  • coderd: adds a connection_path enum and the connection_logs.connect_path / disconnect_path columns. The upsert and the batcher dedup both keep the first non-NULL value. As with client_session_id, the columns are not exposed in the API or UI yet.

Older agents and web connections leave both columns NULL.

Decision log
  • The agent can't reuse watchConnChange. It only starts when a TelemetrySink is set, which only happens in workspacesdk.DialAgent, so it runs on the client and never in the agent. It also assumes a single peer, while the agent has one peer per client. It gets a log line for the CLI side, and the agent uses the per-connection PeerConn API.
  • The peer is bound at accept instead of looked up by IP later. Client tailnet IPs are random for each dial (TailscaleServicePrefix.RandomAddr()), so they are not stable identifiers across sessions. Binding the peer once at accept avoids any later IP-based lookup. The physical endpoint (CurAddr) can change while the connection stays up. It is logged as endpoint, and direct-to-direct endpoint changes are logged too.
  • No agent-wide poller. An earlier version of this change used a single agent-wide poller and a map from IP to session ID. It was replaced by per-connection watching, so log lines already carry the connection's own context.
  • Table schema. There is one row per connection: the disconnect is merged into the connect row. Two columns therefore record the path at both moments. The disconnect report repeats connect_path, so the value survives batch dedup or a lost connect report.
  • Unknown paths. Paths that are unspecified or unknown map to NULL, never to an error, so a report is never dropped because of its path.

Generated by Coder Agents on behalf of @code-asher.

tailnet: connections accepted from a Conn listener are now PeerConns that
record the coordinator peer they came from, resolved once at accept time.
PeerConn exposes the current path (DERP or direct) and a WatchPath
subscription backed by a single shared poller per Conn. watchConnChange
also logs path changes on the client, tagged with the client session ID.

agent: SSH connections and reconnecting PTYs log their initial path and
every change, using loggers that already carry the remote address and
client session ID. Connection reports carry the path at connect and at
disconnect (agent API v2.13).

coderd: store the paths in new connection_logs.connect_path and
disconnect_path columns, keeping the first reported value for each.
# Conflicts:
#	coderd/agentapi/connectionlog_test.go
#	coderd/database/dump.sql
#	coderd/database/modelmethods.go
#	coderd/database/models.go
#	coderd/database/queries.sql.go
#	coderd/database/queries/connectionlogs.sql
@code-asher code-asher closed this Oct 15, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 15, 2026
@code-asher
code-asher deleted the agent-conn-path-logging branch October 15, 2026 02:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant