- Notifications
You must be signed in to change notification settings - Fork1k
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package prometheusmetrics_test | ||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"sort" | ||
@@ -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" | ||
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" | ||
@@ -592,7 +594,15 @@ func TestLabelsAggregation(t *testing.T) { | ||
// given | ||
registry := prometheus.NewRegistry() | ||
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()) | ||
@@ -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 MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 is There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
Uh oh!
There was an error while loading.Please reload this page.