Skip to content

Commit 144d3f3

Browse files
authored
chore: record lifecycle duration metric to prometheus (coder#15279)
`autobuild_execution_duration_seconds` keeps track of how long autobuild takes and exposes it via prometheus histogram
1 parent afacb07 commit 144d3f3

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

cli/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
10351035
autobuildTicker := time.NewTicker(vals.AutobuildPollInterval.Value())
10361036
defer autobuildTicker.Stop()
10371037
autobuildExecutor := autobuild.NewExecutor(
1038-
ctx, options.Database, options.Pubsub, coderAPI.TemplateScheduleStore, &coderAPI.Auditor, coderAPI.AccessControlStore, logger, autobuildTicker.C, options.NotificationsEnqueuer)
1038+
ctx, options.Database, options.Pubsub, options.PrometheusRegistry, coderAPI.TemplateScheduleStore, &coderAPI.Auditor, coderAPI.AccessControlStore, logger, autobuildTicker.C, options.NotificationsEnqueuer)
10391039
autobuildExecutor.Run()
10401040

10411041
hangDetectorTicker := time.NewTicker(vals.JobHangDetectorInterval.Value())

coderd/autobuild/lifecycle_executor.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
"github.com/dustin/go-humanize"
1212
"github.com/google/uuid"
13+
"github.com/prometheus/client_golang/prometheus"
14+
"github.com/prometheus/client_golang/prometheus/promauto"
1315
"golang.org/x/sync/errgroup"
1416
"golang.org/x/xerrors"
1517

@@ -39,6 +41,13 @@ type Executor struct {
3941
statsCh chan<- Stats
4042
// NotificationsEnqueuer handles enqueueing notifications for delivery by SMTP, webhook, etc.
4143
notificationsEnqueuer notifications.Enqueuer
44+
reg prometheus.Registerer
45+
46+
metrics executorMetrics
47+
}
48+
49+
type executorMetrics struct {
50+
autobuildExecutionDuration prometheus.Histogram
4251
}
4352

4453
// Stats contains information about one run of Executor.
@@ -49,7 +58,8 @@ type Stats struct {
4958
}
5059

5160
// New returns a new wsactions executor.
52-
func NewExecutor(ctx context.Context, db database.Store, ps pubsub.Pubsub, tss *atomic.Pointer[schedule.TemplateScheduleStore], auditor *atomic.Pointer[audit.Auditor], acs *atomic.Pointer[dbauthz.AccessControlStore], log slog.Logger, tick <-chan time.Time, enqueuer notifications.Enqueuer) *Executor {
61+
func NewExecutor(ctx context.Context, db database.Store, ps pubsub.Pubsub, reg prometheus.Registerer, tss *atomic.Pointer[schedule.TemplateScheduleStore], auditor *atomic.Pointer[audit.Auditor], acs *atomic.Pointer[dbauthz.AccessControlStore], log slog.Logger, tick <-chan time.Time, enqueuer notifications.Enqueuer) *Executor {
62+
factory := promauto.With(reg)
5363
le := &Executor{
5464
//nolint:gocritic // Autostart has a limited set of permissions.
5565
ctx: dbauthz.AsAutostart(ctx),
@@ -61,6 +71,16 @@ func NewExecutor(ctx context.Context, db database.Store, ps pubsub.Pubsub, tss *
6171
auditor: auditor,
6272
accessControlStore: acs,
6373
notificationsEnqueuer: enqueuer,
74+
reg: reg,
75+
metrics: executorMetrics{
76+
autobuildExecutionDuration: factory.NewHistogram(prometheus.HistogramOpts{
77+
Namespace: "coderd",
78+
Subsystem: "lifecycle",
79+
Name: "autobuild_execution_duration_seconds",
80+
Help: "Duration of each autobuild execution.",
81+
Buckets: prometheus.DefBuckets,
82+
}),
83+
},
6484
}
6585
return le
6686
}
@@ -86,6 +106,7 @@ func (e *Executor) Run() {
86106
return
87107
}
88108
stats := e.runOnce(t)
109+
e.metrics.autobuildExecutionDuration.Observe(stats.Elapsed.Seconds())
89110
if e.statsCh != nil {
90111
select {
91112
case <-e.ctx.Done():

coderd/coderdtest/coderdtest.go

+1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
335335
ctx,
336336
options.Database,
337337
options.Pubsub,
338+
prometheus.NewRegistry(),
338339
&templateScheduleStore,
339340
&auditor,
340341
accessControlStore,

0 commit comments

Comments
 (0)