mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
chore: fix TestServer/Prometheus/DBMetricsDisabled
test flake (#13453)
See: https://github.com/coder/coder/actions/runs/9352137263/job/25739550487#step:5:368
This commit is contained in:
@ -967,26 +967,32 @@ func TestServer(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
// nolint:bodyclose
|
// nolint:bodyclose
|
||||||
res, err = http.DefaultClient.Do(req)
|
res, err = http.DefaultClient.Do(req)
|
||||||
return err == nil
|
if err != nil {
|
||||||
}, testutil.WaitShort, testutil.IntervalFast)
|
return false
|
||||||
defer res.Body.Close()
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(res.Body)
|
scanner := bufio.NewScanner(res.Body)
|
||||||
hasActiveUsers := false
|
hasActiveUsers := false
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
// This metric is manually registered to be tracked in the server. That's
|
// This metric is manually registered to be tracked in the server. That's
|
||||||
// why we test it's tracked here.
|
// why we test it's tracked here.
|
||||||
if strings.HasPrefix(scanner.Text(), "coderd_api_active_users_duration_hour") {
|
if strings.HasPrefix(scanner.Text(), "coderd_api_active_users_duration_hour") {
|
||||||
hasActiveUsers = true
|
hasActiveUsers = true
|
||||||
continue
|
continue
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(scanner.Text(), "coderd_db_query_latencies_seconds") {
|
||||||
|
t.Fatal("db metrics should not be tracked when --prometheus-collect-db-metrics is not enabled")
|
||||||
|
}
|
||||||
|
t.Logf("scanned %s", scanner.Text())
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(scanner.Text(), "coderd_db_query_latencies_seconds") {
|
if scanner.Err() != nil {
|
||||||
t.Fatal("db metrics should not be tracked when --prometheus-collect-db-metrics is not enabled")
|
t.Logf("scanner err: %s", scanner.Err().Error())
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
t.Logf("scanned %s", scanner.Text())
|
|
||||||
}
|
return hasActiveUsers
|
||||||
require.NoError(t, scanner.Err())
|
}, testutil.WaitShort, testutil.IntervalFast, "didn't find coderd_api_active_users_duration_hour in time")
|
||||||
require.True(t, hasActiveUsers)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("DBMetricsEnabled", func(t *testing.T) {
|
t.Run("DBMetricsEnabled", func(t *testing.T) {
|
||||||
@ -1017,20 +1023,25 @@ func TestServer(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
// nolint:bodyclose
|
// nolint:bodyclose
|
||||||
res, err = http.DefaultClient.Do(req)
|
res, err = http.DefaultClient.Do(req)
|
||||||
return err == nil
|
if err != nil {
|
||||||
}, testutil.WaitShort, testutil.IntervalFast)
|
return false
|
||||||
defer res.Body.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(res.Body)
|
|
||||||
hasDBMetrics := false
|
|
||||||
for scanner.Scan() {
|
|
||||||
if strings.HasPrefix(scanner.Text(), "coderd_db_query_latencies_seconds") {
|
|
||||||
hasDBMetrics = true
|
|
||||||
}
|
}
|
||||||
t.Logf("scanned %s", scanner.Text())
|
defer res.Body.Close()
|
||||||
}
|
|
||||||
require.NoError(t, scanner.Err())
|
scanner := bufio.NewScanner(res.Body)
|
||||||
require.True(t, hasDBMetrics)
|
hasDBMetrics := false
|
||||||
|
for scanner.Scan() {
|
||||||
|
if strings.HasPrefix(scanner.Text(), "coderd_db_query_latencies_seconds") {
|
||||||
|
hasDBMetrics = true
|
||||||
|
}
|
||||||
|
t.Logf("scanned %s", scanner.Text())
|
||||||
|
}
|
||||||
|
if scanner.Err() != nil {
|
||||||
|
t.Logf("scanner err: %s", scanner.Err().Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return hasDBMetrics
|
||||||
|
}, testutil.WaitShort, testutil.IntervalFast, "didn't find coderd_db_query_latencies_seconds in time")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
t.Run("GitHubOAuth", func(t *testing.T) {
|
t.Run("GitHubOAuth", func(t *testing.T) {
|
||||||
@ -1347,7 +1358,7 @@ func TestServer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return lastStat.Size() > 0
|
return lastStat.Size() > 0
|
||||||
},
|
},
|
||||||
testutil.WaitShort,
|
dur, //nolint:gocritic
|
||||||
testutil.IntervalFast,
|
testutil.IntervalFast,
|
||||||
"file at %s should exist, last stat: %+v",
|
"file at %s should exist, last stat: %+v",
|
||||||
fiName, lastStat,
|
fiName, lastStat,
|
||||||
|
@ -120,9 +120,9 @@ func Workspaces(ctx context.Context, logger slog.Logger, registerer prometheus.R
|
|||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
// clear all series if there are no database entries
|
// clear all series if there are no database entries
|
||||||
workspaceLatestBuildTotals.Reset()
|
workspaceLatestBuildTotals.Reset()
|
||||||
|
} else {
|
||||||
|
logger.Warn(ctx, "failed to load latest workspace builds", slog.Error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Warn(ctx, "failed to load latest workspace builds", slog.Error(err))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
jobIDs := make([]uuid.UUID, 0, len(builds))
|
jobIDs := make([]uuid.UUID, 0, len(builds))
|
||||||
|
Reference in New Issue
Block a user