nolint:paralleltest // It uses LockIDDBPurge.
(t *testing.T)
| 70 | |
| 71 | //nolint:paralleltest // It uses LockIDDBPurge. |
| 72 | func TestMetrics(t *testing.T) { |
| 73 | t.Run("SuccessfulIteration", func(t *testing.T) { |
| 74 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 75 | defer cancel() |
| 76 | |
| 77 | reg := prometheus.NewRegistry() |
| 78 | clk := quartz.NewMock(t) |
| 79 | now := time.Date(2025, 1, 15, 7, 30, 0, 0, time.UTC) |
| 80 | clk.Set(now).MustWait(ctx) |
| 81 | |
| 82 | db, _ := dbtestutil.NewDB(t) |
| 83 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}) |
| 84 | user := dbgen.User(t, db, database.User{}) |
| 85 | |
| 86 | oldExpiredKey, _ := dbgen.APIKey(t, db, database.APIKey{ |
| 87 | UserID: user.ID, |
| 88 | ExpiresAt: now.Add(-8 * 24 * time.Hour), // Expired 8 days ago |
| 89 | TokenName: "old-expired-key", |
| 90 | }) |
| 91 | |
| 92 | _, err := db.GetAPIKeyByID(ctx, oldExpiredKey.ID) |
| 93 | require.NoError(t, err, "key should exist before purge") |
| 94 | |
| 95 | done := awaitDoTick(ctx, t, clk) |
| 96 | closer := dbpurge.New(ctx, logger, db, &codersdk.DeploymentValues{ |
| 97 | Retention: codersdk.RetentionConfig{ |
| 98 | APIKeys: serpent.Duration(7 * 24 * time.Hour), // 7 days retention |
| 99 | }, |
| 100 | }, reg, nopAuditorPtr(t), dbpurge.WithClock(clk)) |
| 101 | defer closer.Close() |
| 102 | testutil.TryReceive(ctx, t, done) |
| 103 | |
| 104 | hist := promhelp.HistogramValue(t, reg, "coderd_dbpurge_iteration_duration_seconds", prometheus.Labels{ |
| 105 | "success": "true", |
| 106 | }) |
| 107 | require.NotNil(t, hist) |
| 108 | require.Greater(t, hist.GetSampleCount(), uint64(0), "should have at least one sample") |
| 109 | |
| 110 | expiredAPIKeys := promhelp.CounterValue(t, reg, "coderd_dbpurge_records_purged_total", prometheus.Labels{ |
| 111 | "record_type": "expired_api_keys", |
| 112 | }) |
| 113 | require.Greater(t, expiredAPIKeys, 0, "should have deleted at least one expired API key") |
| 114 | |
| 115 | _, err = db.GetAPIKeyByID(ctx, oldExpiredKey.ID) |
| 116 | require.Error(t, err, "key should be deleted after purge") |
| 117 | |
| 118 | workspaceAgentLogs := promhelp.CounterValue(t, reg, "coderd_dbpurge_records_purged_total", prometheus.Labels{ |
| 119 | "record_type": "workspace_agent_logs", |
| 120 | }) |
| 121 | require.GreaterOrEqual(t, workspaceAgentLogs, 0) |
| 122 | |
| 123 | aibridgeRecords := promhelp.CounterValue(t, reg, "coderd_dbpurge_records_purged_total", prometheus.Labels{ |
| 124 | "record_type": "aibridge_records", |
| 125 | }) |
| 126 | require.GreaterOrEqual(t, aibridgeRecords, 0) |
| 127 | |
| 128 | connectionLogs := promhelp.CounterValue(t, reg, "coderd_dbpurge_records_purged_total", prometheus.Labels{ |
| 129 | "record_type": "connection_logs", |
nothing calls this directly
no test coverage detected