Skip to content

feat: add level support for startup logs #7067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/database/dbfake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -3706,6 +3706,7 @@ func (q *fakeQuerier) InsertWorkspaceAgentStartupLogs(_ context.Context, arg dat
ID: id,
AgentID: arg.AgentID,
CreatedAt: arg.CreatedAt[index],
Level: arg.Level[index],
Output: output,
})
outputLength += int32(len(output))
Expand Down
3 changes: 2 additions & 1 deletion coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions coderd/database/migrations/000116_startup_logs_level.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE workspace_agent_startup_logs
DROP COLUMN level;
2 changes: 2 additions & 0 deletions coderd/database/migrations/000116_startup_logs_level.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE workspace_agent_startup_logs
ADD COLUMN level log_level NOT NULL DEFAULT 'info'::log_level;
1 change: 1 addition & 0 deletions coderd/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions coderd/database/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func TestInsertWorkspaceAgentStartupLogs(t *testing.T) {
AgentID: agent.ID,
CreatedAt: []time.Time{database.Now()},
Output: []string{"first"},
Level: []database.LogLevel{database.LogLevelInfo},
// 1 MB is the max
OutputLength: 1 << 20,
})
Expand All @@ -121,6 +122,7 @@ func TestInsertWorkspaceAgentStartupLogs(t *testing.T) {
AgentID: agent.ID,
CreatedAt: []time.Time{database.Now()},
Output: []string{"second"},
Level: []database.LogLevel{database.LogLevelInfo},
OutputLength: 1,
})
require.True(t, database.IsStartupLogsLimitError(err))
Expand Down
15 changes: 10 additions & 5 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions coderd/database/queries/workspaceagents.sql
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ WITH new_length AS (
startup_logs_length = startup_logs_length + @output_length WHERE workspace_agents.id = @agent_id
)
INSERT INTO
workspace_agent_startup_logs
workspace_agent_startup_logs (agent_id, created_at, output, level)
SELECT
@agent_id :: uuid AS agent_id,
unnest(@created_at :: timestamptz [ ]) AS created_at,
unnest(@output :: VARCHAR(1024) [ ]) AS output
unnest(@output :: VARCHAR(1024) [ ]) AS output,
unnest(@level :: log_level [ ]) AS level
RETURNING workspace_agent_startup_logs.*;

-- If an agent hasn't connected in the last 7 days, we purge it's logs.
Expand Down
16 changes: 16 additions & 0 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,31 @@ func (api *API) patchWorkspaceAgentStartupLogs(rw http.ResponseWriter, r *http.R
}
createdAt := make([]time.Time, 0)
output := make([]string, 0)
level := make([]database.LogLevel, 0)
outputLength := 0
for _, log := range req.Logs {
createdAt = append(createdAt, log.CreatedAt)
output = append(output, log.Output)
outputLength += len(log.Output)
if log.Level == "" {
// Default to "info" to support older agents that didn't have the level field.
log.Level = codersdk.LogLevelInfo
}
parsedLevel := database.LogLevel(log.Level)
if !parsedLevel.Valid() {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid log level provided.",
Detail: fmt.Sprintf("invalid log level: %q", log.Level),
})
return
}
level = append(level, parsedLevel)
}
logs, err := api.Database.InsertWorkspaceAgentStartupLogs(ctx, database.InsertWorkspaceAgentStartupLogsParams{
AgentID: workspaceAgent.ID,
CreatedAt: createdAt,
Output: output,
Level: level,
OutputLength: int32(outputLength),
})
if err != nil {
Expand Down Expand Up @@ -1971,5 +1986,6 @@ func convertWorkspaceAgentStartupLog(log database.WorkspaceAgentStartupLog) code
ID: log.ID,
CreatedAt: log.CreatedAt,
Output: log.Output,
Level: codersdk.LogLevel(log.Level),
}
}
5 changes: 3 additions & 2 deletions codersdk/agentsdk/agentsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,9 @@ func (c *Client) PostStartup(ctx context.Context, req PostStartupRequest) error
}

type StartupLog struct {
CreatedAt time.Time `json:"created_at"`
Output string `json:"output"`
CreatedAt time.Time `json:"created_at"`
Output string `json:"output"`
Level codersdk.LogLevel `json:"level"`
}

type PatchStartupLogs struct {
Expand Down
1 change: 1 addition & 0 deletions codersdk/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,5 @@ type WorkspaceAgentStartupLog struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
Output string `json:"output"`
Level LogLevel `json:"level"`
}
24 changes: 18 additions & 6 deletions docs/api/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ curl -X GET http://coder-server:8080/api/v2/workspaceagents/{workspaceagent}/sta
{
"created_at": "2019-08-24T14:15:22Z",
"id": 0,
"level": "trace",
"output": "string"
}
]
Expand All @@ -716,11 +717,22 @@ curl -X GET http://coder-server:8080/api/v2/workspaceagents/{workspaceagent}/sta

Status Code **200**

| Name | Type | Required | Restrictions | Description |
| -------------- | ----------------- | -------- | ------------ | ----------- |
| `[array item]` | array | false | | |
| `» created_at` | string(date-time) | false | | |
| `» id` | integer | false | | |
| `» output` | string | false | | |
| Name | Type | Required | Restrictions | Description |
| -------------- | ------------------------------------------------ | -------- | ------------ | ----------- |
| `[array item]` | array | false | | |
| `» created_at` | string(date-time) | false | | |
| `» id` | integer | false | | |
| `» level` | [codersdk.LogLevel](schemas.md#codersdkloglevel) | false | | |
| `» output` | string | false | | |

#### Enumerated Values

| Property | Value |
| -------- | ------- |
| `level` | `trace` |
| `level` | `debug` |
| `level` | `info` |
| `level` | `warn` |
| `level` | `error` |

To perform this operation, you must be authenticated. [Learn more](authentication.md).
23 changes: 14 additions & 9 deletions docs/api/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
"logs": [
{
"created_at": "string",
"level": "trace",
"output": "string"
}
]
Expand Down Expand Up @@ -302,16 +303,18 @@
```json
{
"created_at": "string",
"level": "trace",
"output": "string"
}
```

### Properties

| Name | Type | Required | Restrictions | Description |
| ------------ | ------ | -------- | ------------ | ----------- |
| `created_at` | string | false | | |
| `output` | string | false | | |
| Name | Type | Required | Restrictions | Description |
| ------------ | -------------------------------------- | -------- | ------------ | ----------- |
| `created_at` | string | false | | |
| `level` | [codersdk.LogLevel](#codersdkloglevel) | false | | |
| `output` | string | false | | |

## agentsdk.Stats

Expand Down Expand Up @@ -4766,17 +4769,19 @@ Parameter represents a set value for the scope.
{
"created_at": "2019-08-24T14:15:22Z",
"id": 0,
"level": "trace",
"output": "string"
}
```

### Properties

| Name | Type | Required | Restrictions | Description |
| ------------ | ------- | -------- | ------------ | ----------- |
| `created_at` | string | false | | |
| `id` | integer | false | | |
| `output` | string | false | | |
| Name | Type | Required | Restrictions | Description |
| ------------ | -------------------------------------- | -------- | ------------ | ----------- |
| `created_at` | string | false | | |
| `id` | integer | false | | |
| `level` | [codersdk.LogLevel](#codersdkloglevel) | false | | |
| `output` | string | false | | |

## codersdk.WorkspaceAgentStatus

Expand Down
1 change: 1 addition & 0 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,7 @@ export interface WorkspaceAgentStartupLog {
readonly id: number
readonly created_at: string
readonly output: string
readonly level: LogLevel
}

// From codersdk/workspaceapps.go
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const workspaceAgentLogsMachine = createMachine(
API.getWorkspaceAgentStartupLogs(ctx.agentID).then((data) =>
data.map((log) => ({
id: log.id,
level: "info" as TypesGen.LogLevel,
level: log.level || "info",
output: log.output,
time: log.created_at,
})),
Expand Down