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

Skip to content

Commit 7bd4c6a

Browse files
committed
feat: add prebuild timing metrics to Prometheus
1 parent cde5b62 commit 7bd4c6a

File tree

14 files changed

+355
-43
lines changed

14 files changed

+355
-43
lines changed

cli/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,12 @@ func enablePrometheus(
236236
}
237237
afterCtx(ctx, closeUsersFunc)
238238

239-
closeWorkspacesFunc, err := prometheusmetrics.Workspaces(ctx, options.Logger.Named("workspaces_metrics"), options.PrometheusRegistry, options.Database, 0)
239+
closeWorkspacesFunc, updateWorkspaceTimingsMetrics, err := prometheusmetrics.Workspaces(ctx, options.Logger.Named("workspaces_metrics"), options.PrometheusRegistry, options.Database, 0)
240240
if err != nil {
241241
return nil, xerrors.Errorf("register workspaces prometheus metric: %w", err)
242242
}
243243
afterCtx(ctx, closeWorkspacesFunc)
244+
options.UpdateWorkspaceTimingMetricsFn = updateWorkspaceTimingsMetrics
244245

245246
insightsMetricsCollector, err := insights.NewMetricsCollector(options.Database, options.Logger, 0, 0)
246247
if err != nil {

coderd/coderd.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ type Options struct {
241241
UpdateAgentMetrics func(ctx context.Context, labels prometheusmetrics.AgentMetricLabels, metrics []*agentproto.Stats_Metric)
242242
StatsBatcher workspacestats.Batcher
243243

244+
// Function to update workspace timing metrics from provisionerdserver
245+
UpdateWorkspaceTimingMetricsFn prometheusmetrics.UpdateWorkspaceTimingMetricsFn
246+
244247
// WorkspaceAppAuditSessionTimeout allows changing the timeout for audit
245248
// sessions. Raising or lowering this value will directly affect the write
246249
// load of the audit log table. This is used for testing. Default 1 hour.
@@ -1923,9 +1926,10 @@ func (api *API) CreateInMemoryTaggedProvisionerDaemon(dialCtx context.Context, n
19231926
api.UsageInserter,
19241927
api.DeploymentValues,
19251928
provisionerdserver.Options{
1926-
OIDCConfig: api.OIDCConfig,
1927-
ExternalAuthConfigs: api.ExternalAuthConfigs,
1928-
Clock: api.Clock,
1929+
OIDCConfig: api.OIDCConfig,
1930+
ExternalAuthConfigs: api.ExternalAuthConfigs,
1931+
Clock: api.Clock,
1932+
UpdateWorkspaceTimingMetricsFn: api.UpdateWorkspaceTimingMetricsFn,
19291933
},
19301934
api.NotificationsEnqueuer,
19311935
&api.PrebuildsReconciler,

coderd/database/dbauthz/dbauthz.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,6 +2689,13 @@ func (q *querier) GetQuotaConsumedForUser(ctx context.Context, params database.G
26892689
return q.db.GetQuotaConsumedForUser(ctx, params)
26902690
}
26912691

2692+
func (q *querier) GetRegularWorkspaceCreateMetrics(ctx context.Context) ([]database.GetRegularWorkspaceCreateMetricsRow, error) {
2693+
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceWorkspace.All()); err != nil {
2694+
return nil, err
2695+
}
2696+
return q.db.GetRegularWorkspaceCreateMetrics(ctx)
2697+
}
2698+
26922699
func (q *querier) GetReplicaByID(ctx context.Context, id uuid.UUID) (database.Replica, error) {
26932700
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceSystem); err != nil {
26942701
return database.Replica{}, err

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,6 +3226,10 @@ func (s *MethodTestSuite) TestWorkspace() {
32263226
d := dbgen.WorkspaceAgentDevcontainer(s.T(), db, database.WorkspaceAgentDevcontainer{WorkspaceAgentID: agt.ID})
32273227
check.Args(agt.ID).Asserts(w, policy.ActionRead).Returns([]database.WorkspaceAgentDevcontainer{d})
32283228
}))
3229+
s.Run("GetRegularWorkspaceCreateMetrics", s.Subtest(func(_ database.Store, check *expects) {
3230+
check.Args().
3231+
Asserts(rbac.ResourceWorkspace.All(), policy.ActionRead)
3232+
}))
32293233
}
32303234

32313235
func (s *MethodTestSuite) TestWorkspacePortSharing() {

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: 56 additions & 1 deletion
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ HAVING COUNT(*) = @hard_limit::bigint;
230230
SELECT
231231
t.name as template_name,
232232
tvp.name as preset_name,
233-
o.name as organization_name,
233+
o.name as organization_name,
234234
COUNT(*) as created_count,
235235
COUNT(*) FILTER (WHERE pj.job_status = 'failed'::provisioner_job_status) as failed_count,
236236
COUNT(*) FILTER (

coderd/database/queries/workspaces.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,3 +914,22 @@ SET
914914
user_acl = @user_acl
915915
WHERE
916916
id = @id;
917+
918+
-- name: GetRegularWorkspaceCreateMetrics :many
919+
SELECT
920+
t.name AS template_name,
921+
COALESCE(tvp.name, '') AS preset_name,
922+
o.name AS organization_name,
923+
COUNT(*) AS created_count
924+
FROM workspaces w
925+
INNER JOIN workspace_builds wb ON wb.workspace_id = w.id
926+
AND wb.build_number = 1
927+
AND wb.transition = 'start'::workspace_transition
928+
AND wb.initiator_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid -- The system user responsible for prebuilds.
929+
INNER JOIN templates t ON t.id = w.template_id
930+
LEFT JOIN template_version_presets tvp ON tvp.id = wb.template_version_preset_id
931+
INNER JOIN provisioner_jobs pj ON pj.id = wb.job_id
932+
INNER JOIN organizations o ON o.id = w.organization_id
933+
WHERE NOT t.deleted AND pj.job_status = 'succeeded'::provisioner_job_status
934+
GROUP BY t.name, COALESCE(tvp.name, ''), o.name
935+
ORDER BY t.name, preset_name, o.name;

0 commit comments

Comments
 (0)