Skip to content

Commit 887014c

Browse files
committed
feat: add level support for startup logs
This allows external services like our devcontainer support to display errors and warnings with custom styles to indicate failures to users.
1 parent e5c6ebd commit 887014c

17 files changed

+89
-25
lines changed

coderd/apidoc/docs.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbfake/databasefake.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3706,6 +3706,7 @@ func (q *fakeQuerier) InsertWorkspaceAgentStartupLogs(_ context.Context, arg dat
37063706
ID: id,
37073707
AgentID: arg.AgentID,
37083708
CreatedAt: arg.CreatedAt[index],
3709+
Level: arg.Level[index],
37093710
Output: output,
37103711
})
37113712
outputLength += int32(len(output))

coderd/database/dump.sql

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE workspace_agent_startup_logs
2+
DROP COLUMN level;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE workspace_agent_startup_logs
2+
ADD COLUMN level log_level NOT NULL DEFAULT 'info'::log_level;

coderd/database/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func TestInsertWorkspaceAgentStartupLogs(t *testing.T) {
111111
AgentID: agent.ID,
112112
CreatedAt: []time.Time{database.Now()},
113113
Output: []string{"first"},
114+
Level: []database.LogLevel{database.LogLevelInfo},
114115
// 1 MB is the max
115116
OutputLength: 1 << 20,
116117
})
@@ -121,6 +122,7 @@ func TestInsertWorkspaceAgentStartupLogs(t *testing.T) {
121122
AgentID: agent.ID,
122123
CreatedAt: []time.Time{database.Now()},
123124
Output: []string{"second"},
125+
Level: []database.LogLevel{database.LogLevelInfo},
124126
OutputLength: 1,
125127
})
126128
require.True(t, database.IsStartupLogsLimitError(err))

coderd/database/queries.sql.go

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagents.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,12 @@ WITH new_length AS (
151151
startup_logs_length = startup_logs_length + @output_length WHERE workspace_agents.id = @agent_id
152152
)
153153
INSERT INTO
154-
workspace_agent_startup_logs
154+
workspace_agent_startup_logs (agent_id, created_at, output, level)
155155
SELECT
156156
@agent_id :: uuid AS agent_id,
157157
unnest(@created_at :: timestamptz [ ]) AS created_at,
158-
unnest(@output :: VARCHAR(1024) [ ]) AS output
158+
unnest(@output :: VARCHAR(1024) [ ]) AS output,
159+
unnest(@level :: log_level [ ]) AS level
159160
RETURNING workspace_agent_startup_logs.*;
160161

161162
-- If an agent hasn't connected in the last 7 days, we purge it's logs.

coderd/workspaceagents.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,31 @@ func (api *API) patchWorkspaceAgentStartupLogs(rw http.ResponseWriter, r *http.R
256256
}
257257
createdAt := make([]time.Time, 0)
258258
output := make([]string, 0)
259+
level := make([]database.LogLevel, 0)
259260
outputLength := 0
260261
for _, log := range req.Logs {
261262
createdAt = append(createdAt, log.CreatedAt)
262263
output = append(output, log.Output)
263264
outputLength += len(log.Output)
265+
if log.Level == "" {
266+
// Default to "info" to support older agents that didn't have the level field.
267+
log.Level = codersdk.LogLevelInfo
268+
}
269+
parsedLevel := database.LogLevel(log.Level)
270+
if !parsedLevel.Valid() {
271+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
272+
Message: "Invalid log level provided.",
273+
Detail: fmt.Sprintf("invalid log level: %q", log.Level),
274+
})
275+
return
276+
}
277+
level = append(level, parsedLevel)
264278
}
265279
logs, err := api.Database.InsertWorkspaceAgentStartupLogs(ctx, database.InsertWorkspaceAgentStartupLogsParams{
266280
AgentID: workspaceAgent.ID,
267281
CreatedAt: createdAt,
268282
Output: output,
283+
Level: level,
269284
OutputLength: int32(outputLength),
270285
})
271286
if err != nil {
@@ -1971,5 +1986,6 @@ func convertWorkspaceAgentStartupLog(log database.WorkspaceAgentStartupLog) code
19711986
ID: log.ID,
19721987
CreatedAt: log.CreatedAt,
19731988
Output: log.Output,
1989+
Level: codersdk.LogLevel(log.Level),
19741990
}
19751991
}

codersdk/agentsdk/agentsdk.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,9 @@ func (c *Client) PostStartup(ctx context.Context, req PostStartupRequest) error
545545
}
546546

547547
type StartupLog struct {
548-
CreatedAt time.Time `json:"created_at"`
549-
Output string `json:"output"`
548+
CreatedAt time.Time `json:"created_at"`
549+
Output string `json:"output"`
550+
Level codersdk.LogLevel `json:"level"`
550551
}
551552

552553
type PatchStartupLogs struct {

codersdk/workspaceagents.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,5 @@ type WorkspaceAgentStartupLog struct {
510510
ID int64 `json:"id"`
511511
CreatedAt time.Time `json:"created_at" format:"date-time"`
512512
Output string `json:"output"`
513+
Level LogLevel `json:"level"`
513514
}

docs/api/agents.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ curl -X GET http://coder-server:8080/api/v2/workspaceagents/{workspaceagent}/sta
701701
{
702702
"created_at": "2019-08-24T14:15:22Z",
703703
"id": 0,
704+
"level": "trace",
704705
"output": "string"
705706
}
706707
]
@@ -716,11 +717,22 @@ curl -X GET http://coder-server:8080/api/v2/workspaceagents/{workspaceagent}/sta
716717

717718
Status Code **200**
718719

719-
| Name | Type | Required | Restrictions | Description |
720-
| -------------- | ----------------- | -------- | ------------ | ----------- |
721-
| `[array item]` | array | false | | |
722-
| `» created_at` | string(date-time) | false | | |
723-
| `» id` | integer | false | | |
724-
| `» output` | string | false | | |
720+
| Name | Type | Required | Restrictions | Description |
721+
| -------------- | ------------------------------------------------ | -------- | ------------ | ----------- |
722+
| `[array item]` | array | false | | |
723+
| `» created_at` | string(date-time) | false | | |
724+
| `» id` | integer | false | | |
725+
| `» level` | [codersdk.LogLevel](schemas.md#codersdkloglevel) | false | | |
726+
| `» output` | string | false | | |
727+
728+
#### Enumerated Values
729+
730+
| Property | Value |
731+
| -------- | ------- |
732+
| `level` | `trace` |
733+
| `level` | `debug` |
734+
| `level` | `info` |
735+
| `level` | `warn` |
736+
| `level` | `error` |
725737

726738
To perform this operation, you must be authenticated. [Learn more](authentication.md).

docs/api/schemas.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
"logs": [
218218
{
219219
"created_at": "string",
220+
"level": "trace",
220221
"output": "string"
221222
}
222223
]
@@ -302,16 +303,18 @@
302303
```json
303304
{
304305
"created_at": "string",
306+
"level": "trace",
305307
"output": "string"
306308
}
307309
```
308310

309311
### Properties
310312

311-
| Name | Type | Required | Restrictions | Description |
312-
| ------------ | ------ | -------- | ------------ | ----------- |
313-
| `created_at` | string | false | | |
314-
| `output` | string | false | | |
313+
| Name | Type | Required | Restrictions | Description |
314+
| ------------ | -------------------------------------- | -------- | ------------ | ----------- |
315+
| `created_at` | string | false | | |
316+
| `level` | [codersdk.LogLevel](#codersdkloglevel) | false | | |
317+
| `output` | string | false | | |
315318

316319
## agentsdk.Stats
317320

@@ -4766,17 +4769,19 @@ Parameter represents a set value for the scope.
47664769
{
47674770
"created_at": "2019-08-24T14:15:22Z",
47684771
"id": 0,
4772+
"level": "trace",
47694773
"output": "string"
47704774
}
47714775
```
47724776

47734777
### Properties
47744778

4775-
| Name | Type | Required | Restrictions | Description |
4776-
| ------------ | ------- | -------- | ------------ | ----------- |
4777-
| `created_at` | string | false | | |
4778-
| `id` | integer | false | | |
4779-
| `output` | string | false | | |
4779+
| Name | Type | Required | Restrictions | Description |
4780+
| ------------ | -------------------------------------- | -------- | ------------ | ----------- |
4781+
| `created_at` | string | false | | |
4782+
| `id` | integer | false | | |
4783+
| `level` | [codersdk.LogLevel](#codersdkloglevel) | false | | |
4784+
| `output` | string | false | | |
47804785

47814786
## codersdk.WorkspaceAgentStatus
47824787

site/src/api/typesGenerated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ export interface WorkspaceAgentStartupLog {
11311131
readonly id: number
11321132
readonly created_at: string
11331133
readonly output: string
1134+
readonly level: LogLevel
11341135
}
11351136

11361137
// From codersdk/workspaceapps.go

site/src/xServices/workspaceAgentLogs/workspaceAgentLogsXService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const workspaceAgentLogsMachine = createMachine(
7373
API.getWorkspaceAgentStartupLogs(ctx.agentID).then((data) =>
7474
data.map((log) => ({
7575
id: log.id,
76-
level: "info" as TypesGen.LogLevel,
76+
level: log.level || "info",
7777
output: log.output,
7878
time: log.created_at,
7979
})),

0 commit comments

Comments
 (0)