Skip to content

Commit 5952fa6

Browse files
committed
test(coderd/prometheusmetrics/insights): improve TestCollectInsights
1 parent f92c18e commit 5952fa6

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

coderd/prometheusmetrics/insights/metricscollector_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/google/go-cmp/cmp"
1213
"github.com/google/uuid"
1314
"github.com/prometheus/client_golang/prometheus"
1415
io_prometheus_client "github.com/prometheus/client_model/go"
@@ -110,7 +111,7 @@ func TestCollectInsights(t *testing.T) {
110111
UserID: user.UserID,
111112
WorkspaceID: workspace.ID,
112113
AgentID: resources[0].Agents[0].ID,
113-
AccessMethod: "terminal",
114+
AccessMethod: "path",
114115
SlugOrPort: "golden-slug",
115116
SessionID: uuid.New(),
116117
SessionStartedAt: time.Now().Add(-3 * time.Minute),
@@ -163,13 +164,16 @@ func TestCollectInsights(t *testing.T) {
163164
require.NoError(t, err)
164165

165166
collected := map[string]int{}
166-
assert.Eventuallyf(t, func() bool {
167+
ok := assert.Eventuallyf(t, func() bool {
167168
// When
168169
metrics, err := registry.Gather()
169-
require.NoError(t, err)
170+
if !assert.NoError(t, err) {
171+
return false
172+
}
170173

171174
// Then
172175
for _, metric := range metrics {
176+
t.Logf("metric: %s: %#v", metric.GetName(), metric)
173177
switch metric.GetName() {
174178
case "coderd_insights_applications_usage_seconds", "coderd_insights_templates_active_users", "coderd_insights_parameters":
175179
for _, m := range metric.Metric {
@@ -180,12 +184,16 @@ func TestCollectInsights(t *testing.T) {
180184
collected[key] = int(m.Gauge.GetValue())
181185
}
182186
default:
183-
require.FailNowf(t, "unexpected metric collected", "metric: %s", metric.GetName())
187+
assert.Failf(t, "unexpected metric collected", "metric: %s", metric.GetName())
184188
}
185189
}
186190

187191
return insightsMetricsAreEqual(golden, collected)
188-
}, testutil.WaitMedium, testutil.IntervalFast, "template insights are inconsistent with golden files, got: %v", collected)
192+
}, testutil.WaitMedium, testutil.IntervalFast, "template insights are inconsistent with golden files")
193+
if !ok {
194+
diff := cmp.Diff(golden, collected)
195+
assert.Empty(t, diff, "template insights are inconsistent with golden files (-golden +collected)")
196+
}
189197
}
190198

191199
func metricLabelAsString(m *io_prometheus_client.Metric) string {

0 commit comments

Comments
 (0)