8000 feat: add template info tags to `coderd_agents_up` metric (#7942) · coder/coder@dd4aafb · GitHub
[go: up one dir, main page]

Skip to content

Commit dd4aafb

Browse files
goodsparkcoadler
andauthored
feat: add template info tags to coderd_agents_up metric (#7942)
Co-authored-by: Colin Adler <colin1adler@gmail.com>
1 parent 398e8fd commit dd4aafb

File tree

6 files changed

+116
-42
lines changed

6 files changed

+116
-42
lines changed

coderd/database/dbfake/dbfake.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,12 @@ func (q *fakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
607607
}
608608
if arg.Limit > 0 {
609609
if int(arg.Limit) > len(workspaces) {
610-
return convertToWorkspaceRows(workspaces, int64(beforePageCount)), nil
610+
return q.convertToWorkspaceRowsNoLock(ctx, workspaces, int64(beforePageCount)), nil
611611
}
612612
workspaces = workspaces[:arg.Limit]
613613
}
614614

615-
return convertToWorkspaceRows(workspaces, int64(beforePageCount)), nil
615+
return q.convertToWorkspaceRowsNoLock(ctx, workspaces, int64(beforePageCount)), nil
616616
}
617617

618618
// mapAgentStatus determines the agent status based on different timestamps like created_at, last_connected_at, disconnected_at, etc.
@@ -649,10 +649,10 @@ func mapAgentStatus(dbAgent database.WorkspaceAgent, agentInactiveDisconnectTime
649649
return status
650650
}
651651

652-
func convertToWorkspaceRows(workspaces []database.Workspace, count int64) []database.GetWorkspacesRow {
653-
rows := make([]database.GetWorkspacesRow, len(workspaces))
654-
for i, w := range workspaces {
655-
rows[i] = database.GetWorkspacesRow{
652+
func (q *fakeQuerier) convertToWorkspaceRowsNoLock(ctx context.Context, workspaces []database.Workspace, count int64) []database.GetWorkspacesRow {
653+
rows := make([]database.GetWorkspacesRow, 0, len(workspaces))
654+
for _, w := range workspaces {
655+
wr := database.GetWorkspacesRow{
656656
ID: w.ID,
657657
CreatedAt: w.CreatedAt,
658658
UpdatedAt: w.UpdatedAt,
@@ -666,6 +666,28 @@ func convertToWorkspaceRows(workspaces []database.Workspace, count int64) []data
666666
LastUsedAt: w.LastUsedAt,
667667
Count: count,
668668
}
669+
670+
for _, t := range q.templates {
671+
if t.ID == w.TemplateID {
672+
wr.TemplateName = t.Name
673+
break
674+
}
675+
}
676+
677+
if build, err := q.getLatestWorkspaceBuildByWorkspaceIDNoLock(ctx, w.ID); err == nil {
678+
for _, tv := range q.templateVersions {
679+
if tv.ID == build.TemplateVersionID {
680+
wr.TemplateVersionID = tv.ID
681+
wr.TemplateVersionName = sql.NullString{
682+
Valid: true,
683+
String: tv.Name,
684+
}
685+
break
686+
}
687+
}
688+
}
689+
690+
rows = append(rows, wr)
669691
}
670692
return rows
671693
}

coderd/database/modelqueries.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
236236
&i.Ttl,
237237
&i.LastUsedAt,
238238
&i.LockedAt,
239+
&i.TemplateName,
240+
&i.TemplateVersionID,
241+
&i.TemplateVersionName,
239242
&i.Count,
240243
); err != nil {
241244
return nil, err

coderd/database/queries.sql.go

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

coderd/database/queries/workspaces.sql

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ WHERE
7575

7676
-- name: GetWorkspaces :many
7777
SELECT
78-
workspaces.*, COUNT(*) OVER () as count
78+
workspaces.*,
79+
COALESCE(template_name.template_name, 'unknown') as template_name,
80+
latest_build.template_version_id,
81+
latest_build.template_version_name,
82+
COUNT(*) OVER () as count
7983
FROM
8084
workspaces
8185
JOIN
@@ -85,6 +89,8 @@ ON
8589
LEFT JOIN LATERAL (
8690
SELECT
8791
workspace_builds.transition,
92+
workspace_builds.template_version_id,
93+
template_versions.name AS template_version_name,
8894
provisioner_jobs.id AS provisioner_job_id,
8995
provisioner_jobs.started_at,
9096
provisioner_jobs.updated_at,
@@ -97,13 +103,25 @@ LEFT JOIN LATERAL (
97103
provisioner_jobs
98104
ON
99105
provisioner_jobs.id = workspace_builds.job_id
106+
LEFT JOIN
107+
template_versions
108+
ON
109+
template_versions.id = workspace_builds.template_version_id
100110
WHERE
101111
workspace_builds.workspace_id = workspaces.id
102112
ORDER BY
103113
build_number DESC
104114
LIMIT
105115
1
106116
) latest_build ON TRUE
117+
LEFT JOIN LATERAL (
118+
SELECT
119+
templates.name AS template_name
120+
FROM
121+
templates
122+
WHERE
123+
templates.id = workspaces.template_id
124+
) template_name ON true
107125
WHERE
108126
-- Optionally include deleted workspaces
109127
workspaces.deleted = @deleted
@@ -175,33 +193,33 @@ WHERE
175193
-- Filter by owner_id
176194
AND CASE
177195
WHEN @owner_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
178-
owner_id = @owner_id
196+
workspaces.owner_id = @owner_id
179197
ELSE true
180198
END
181199
-- Filter by owner_name
182200
AND CASE
183201
WHEN @owner_username :: text != '' THEN
184-
owner_id = (SELECT id FROM users WHERE lower(username) = lower(@owner_username) AND deleted = false)
202+
workspaces.owner_id = (SELECT id FROM users WHERE lower(username) = lower(@owner_username) AND deleted = false)
185203
ELSE true
186204
END
187205
-- Filter by template_name
188206
-- There can be more than 1 template with the same name across organizations.
189207
-- Use the organization filter to restrict to 1 org if needed.
190208
AND CASE
191209
WHEN @template_name :: text != '' THEN
192-
template_id = ANY(SELECT id FROM templates WHERE lower(name) = lower(@template_name) AND deleted = false)
210+
workspaces.template_id = ANY(SELECT id FROM templates WHERE lower(name) = lower(@template_name) AND deleted = false)
193211
ELSE true
194212
END
195213
-- Filter by template_ids
196214
AND CASE
197215
WHEN array_length(@template_ids :: uuid[], 1) > 0 THEN
198-
template_id = ANY(@template_ids)
216+
workspaces.template_id = ANY(@template_ids)
199217
ELSE true
200218
END
201219
-- Filter by name, matching on substring
202220
AND CASE
203221
WHEN @name :: text != '' THEN
204-
name ILIKE '%' || @name || '%'
222+
workspaces.name ILIKE '%' || @name || '%'
205223
ELSE true
206224
END
207225
-- Filter by agent status
@@ -249,7 +267,7 @@ ORDER BY
249267
latest_build.error IS NULL AND
250268
latest_build.transition = 'start'::workspace_transition) DESC,
251269
LOWER(users.username) ASC,
252-
LOWER(name) ASC
270+
LOWER(workspaces.name) ASC
253271
LIMIT
254272
CASE
255273
WHEN @limit_ :: integer > 0 THEN

coderd/prometheusmetrics/prometheusmetrics.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"tailscale.com/tailcfg"
1616

1717
"cdr.dev/slog"
18-
1918
"github.com/coder/coder/coderd/database"
2019
"github.com/coder/coder/coderd/database/db2sdk"
2120
"github.com/coder/coder/coderd/database/dbauthz"
@@ -153,7 +152,7 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
153152
Subsystem: "agents",
154153
Name: "up",
155154
Help: "The number of active agents per workspace.",
156-
}, []string{usernameLabel, workspaceNameLabel}))
155+
}, []string{usernameLabel, workspaceNameLabel, "template_name", "template_version"}))
157156
err := registerer.Register(agentsGauge)
158157
if err != nil {
159158
return nil, err
@@ -234,29 +233,35 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
234233
}
235234

236235
for _, workspace := range workspaceRows {
236+
templateName := workspace.TemplateName
237+
templateVersionName := workspace.TemplateVersionName.String
238+
if !workspace.TemplateVersionName.Valid {
239+
templateVersionName = "unknown"
240+
}
241+
237242
user, err := db.GetUserByID(ctx, workspace.OwnerID)
238243
if err != nil {
239244
logger.Error(ctx, "can't get user from the database", slog.F("user_id", workspace.OwnerID), slog.Error(err))
240-
agentsGauge.WithLabelValues(VectorOperationAdd, 0, user.Username, workspace.Name)
245+
agentsGauge.WithLabelValues(VectorOperationAdd, 0, user.Username, workspace.Name, templateName, templateVersionName)
241246
continue
242247
}
243248

244249
agents, err := db.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, workspace.ID)
245250
if err != nil {
246251
logger.Error(ctx, "can't get workspace agents", slog.F("workspace_id", workspace.ID), slog.Error(err))
247-
agentsGauge.WithLabelValues(VectorOperationAdd, 0, user.Username, workspace.Name)
252+
agentsGauge.WithLabelValues(VectorOperationAdd, 0, user.Username, workspace.Name, templateName, templateVersionName)
248253
continue
249254
}
250255

251256
if len(agents) == 0 {
252257
logger.Debug(ctx, "workspace agents are unavailable", slog.F("workspace_id", workspace.ID))
253-
agentsGauge.WithLabelValues(VectorOperationAdd, 0, user.Username, workspace.Name)
258+
agentsGauge.WithLabelValues(VectorOperationAdd, 0, user.Username, workspace.Name, templateName, templateVersionName)
254259
continue
255260
}
256261

257262
for _, agent := range agents {
258263
// Collect information about agents
259-
agentsGauge.WithLabelValues(VectorOperationAdd, 1, user.Username, workspace.Name)
264+
agentsGauge.WithLabelValues(VectorOperationAdd, 1, user.Username, workspace.Name, templateName, templateVersionName)
260265

261266
connectionStatus := agent.Status(agentInactiveDisconnectTimeout)
262267
node := (*coordinator.Load()).Node(agent.ID)

coderd/prometheusmetrics/prometheusmetrics_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func TestAgents(t *testing.T) {
312312
// when
313313
closeFunc, err := prometheusmetrics.Agents(ctx, slogtest.Make(t, &slogtest.Options{
314314
IgnoreErrors: true,
315-
}), registry, db, &coordinatorPtr, derpMap, agentInactiveDisconnectTimeout, time.Millisecond)
315+
}), registry, db, &coordinatorPtr, derpMap, agentInactiveDisconnectTimeout, 50*time.Millisecond)
316316
require.NoError(t, err)
317317
t.Cleanup(closeFunc)
318318

@@ -332,8 +332,10 @@ func TestAgents(t *testing.T) {
332332
for _, metric := range metrics {
333333
switch metric.GetName() {
334334
case "coderd_agents_up":
335-
assert.Equal(t, "testuser", metric.Metric[0].Label[0].GetValue()) // Username
336-
assert.Equal(t, workspace.Name, metric.Metric[0].Label[1].GetValue()) // Workspace name
335+
assert.Equal(t, template.Name, metric.Metric[0].Label[0].GetValue()) // Template name
336+
assert.Equal(t, version.Name, metric.Metric[0].Label[1].GetValue()) // Template version name
337+
assert.Equal(t, "testuser", metric.Metric[0].Label[2].GetValue()) // Username
338+
assert.Equal(t, workspace.Name, metric.Metric[0].Label[3].GetValue()) // Workspace name
337339
assert.Equal(t, 1, int(metric.Metric[0].Gauge.GetValue())) // Metric value
338340
agentsUp = true
339341
case "coderd_agents_connections":

0 commit comments

Comments
 (0)
0