Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

test: fix flake, TestLabelsAggregation update & collect timing#19654

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
Emyrk wants to merge3 commits intomainfromstevenmasley/TestLabelsAggregation
Closed
Changes fromall 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
25 changes: 22 additions & 3 deletionscoderd/prometheusmetrics/aggregator_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
package prometheusmetrics_test

import (
"bytes"
"context"
"fmt"
"sort"
Expand All@@ -14,10 +15,11 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/agentmetrics"

agentproto "github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/agentmetrics"
"github.com/coder/coder/v2/coderd/prometheusmetrics"
"github.com/coder/coder/v2/cryptorand"
"github.com/coder/coder/v2/testutil"
Expand DownExpand Up@@ -592,7 +594,15 @@ func TestLabelsAggregation(t *testing.T) {

// given
registry := prometheus.NewRegistry()
metricsAggregator, err := prometheusmetrics.NewMetricsAggregator(slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}), registry, time.Hour, tc.aggregateOn) // time.Hour, so metrics won't expire

logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
logger = logger.Leveled(slog.LevelDebug)

// updateWatch is how we know the update was processed before we collect
var updateWatch bytes.Buffer
logger = logger.AppendSinks(sloghuman.Sink(&updateWatch))

metricsAggregator, err := prometheusmetrics.NewMetricsAggregator(logger, registry, time.Hour, tc.aggregateOn) // time.Hour, so metrics won't expire
require.NoError(t, err)

ctx, cancelFunc := context.WithCancel(context.Background())
Expand All@@ -606,6 +616,15 @@ func TestLabelsAggregation(t *testing.T) {
metricsAggregator.Update(ctx, sc.labels, sc.metrics)
}

// This code makes sure all updates are processed before we start a collection.
// We do this by grepping the logs for "update metrics" messages, which are logged
// every time an update is processed.
//
// This is not ideal as the code relies on implementation details (the log message),
require.Eventually(t, func() bool {
return strings.Count(updateWatch.String(), "update metrics") == len(tc.given)
}, testutil.WaitMedium, testutil.IntervalFast)
Comment on lines +619 to +626
Copy link
MemberAuthor

@EmyrkEmyrkAug 30, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is a really unfortunate fix. There is no way to hook into the timing. Maybe with quartz?

The issue istime.Now() is called before the channel enqueue. So to do this with quartz, we would need to add some othertime.Now() function in the update select case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This may need to be some kind of buffer protected by a mutex, or perhaps a buffered channel instead?


// then
require.Eventually(t, func() bool {
var actual []prometheus.Metric
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp