Commit 5fbc409
authored
feat(logging): tag logs with a per-session ID (#1073)
## What
Introduce a per-session identifier and make the extension tag every log
line with it, so all logs for a session can be correlated by searching a
single ID.
This is **Phase 1 of 3** for
[DEVEX-661](https://linear.app/codercom/issue/DEVEX-661) — "VS Code: add
`session_id` to all requests, logs, existing telemetry, and CLI
invocations".
## Changes
- Add `SessionLogger` (`src/logging/sessionLogger.ts`) — a `Logger` that
wraps the Coder output channel and prefixes every message with
`[<sessionId>]`.
- Generate the session ID once in `ServiceContainer` (reusing the
existing `newSessionId()` that already backs the telemetry session, so
logs, telemetry, requests, and the CLI all share one ID), and expose it
via `getSessionId()` for the later phases.
Because every service already receives `Logger` by injection,
session-tagged logging propagates with no call-site changes.
## Design notes
The code owner (Ehab) suggested composing the session ID and the VS Code
logger inside `container.ts`'s `ServiceContainer`; this PR follows that
approach. The telemetry `sessionId` from `newSessionId()` *is* the RFC
`session_id` (confirmed with Ehab — reuse it rather than minting a
second ID).
## Testing
- `pnpm typecheck`, targeted `pnpm lint`, and `pnpm test:extension`
(full suite: 2108 passing).
- New unit tests for `SessionLogger` prefixing and argument forwarding.
<details>
<summary>Implementation plan (DEVEX-661)</summary>
Thread a single per-session identifier (16-byte / 32-char lowercase hex)
so that
logs, API requests, existing telemetry, and the CLI `ssh` invocation the
extension drives can all be correlated by one `session_id`.
**In-scope RFC requirements**
| Req | Summary | Where |
| --- | ------- | ----- |
| 1 | Generate 16-byte / 32-hex session ID | Reuse existing
`newSessionId()` |
| 2 | Session ID on every client log | Phase 1 (this PR) |
| 3 | `session_id` on every API request via baggage | Phase 2 |
| 4.2 | `session_id` on VS Code telemetry | Phase 2 (already covered by
reusing the ID) |
| 5.2/5.3 | `CODER_TRACE_SESSION_ID` via `process.env` + terminal env
collection | Phase 2 |
| 7 | Log workspace/agent/lifecycle state changes at `info` | Phase 3 |
| 16 | Default `--log-dir` + old-log cleanup | Already implemented —
verify only |
**Out of scope:** coderd tracing middleware (req 6),
agent/coordination-protocol
changes and the CLI `ssh` subcommand behavior (reqs 8–15) live in
`coder/coder`.
Req 13 (in-memory log buffer flushed on connection failure) is deferred
to a
follow-up.
**Key decisions**
- D1 (resolved): the telemetry `sessionId` *is* the RFC `session_id` —
one ID,
generated once in `ServiceContainer`.
- D2: session scope is per activation; Remote-SSH opens each workspace
in a fresh
activation, so container-scoped ≈ per-connection.
- D3 (resolved): prefix every log line with the full 32-hex ID as
`[<sessionId>] <message>`.
- D4: `baggage: session_id=<hex>` header.
**Phases**
1. Session ID + `SessionLogger` composition in the container (this PR).
2. Thread the ID to API requests (baggage), telemetry (already covered),
and the
CLI via `CODER_TRACE_SESSION_ID` on `process.env` + the terminal env
collection.
3. Log workspace / agent / lifecycle state transitions at `info`.
</details>
---
🤖 Generated by Coder Agents.1 parent 04503bc commit 5fbc409
5 files changed
Lines changed: 118 additions & 8 deletions
File tree
- src
- core
- logging
- test/unit/logging
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
| |||
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
29 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
| |||
42 | 46 | | |
43 | 47 | | |
44 | 48 | | |
45 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
46 | 59 | | |
47 | 60 | | |
48 | 61 | | |
| |||
56 | 69 | | |
57 | 70 | | |
58 | 71 | | |
59 | | - | |
| 72 | + | |
60 | 73 | | |
61 | 74 | | |
62 | 75 | | |
| |||
187 | 200 | | |
188 | 201 | | |
189 | 202 | | |
190 | | - | |
| 203 | + | |
191 | 204 | | |
192 | 205 | | |
193 | 206 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
| 48 | + | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
| 118 | + | |
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | | - | |
| 129 | + | |
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
75 | 109 | | |
76 | 110 | | |
77 | 111 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
0 commit comments