@@ -200,7 +200,7 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
200
200
})
201
201
if err != nil {
202
202
logger .Error (ctx , "can't get workspace rows" , slog .Error (err ))
203
- continue
203
+ goto done
204
204
}
205
205
206
206
for _ , workspace := range workspaceRows {
@@ -283,9 +283,59 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
283
283
agentsConnectionLatenciesGauge .Commit ()
284
284
agentsAppsGauge .Commit ()
285
285
286
+ done:
286
287
logger .Debug (ctx , "Agent metrics collection is done" )
287
288
metricsCollectorAgents .Observe (timer .ObserveDuration ().Seconds ())
288
289
}
289
290
}()
290
291
return cancelFunc , nil
291
292
}
293
+
294
+ func AgentStats (ctx context.Context , logger slog.Logger , registerer prometheus.Registerer , db database.Store , duration time.Duration ) (context.CancelFunc , error ) {
295
+ if duration == 0 {
296
+ duration = 1 * time .Minute
297
+ }
298
+
299
+ metricsCollectorAgentStats := prometheus .NewHistogram (prometheus.HistogramOpts {
300
+ Namespace : "coderd" ,
301
+ Subsystem : "prometheusmetrics" ,
302
+ Name : "agentstats_execution_seconds" ,
303
+ Help : "Histogram for duration of agent stats metrics collection in seconds." ,
304
+ Buckets : []float64 {0.001 , 0.005 , 0.010 , 0.025 , 0.050 , 0.100 , 0.500 , 1 , 5 , 10 , 30 },
305
+ })
306
+ err := registerer .Register (metricsCollectorAgentStats )
307
+ if err != nil {
308
+ return nil , err
309
+ }
310
+
311
+ createdAfter := database .Now ().Add (- duration )
312
+ ctx , cancelFunc := context .WithCancel (ctx )
313
+ ticker := time .NewTicker (duration )
314
+ go func () {
315
+ defer ticker .Stop ()
316
+ for {
317
+ select {
318
+ case <- ctx .Done ():
319
+ return
320
+ case <- ticker .C :
321
+ }
322
+
323
+ logger .Debug (ctx , "Agent metrics collection is starting" )
324
+ timer := prometheus .NewTimer (metricsCollectorAgentStats )
325
+
326
+ _ , err := db .GetWorkspaceAgentStats (ctx , createdAfter )
327
+ if err != nil {
328
+ logger .Error (ctx , "can't get agent stats" , slog .Error (err ))
329
+ goto done
330
+ }
331
+
332
+ db .GetWorkspAgents
333
+
334
+ done:
335
+ logger .Debug (ctx , "Agent metrics collection is done" )
336
+ metricsCollectorAgentStats .Observe (timer .ObserveDuration ().Seconds ())
337
+ }
338
+ }()
339
+ return cancelFunc , nil
340
+
341
+ }
0 commit comments