@@ -1208,17 +1208,15 @@ func TestServer(t *testing.T) {
1208
1208
}
1209
1209
}
1210
1210
return htmlFirstServedFound
1211
- }, testutil .WaitMedium , testutil .IntervalFast , "no html_first_served telemetry item" )
1211
+ }, testutil .WaitLong , testutil .IntervalSlow , "no html_first_served telemetry item" )
1212
1212
})
1213
1213
t .Run ("Prometheus" , func (t * testing.T ) {
1214
1214
t .Parallel ()
1215
1215
1216
1216
t .Run ("DBMetricsDisabled" , func (t * testing.T ) {
1217
1217
t .Parallel ()
1218
1218
1219
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
1220
- defer cancel ()
1221
-
1219
+ ctx := testutil .Context (t , testutil .WaitLong )
1222
1220
randPort := testutil .RandomPort (t )
1223
1221
inv , cfg := clitest .New (t ,
1224
1222
"server" ,
@@ -1235,46 +1233,45 @@ func TestServer(t *testing.T) {
1235
1233
clitest .Start (t , inv )
1236
1234
_ = waitAccessURL (t , cfg )
1237
1235
1238
- var res * http.Response
1239
- require .Eventually (t , func () bool {
1240
- req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("http://127.0.0.1:%d" , randPort ), nil )
1241
- assert .NoError (t , err )
1236
+ testutil .Eventually (ctx , t , func (ctx context.Context ) bool {
1237
+ req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("http://127.0.0.1:%d/metrics" , randPort ), nil )
1238
+ if err != nil {
1239
+ t .Logf ("error creating request: %s" , err .Error ())
1240
+ return false
1241
+ }
1242
1242
// nolint:bodyclose
1243
- res , err = http .DefaultClient .Do (req )
1243
+ res , err : = http .DefaultClient .Do (req )
1244
1244
if err != nil {
1245
+ t .Logf ("error hitting prometheus endpoint: %s" , err .Error ())
1245
1246
return false
1246
1247
}
1247
1248
defer res .Body .Close ()
1248
-
1249
1249
scanner := bufio .NewScanner (res .Body )
1250
- hasActiveUsers := false
1250
+ var activeUsersFound bool
1251
+ var scannedOnce bool
1251
1252
for scanner .Scan () {
1253
+ line := scanner .Text ()
1254
+ if ! scannedOnce {
1255
+ t .Logf ("scanned: %s" , line ) // avoid spamming logs
1256
+ scannedOnce = true
1257
+ }
1258
+ if strings .HasPrefix (line , "coderd_db_query_latencies_seconds" ) {
1259
+ t .Errorf ("db metrics should not be tracked when --prometheus-collect-db-metrics is not enabled" )
1260
+ }
1252
1261
// This metric is manually registered to be tracked in the server. That's
1253
1262
// why we test it's tracked here.
1254
- if strings .HasPrefix (scanner .Text (), "coderd_api_active_users_duration_hour" ) {
1255
- hasActiveUsers = true
1256
- continue
1257
- }
1258
- if strings .HasPrefix (scanner .Text (), "coderd_db_query_latencies_seconds" ) {
1259
- t .Fatal ("db metrics should not be tracked when --prometheus-collect-db-metrics is not enabled" )
1263
+ if strings .HasPrefix (line , "coderd_api_active_users_duration_hour" ) {
1264
+ activeUsersFound = true
1260
1265
}
1261
- t .Logf ("scanned %s" , scanner .Text ())
1262
- }
1263
- if scanner .Err () != nil {
1264
- t .Logf ("scanner err: %s" , scanner .Err ().Error ())
1265
- return false
1266
1266
}
1267
-
1268
- return hasActiveUsers
1269
- }, testutil .WaitShort , testutil .IntervalFast , "didn't find coderd_api_active_users_duration_hour in time" )
1267
+ return activeUsersFound
1268
+ }, testutil .IntervalSlow , "didn't find coderd_api_active_users_duration_hour in time" )
1270
1269
})
1271
1270
1272
1271
t .Run ("DBMetricsEnabled" , func (t * testing.T ) {
1273
1272
t .Parallel ()
1274
1273
1275
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
1276
- defer cancel ()
1277
-
1274
+ ctx := testutil .Context (t , testutil .WaitLong )
1278
1275
randPort := testutil .RandomPort (t )
1279
1276
inv , cfg := clitest .New (t ,
1280
1277
"server" ,
@@ -1291,31 +1288,34 @@ func TestServer(t *testing.T) {
1291
1288
clitest .Start (t , inv )
1292
1289
_ = waitAccessURL (t , cfg )
1293
1290
1294
- var res * http.Response
1295
- require .Eventually (t , func () bool {
1296
- req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("http://127.0.0.1:%d" , randPort ), nil )
1297
- assert .NoError (t , err )
1291
+ testutil .Eventually (ctx , t , func (ctx context.Context ) bool {
1292
+ req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("http://127.0.0.1:%d/metrics" , randPort ), nil )
1293
+ if err != nil {
1294
+ t .Logf ("error creating request: %s" , err .Error ())
1295
+ return false
1296
+ }
1298
1297
// nolint:bodyclose
1299
- res , err = http .DefaultClient .Do (req )
1298
+ res , err : = http .DefaultClient .Do (req )
1300
1299
if err != nil {
1300
+ t .Logf ("error hitting prometheus endpoint: %s" , err .Error ())
1301
1301
return false
1302
1302
}
1303
1303
defer res .Body .Close ()
1304
-
1305
1304
scanner := bufio .NewScanner (res .Body )
1306
- hasDBMetrics := false
1305
+ var dbMetricsFound bool
1306
+ var scannedOnce bool
1307
1307
for scanner .Scan () {
1308
- if strings .HasPrefix (scanner .Text (), "coderd_db_query_latencies_seconds" ) {
1309
- hasDBMetrics = true
1308
+ line := scanner .Text ()
1309
+ if ! scannedOnce {
1310
+ t .Logf ("scanned: %s" , line ) // avoid spamming logs
1311
+ scannedOnce = true
1312
+ }
1313
+ if strings .HasPrefix (line , "coderd_db_query_latencies_seconds" ) {
1314
+ dbMetricsFound = true
1310
1315
}
1311
- t .Logf ("scanned %s" , scanner .Text ())
1312
- }
1313
- if scanner .Err () != nil {
1314
- t .Logf ("scanner err: %s" , scanner .Err ().Error ())
1315
- return false
1316
1316
}
1317
- return hasDBMetrics
1318
- }, testutil .WaitShort , testutil . IntervalFast , "didn't find coderd_db_query_latencies_seconds in time" )
1317
+ return dbMetricsFound
1318
+ }, testutil .IntervalSlow , "didn't find coderd_db_query_latencies_seconds in time" )
1319
1319
})
1320
1320
})
1321
1321
t .Run ("GitHubOAuth" , func (t * testing.T ) {
0 commit comments