Skip to content

Commit 4e1e745

Browse files
SasSwartdannykopping
authored andcommitted
add prebuild metrics and observability
Signed-off-by: Danny Kopping <[email protected]>
1 parent a87e127 commit 4e1e745

File tree

13 files changed

+319
-5
lines changed

13 files changed

+319
-5
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,13 @@ func (q *querier) GetParameterSchemasByJobID(ctx context.Context, jobID uuid.UUI
19771977
return q.db.GetParameterSchemasByJobID(ctx, jobID)
19781978
}
19791979

1980+
func (q *querier) GetPrebuildMetrics(ctx context.Context) ([]database.GetPrebuildMetricsRow, error) {
1981+
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceTemplate); err != nil {
1982+
return nil, err
1983+
}
1984+
return q.db.GetPrebuildMetrics(ctx)
1985+
}
1986+
19801987
func (q *querier) GetPrebuildsInProgress(ctx context.Context) ([]database.GetPrebuildsInProgressRow, error) {
19811988
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceTemplate); err != nil {
19821989
return nil, err

coderd/database/dbmem/dbmem.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3784,6 +3784,10 @@ func (q *FakeQuerier) GetParameterSchemasByJobID(_ context.Context, jobID uuid.U
37843784
return parameters, nil
37853785
}
37863786

3787+
func (q *FakeQuerier) GetPrebuildMetrics(ctx context.Context) ([]database.GetPrebuildMetricsRow, error) {
3788+
panic("not implemented")
3789+
}
3790+
37873791
func (q *FakeQuerier) GetPrebuildsInProgress(ctx context.Context) ([]database.GetPrebuildsInProgressRow, error) {
37883792
panic("not implemented")
37893793
}

coderd/database/dbmetrics/querymetrics.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/prebuilds.sql

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,46 @@ RETURNING w.id, w.name;
7171
INSERT INTO template_version_preset_prebuilds (id, preset_id, desired_instances, invalidate_after_secs)
7272
VALUES (@id::uuid, @preset_id::uuid, @desired_instances::int, @invalidate_after_secs::int)
7373
RETURNING *;
74+
75+
-- name: GetPrebuildMetrics :many
76+
SELECT
77+
t.name as template_name,
78+
tvp.name as preset_name,
79+
COUNT(*) FILTER ( -- created
80+
-- TODO (sasswart): double check which job statuses should be included here
81+
WHERE
82+
pj.initiator_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid
83+
AND pj.job_status = 'succeeded'::provisioner_job_status
84+
) as created,
85+
COUNT(*) FILTER ( -- failed
86+
-- TODO (sasswart): should we count cancelled here?
87+
WHERE pj.initiator_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid
88+
AND pj.job_status = 'failed'::provisioner_job_status
89+
) as failed,
90+
COUNT(*) FILTER ( -- assigned
91+
WHERE pj.initiator_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid
92+
AND NOT w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid
93+
) as assigned,
94+
COUNT(*) FILTER ( -- exhausted
95+
-- TODO (sasswart): write a filter to count this
96+
-- we should be able to count:
97+
-- - workspace builds
98+
-- - that have a preset id
99+
-- - and that preset has prebuilds enabled
100+
-- - and the job for the prebuild was initiated by a user other than the prebuilds user
101+
WHERE
102+
wb.template_version_preset_id IS NOT NULL
103+
AND w.owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid
104+
AND wb.initiator_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid
105+
) as exhausted,
106+
COUNT(*) FILTER ( -- used_preset
107+
WHERE wb.template_version_preset_id IS NOT NULL
108+
) as used_preset
109+
FROM workspace_builds wb
110+
INNER JOIN provisioner_jobs pj ON wb.job_id = pj.id
111+
LEFT JOIN workspaces w ON wb.workspace_id = w.id
112+
LEFT JOIN template_version_presets tvp ON wb.template_version_preset_id = tvp.id
113+
LEFT JOIN template_versions tv ON tv.id = wb.template_version_id
114+
LEFT JOIN templates t ON t.id = tv.template_id
115+
WHERE pj.initiator_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid
116+
GROUP BY t.name, tvp.name;

enterprise/coderd/coderd.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,13 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
590590
} else {
591591
api.prebuildsController = prebuilds.NewController(options.Database, options.Pubsub, options.DeploymentValues.Prebuilds, options.Logger.Named("prebuilds.controller"))
592592
go api.prebuildsController.Loop(ctx)
593+
594+
prebuildMetricsCollector := prebuilds.NewMetricsCollector(options.Database, options.Logger)
595+
// should this be api.prebuild...
596+
err = api.PrometheusRegistry.Register(prebuildMetricsCollector)
597+
if err != nil {
598+
return nil, xerrors.Errorf("unable to register prebuilds metrics collector: %w", err)
599+
}
593600
}
594601
}
595602

enterprise/coderd/prebuilds/claim.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (e EnterpriseClaimer) Claim(ctx context.Context, store database.Store, user
5252
}
5353

5454
func (e EnterpriseClaimer) Initiator() uuid.UUID {
55-
return ownerID
55+
return OwnerID
5656
}
5757

5858
var _ prebuilds.Claimer = &EnterpriseClaimer{}

enterprise/coderd/prebuilds/controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (c *Controller) createPrebuild(ctx context.Context, prebuildID uuid.UUID, t
321321
ID: prebuildID,
322322
CreatedAt: now,
323323
UpdatedAt: now,
324-
OwnerID: ownerID,
324+
OwnerID: OwnerID,
325325
OrganizationID: template.OrganizationID,
326326
TemplateID: template.ID,
327327
Name: name,
@@ -382,14 +382,14 @@ func (c *Controller) provision(ctx context.Context, prebuildID uuid.UUID, templa
382382

383383
builder := wsbuilder.New(workspace, transition).
384384
Reason(database.BuildReasonInitiator).
385-
Initiator(ownerID).
385+
Initiator(OwnerID).
386386
ActiveVersion().
387387
VersionID(template.ActiveVersionID).
388388
MarkPrebuild().
389389
TemplateVersionPresetID(presetID)
390390

391391
// We only inject the required params when the prebuild is being created.
392-
// This mirrors the behaviour of regular workspace deletion (see cli/delete.go).
392+
// This mirrors the behavior of regular workspace deletion (see cli/delete.go).
393393
if transition != database.WorkspaceTransitionDelete {
394394
builder = builder.RichParameterValues(params)
395395
}

0 commit comments

Comments
 (0)