@@ -329,11 +329,17 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
329
329
}, nil
330
330
}
331
331
332
- func AgentStats (ctx context.Context , logger slog.Logger , registerer prometheus.Registerer , db database.Store , initialCreateAfter time.Time , duration time.Duration ) (func (), error ) {
332
+ var DefaultAgentStatsLabels = []string {agentmetrics .AgentNameLabel , agentmetrics .UsernameLabel , agentmetrics .WorkspaceNameLabel }
333
+
334
+ func AgentStats (ctx context.Context , logger slog.Logger , registerer prometheus.Registerer , db database.Store , initialCreateAfter time.Time , duration time.Duration , aggregateByLabels []string ) (func (), error ) {
333
335
if duration == 0 {
334
336
duration = 1 * time .Minute
335
337
}
336
338
339
+ if len (aggregateByLabels ) == 0 {
340
+ aggregateByLabels = DefaultAgentStatsLabels
341
+ }
342
+
337
343
metricsCollectorAgentStats := prometheus .NewHistogram (prometheus.HistogramOpts {
338
344
Namespace : "coderd" ,
339
345
Subsystem : "prometheusmetrics" ,
@@ -351,7 +357,7 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
351
357
Subsystem : "agentstats" ,
352
358
Name : "tx_bytes" ,
353
359
Help : "Agent Tx bytes" ,
354
- }, [] string { agentmetrics . AgentNameLabel , agentmetrics . UsernameLabel , agentmetrics . WorkspaceNameLabel } ))
360
+ }, aggregateByLabels ))
355
361
err = registerer .Register (agentStatsTxBytesGauge )
356
362
if err != nil {
357
363
return nil , err
@@ -362,7 +368,7 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
362
368
Subsystem : "agentstats" ,
363
369
Name : "rx_bytes" ,
364
370
Help : "Agent Rx bytes" ,
365
- }, [] string { agentmetrics . AgentNameLabel , agentmetrics . UsernameLabel , agentmetrics . WorkspaceNameLabel } ))
371
+ }, aggregateByLabels ))
366
372
err = registerer .Register (agentStatsRxBytesGauge )
367
373
if err != nil {
368
374
return nil , err
@@ -373,7 +379,7 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
373
379
Subsystem : "agentstats" ,
374
380
Name : "connection_count" ,
375
381
Help : "The number of established connections by agent" ,
376
- }, [] string { agentmetrics . AgentNameLabel , agentmetrics . UsernameLabel , agentmetrics . WorkspaceNameLabel } ))
382
+ }, aggregateByLabels ))
377
383
err = registerer .Register (agentStatsConnectionCountGauge )
378
384
if err != nil {
379
385
return nil , err
@@ -384,7 +390,7 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
384
390
Subsystem : "agentstats" ,
385
391
Name : "connection_median_latency_seconds" ,
386
392
Help : "The median agent connection latency in seconds" ,
387
- }, [] string { agentmetrics . AgentNameLabel , agentmetrics . UsernameLabel , agentmetrics . WorkspaceNameLabel } ))
393
+ }, aggregateByLabels ))
388
394
err = registerer .Register (agentStatsConnectionMedianLatencyGauge )
389
395
if err != nil {
390
396
return nil , err
@@ -395,7 +401,7 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
395
401
Subsystem : "agentstats" ,
396
402
Name : "session_count_jetbrains" ,
397
403
Help : "The number of session established by JetBrains" ,
398
- }, [] string { agentmetrics . AgentNameLabel , agentmetrics . UsernameLabel , agentmetrics . WorkspaceNameLabel } ))
404
+ }, aggregateByLabels ))
399
405
err = registerer .Register (agentStatsSessionCountJetBrainsGauge )
400
406
if err != nil {
401
407
return nil , err
@@ -406,7 +412,7 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
406
412
Subsystem : "agentstats" ,
407
413
Name : "session_count_reconnecting_pty" ,
408
414
Help : "The number of session established by reconnecting PTY" ,
409
- }, [] string { agentmetrics . AgentNameLabel , agentmetrics . UsernameLabel , agentmetrics . WorkspaceNameLabel } ))
415
+ }, aggregateByLabels ))
410
416
err = registerer .Register (agentStatsSessionCountReconnectingPTYGauge )
411
417
if err != nil {
412
418
return nil , err
@@ -417,7 +423,7 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
417
423
Subsystem : "agentstats" ,
418
424
Name : "session_count_ssh" ,
419
425
Help : "The number of session established by SSH" ,
420
- }, [] string { agentmetrics . AgentNameLabel , agentmetrics . UsernameLabel , agentmetrics . WorkspaceNameLabel } ))
426
+ }, aggregateByLabels ))
421
427
err = registerer .Register (agentStatsSessionCountSSHGauge )
422
428
if err != nil {
423
429
return nil , err
@@ -428,7 +434,7 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
428
434
Subsystem : "agentstats" ,
429
435
Name : "session_count_vscode" ,
430
436
Help : "The number of session established by VSCode" ,
431
- }, [] string { agentmetrics . AgentNameLabel , agentmetrics . UsernameLabel , agentmetrics . WorkspaceNameLabel } ))
437
+ }, aggregateByLabels ))
432
438
err = registerer .Register (agentStatsSessionCountVSCodeGauge )
433
439
if err != nil {
434
440
return nil , err
@@ -460,16 +466,28 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
460
466
logger .Error (ctx , "can't get agent stats" , slog .Error (err ))
461
467
} else {
462
468
for _ , agentStat := range stats {
463
- agentStatsRxBytesGauge .WithLabelValues (VectorOperationAdd , float64 (agentStat .RxBytes ), agentStat .AgentName , agentStat .Username , agentStat .WorkspaceName )
464
- agentStatsTxBytesGauge .WithLabelValues (VectorOperationAdd , float64 (agentStat .TxBytes ), agentStat .AgentName , agentStat .Username , agentStat .WorkspaceName )
469
+ var labelValues []string
470
+ for _ , label := range aggregateByLabels {
471
+ switch label {
472
+ case agentmetrics .UsernameLabel :
473
+ labelValues = append (labelValues , agentStat .Username )
474
+ case agentmetrics .WorkspaceNameLabel :
475
+ labelValues = append (labelValues , agentStat .WorkspaceName )
476
+ case agentmetrics .AgentNameLabel :
477
+ labelValues = append (labelValues , agentStat .AgentName )
478
+ }
479
+ }
480
+
481
+ agentStatsRxBytesGauge .WithLabelValues (VectorOperationAdd , float64 (agentStat .RxBytes ), labelValues ... )
482
+ agentStatsTxBytesGauge .WithLabelValues (VectorOperationAdd , float64 (agentStat .TxBytes ), labelValues ... )
465
483
466
- agentStatsConnectionCountGauge .WithLabelValues (VectorOperationSet , float64 (agentStat .ConnectionCount ), agentStat . AgentName , agentStat . Username , agentStat . WorkspaceName )
467
- agentStatsConnectionMedianLatencyGauge .WithLabelValues (VectorOperationSet , agentStat .ConnectionMedianLatencyMS / 1000.0 /* (to seconds) */ , agentStat . AgentName , agentStat . Username , agentStat . WorkspaceName )
484
+ agentStatsConnectionCountGauge .WithLabelValues (VectorOperationSet , float64 (agentStat .ConnectionCount ), labelValues ... )
485
+ agentStatsConnectionMedianLatencyGauge .WithLabelValues (VectorOperationSet , agentStat .ConnectionMedianLatencyMS / 1000.0 /* (to seconds) */ , labelValues ... )
468
486
469
- agentStatsSessionCountJetBrainsGauge .WithLabelValues (VectorOperationSet , float64 (agentStat .SessionCountJetBrains ), agentStat . AgentName , agentStat . Username , agentStat . WorkspaceName )
470
- agentStatsSessionCountReconnectingPTYGauge .WithLabelValues (VectorOperationSet , float64 (agentStat .SessionCountReconnectingPTY ), agentStat . AgentName , agentStat . Username , agentStat . WorkspaceName )
471
- agentStatsSessionCountSSHGauge .WithLabelValues (VectorOperationSet , float64 (agentStat .SessionCountSSH ), agentStat . AgentName , agentStat . Username , agentStat . WorkspaceName )
472
- agentStatsSessionCountVSCodeGauge .WithLabelValues (VectorOperationSet , float64 (agentStat .SessionCountVSCode ), agentStat . AgentName , agentStat . Username , agentStat . WorkspaceName )
487
+ agentStatsSessionCountJetBrainsGauge .WithLabelValues (VectorOperationSet , float64 (agentStat .SessionCountJetBrains ), labelValues ... )
488
+ agentStatsSessionCountReconnectingPTYGauge .WithLabelValues (VectorOperationSet , float64 (agentStat .SessionCountReconnectingPTY ), labelValues ... )
489
+ agentStatsSessionCountSSHGauge .WithLabelValues (VectorOperationSet , float64 (agentStat .SessionCountSSH ), labelValues ... )
490
+ agentStatsSessionCountVSCodeGauge .WithLabelValues (VectorOperationSet , float64 (agentStat .SessionCountVSCode ), labelValues ... )
473
491
}
474
492
475
493
if len (stats ) > 0 {
0 commit comments