Skip to content

Commit 70af2c3

Browse files
committed
chore: remove global retention fallback for workspace agent logs
Workspace agent logs retention is now explicit - it's enabled when --workspace-agent-logs-retention is set to a non-zero duration (default 7d), and disabled when set to 0. No fallback to global retention.
1 parent 62e3ab8 commit 70af2c3

File tree

12 files changed

+46
-54
lines changed

12 files changed

+46
-54
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,9 +1784,9 @@ func (q *querier) DeleteOldTelemetryLocks(ctx context.Context, beforeTime time.T
17841784
return q.db.DeleteOldTelemetryLocks(ctx, beforeTime)
17851785
}
17861786

1787-
func (q *querier) DeleteOldWorkspaceAgentLogs(ctx context.Context, threshold time.Time) error {
1787+
func (q *querier) DeleteOldWorkspaceAgentLogs(ctx context.Context, threshold time.Time) (int64, error) {
17881788
if err := q.authorizeContext(ctx, policy.ActionDelete, rbac.ResourceSystem); err != nil {
1789-
return err
1789+
return 0, err
17901790
}
17911791
return q.db.DeleteOldWorkspaceAgentLogs(ctx, threshold)
17921792
}

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3227,7 +3227,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
32273227
}))
32283228
s.Run("DeleteOldWorkspaceAgentLogs", s.Mocked(func(dbm *dbmock.MockStore, _ *gofakeit.Faker, check *expects) {
32293229
t := time.Time{}
3230-
dbm.EXPECT().DeleteOldWorkspaceAgentLogs(gomock.Any(), t).Return(nil).AnyTimes()
3230+
dbm.EXPECT().DeleteOldWorkspaceAgentLogs(gomock.Any(), t).Return(int64(0), nil).AnyTimes()
32313231
check.Args(t).Asserts(rbac.ResourceSystem, policy.ActionDelete)
32323232
}))
32333233
s.Run("InsertWorkspaceAgentStats", s.Mocked(func(dbm *dbmock.MockStore, _ *gofakeit.Faker, check *expects) {

coderd/database/dbmetrics/querymetrics.go

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

coderd/database/dbmock/dbmock.go

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

coderd/database/dbpurge/dbpurge.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, vals *coder
6565
return nil
6666
}
6767

68+
var purgedWorkspaceAgentLogs int64
6869
workspaceAgentLogsRetention := vals.Retention.WorkspaceAgentLogs.Value()
69-
if workspaceAgentLogsRetention == 0 {
70-
workspaceAgentLogsRetention = vals.Retention.Global.Value()
71-
}
7270
if workspaceAgentLogsRetention > 0 {
7371
deleteOldWorkspaceAgentLogsBefore := start.Add(-workspaceAgentLogsRetention)
74-
if err := tx.DeleteOldWorkspaceAgentLogs(ctx, deleteOldWorkspaceAgentLogsBefore); err != nil {
72+
purgedWorkspaceAgentLogs, err = tx.DeleteOldWorkspaceAgentLogs(ctx, deleteOldWorkspaceAgentLogsBefore)
73+
if err != nil {
7574
return xerrors.Errorf("failed to delete old workspace agent logs: %w", err)
7675
}
7776
}
@@ -153,6 +152,7 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, vals *coder
153152
}
154153

155154
logger.Debug(ctx, "purged old database entries",
155+
slog.F("workspace_agent_logs", purgedWorkspaceAgentLogs),
156156
slog.F("expired_api_keys", expiredAPIKeys),
157157
slog.F("aibridge_records", purgedAIBridgeRecords),
158158
slog.F("connection_logs", purgedConnectionLogs),

coderd/database/dbpurge/dbpurge_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,7 @@ func TestDeleteOldWorkspaceAgentLogsRetention(t *testing.T) {
423423
logsAge: 60 * 24 * time.Hour, // 60 days ago
424424
expectDeleted: false,
425425
},
426-
{
427-
name: "GlobalRetentionFallback",
428-
retentionConfig: codersdk.RetentionConfig{
429-
Global: serpent.Duration(14 * 24 * time.Hour), // 14 days global
430-
WorkspaceAgentLogs: serpent.Duration(0), // Not set, falls back to global
431-
},
432-
logsAge: 15 * 24 * time.Hour, // 15 days ago
433-
expectDeleted: true,
434-
},
426+
435427
{
436428
name: "CustomRetention30Days",
437429
retentionConfig: codersdk.RetentionConfig{

coderd/database/querier.go

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

coderd/database/queries.sql.go

Lines changed: 8 additions & 5 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ INSERT INTO
199199
-- name: GetWorkspaceAgentLogSourcesByAgentIDs :many
200200
SELECT * FROM workspace_agent_log_sources WHERE workspace_agent_id = ANY(@ids :: uuid [ ]);
201201

202-
-- If an agent hasn't connected in the last 7 days, we purge it's logs.
202+
-- If an agent hasn't connected within the retention period, we purge its logs.
203203
-- Exception: if the logs are related to the latest build, we keep those around.
204204
-- Logs can take up a lot of space, so it's important we clean up frequently.
205-
-- name: DeleteOldWorkspaceAgentLogs :exec
205+
-- name: DeleteOldWorkspaceAgentLogs :execrows
206206
WITH
207207
latest_builds AS (
208208
SELECT

docs/admin/setup/data-retention.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ a YAML configuration file.
2626

2727
### Settings
2828

29-
| Setting | CLI Flag | Environment Variable | Default | Description |
30-
|----------------------|------------------------------------|----------------------------------------|----------------|------------------------------------------|
31-
| Audit Logs | `--audit-logs-retention` | `CODER_AUDIT_LOGS_RETENTION` | `0` (disabled) | How long to retain Audit Log entries |
32-
| Connection Logs | `--connection-logs-retention` | `CODER_CONNECTION_LOGS_RETENTION` | `0` (disabled) | How long to retain Connection Logs |
33-
| API Keys | `--api-keys-retention` | `CODER_API_KEYS_RETENTION` | `7d` | How long to retain expired API keys |
34-
| Workspace Agent Logs | `--workspace-agent-logs-retention` | `CODER_WORKSPACE_AGENT_LOGS_RETENTION` | `7d` | How long to retain workspace agent logs |
29+
| Setting | CLI Flag | Environment Variable | Default | Description |
30+
|----------------------|------------------------------------|----------------------------------------|----------------|-----------------------------------------|
31+
| Audit Logs | `--audit-logs-retention` | `CODER_AUDIT_LOGS_RETENTION` | `0` (disabled) | How long to retain Audit Log entries |
32+
| Connection Logs | `--connection-logs-retention` | `CODER_CONNECTION_LOGS_RETENTION` | `0` (disabled) | How long to retain Connection Logs |
33+
| API Keys | `--api-keys-retention` | `CODER_API_KEYS_RETENTION` | `7d` | How long to retain expired API keys |
34+
| Workspace Agent Logs | `--workspace-agent-logs-retention` | `CODER_WORKSPACE_AGENT_LOGS_RETENTION` | `7d` | How long to retain workspace agent logs |
3535

3636
### Duration Format
3737

@@ -107,13 +107,14 @@ error message when users attempt to use an expired key.
107107

108108
### Workspace Agent Logs Behavior
109109

110-
Workspace agent logs are retained based on the retention period, but **logs from
111-
the latest build of each workspace are always retained** regardless of age. This
112-
ensures you can always debug issues with active workspaces.
110+
Workspace agent logs are deleted based on when the agent last connected, not the
111+
age of the logs themselves. **Logs from the latest build of each workspace are
112+
always retained** regardless of when the agent last connected. This ensures you
113+
can always debug issues with active workspaces.
113114

114-
Only logs from non-latest workspace builds that are older than the retention
115-
period are deleted. Setting `--workspace-agent-logs-retention=7d` keeps all logs
116-
from the latest build plus logs from previous builds for up to 7 days.
115+
For non-latest builds, logs are deleted if the agent hasn't connected within the
116+
retention period. Setting `--workspace-agent-logs-retention=7d` deletes logs for
117+
agents that haven't connected in 7 days (excluding those from the latest build).
117118

118119
## Best Practices
119120

0 commit comments

Comments
 (0)