Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a8e4e6

Browse files
committed
Add timer to measure the metrics collection
1 parent 7418779 commit 3a8e4e6

File tree

4 files changed

+90
-54
lines changed

4 files changed

+90
-54
lines changed

coderd/prometheusmetrics/prometheusmetrics.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
168168
return nil, err
169169
}
170170

171+
metricsCollectorAgents := prometheus.NewHistogram(prometheus.HistogramOpts{
172+
Namespace: "coderd",
173+
Subsystem: "prometheusmetrics",
174+
Name: "agents_execution_seconds",
175+
Help: "Histogram for duration of agents metrics collection in seconds.",
176+
Buckets: []float64{0.001, 0.005, 0.010, 0.025, 0.050, 0.100, 0.500, 1, 5, 10, 30},
177+
})
178+
err = registerer.Register(metricsCollectorAgents)
179+
if err != nil {
180+
return nil, err
181+
}
182+
171183
// nolint:gocritic // Prometheus must collect metrics for all Coder users.
172184
ctx, cancelFunc := context.WithCancel(dbauthz.AsSystemRestricted(ctx))
173185
ticker := time.NewTicker(duration)
@@ -180,7 +192,8 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
180192
case <-ticker.C:
181193
}
182194

183-
logger.Debug(ctx, "Collect agent metrics now")
195+
logger.Debug(ctx, "Agent metrics collection is starting")
196+
timer := prometheus.NewTimer(metricsCollectorAgents)
184197

185198
workspaceRows, err := db.GetWorkspaces(ctx, database.GetWorkspacesParams{
186199
AgentInactiveDisconnectTimeoutSeconds: int64(agentInactiveDisconnectTimeout.Seconds()),
@@ -269,6 +282,9 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
269282
}
270283
}
271284
}
285+
286+
logger.Debug(ctx, "Agent metrics collection is done")
287+
metricsCollectorAgents.Observe(timer.ObserveDuration().Seconds())
272288
}
273289
}()
274290
return cancelFunc, nil

coderd/prometheusmetrics/prometheusmetrics_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ func TestAgents(t *testing.T) {
310310
var agentsUp bool
311311
var agentsConnections bool
312312
var agentsApps bool
313+
var agentsExecutionInSeconds bool
313314
require.Eventually(t, func() bool {
314315
metrics, err := registry.Gather()
315316
assert.NoError(t, err)
@@ -342,10 +343,12 @@ func TestAgents(t *testing.T) {
342343
assert.Equal(t, workspace.Name, metric.Metric[0].Label[4].GetValue()) // Workspace name
343344
assert.Equal(t, 1, int(metric.Metric[0].Gauge.GetValue())) // Metric value
344345
agentsApps = true
346+
case "coderd_prometheusmetrics_agents_execution_seconds":
347+
agentsExecutionInSeconds = true
345348
default:
346349
require.FailNowf(t, "unexpected metric collected", "metric: %s", metric.GetName())
347350
}
348351
}
349-
return agentsUp && agentsConnections && agentsApps
352+
return agentsUp && agentsConnections && agentsApps && agentsExecutionInSeconds
350353
}, testutil.WaitShort, testutil.IntervalFast)
351354
}

0 commit comments

Comments
 (0)