mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
fix(coderd/prometheusmetrics): close batcher to force flush before asserting agent stats (#9465)
This commit is contained in:
@ -379,13 +379,15 @@ func TestAgentStats(t *testing.T) {
|
|||||||
t.Cleanup(cancelFunc)
|
t.Cleanup(cancelFunc)
|
||||||
|
|
||||||
db, pubsub := dbtestutil.NewDB(t)
|
db, pubsub := dbtestutil.NewDB(t)
|
||||||
log := slogtest.Make(t, nil)
|
log := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
|
||||||
|
|
||||||
batcher, closeBatcher, err := batchstats.New(ctx,
|
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),
|
batchstats.WithStore(db),
|
||||||
// We want our stats, and we want them NOW.
|
|
||||||
batchstats.WithBatchSize(1),
|
|
||||||
batchstats.WithInterval(time.Hour),
|
|
||||||
batchstats.WithLogger(log),
|
batchstats.WithLogger(log),
|
||||||
)
|
)
|
||||||
require.NoError(t, err, "create stats batcher failed")
|
require.NoError(t, err, "create stats batcher failed")
|
||||||
@ -435,6 +437,11 @@ func TestAgentStats(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
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
|
// when
|
||||||
//
|
//
|
||||||
// Set initialCreateAfter to some time in the past, so that AgentStats would include all above PostStats,
|
// Set initialCreateAfter to some time in the past, so that AgentStats would include all above PostStats,
|
||||||
|
Reference in New Issue
Block a user