Skip to content

fix(coderd/prometheusmetrics): close batcher to force flush before asserting agent stats #9465

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
Aug 31, 2023
Merged
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
15 changes: 11 additions & 4 deletions coderd/prometheusmetrics/prometheusmetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,15 @@ func TestAgentStats(t *testing.T) {
t.Cleanup(cancelFunc)

db, pubsub := dbtestutil.NewDB(t)
log := slogtest.Make(t, nil)
log := slogtest.Make(t, nil).Leveled(slog.LevelDebug)

batcher, closeBatcher, err := batchstats.New(ctx,
// We had previously set the batch size to 1 here, but that caused
// intermittent test flakes due to a race between the batcher completing
// its flush and the test asserting that the metrics were collected.
// Instead, we close the batcher after all stats have been posted, which
// forces a flush.
batchstats.WithStore(db),
// We want our stats, and we want them NOW.
batchstats.WithBatchSize(1),
batchstats.WithInterval(time.Hour),
batchstats.WithLogger(log),
)
require.NoError(t, err, "create stats batcher failed")
Expand Down Expand Up @@ -435,6 +437,11 @@ func TestAgentStats(t *testing.T) {
require.NoError(t, err)
}

// Ensure that all stats are flushed to the database
// before we query them. We do not expect any more stats
// to be posted after this.
closeBatcher()

// when
//
// Set initialCreateAfter to some time in the past, so that AgentStats would include all above PostStats,
Expand Down