Skip to content

Commit 24f3445

Browse files
authored
chore: track workspace resource monitors in telemetry (#16776)
Addresses coder/nexus#195. Specifically, just the "tracking templates" requirement: > ## Tracking in templates > To enable resource alerts, a user must add the resource_monitoring block to a template's coder_agent resource. We'd like to track if customers have any resource monitoring enabled on a per-deployment basis. Even better, we could identify which templates are using resource monitoring.
1 parent dfcd93b commit 24f3445

File tree

10 files changed

+285
-22
lines changed

10 files changed

+285
-22
lines changed

coderd/database/dbauthz/dbauthz.go

+22
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,17 @@ func (q *querier) FetchMemoryResourceMonitorsByAgentID(ctx context.Context, agen
14381438
return q.db.FetchMemoryResourceMonitorsByAgentID(ctx, agentID)
14391439
}
14401440

1441+
func (q *querier) FetchMemoryResourceMonitorsUpdatedAfter(ctx context.Context, updatedAt time.Time) ([]database.WorkspaceAgentMemoryResourceMonitor, error) {
1442+
// Ideally, we would return a list of monitors that the user has access to. However, that check would need to
1443+
// be implemented similarly to GetWorkspaces, which is more complex than what we're doing here. Since this query
1444+
// was introduced for telemetry, we perform a simpler check.
1445+
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceWorkspaceAgentResourceMonitor); err != nil {
1446+
return nil, err
1447+
}
1448+
1449+
return q.db.FetchMemoryResourceMonitorsUpdatedAfter(ctx, updatedAt)
1450+
}
1451+
14411452
func (q *querier) FetchNewMessageMetadata(ctx context.Context, arg database.FetchNewMessageMetadataParams) (database.FetchNewMessageMetadataRow, error) {
14421453
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceNotificationMessage); err != nil {
14431454
return database.FetchNewMessageMetadataRow{}, err
@@ -1459,6 +1470,17 @@ func (q *querier) FetchVolumesResourceMonitorsByAgentID(ctx context.Context, age
14591470
return q.db.FetchVolumesResourceMonitorsByAgentID(ctx, agentID)
14601471
}
14611472

1473+
func (q *querier) FetchVolumesResourceMonitorsUpdatedAfter(ctx context.Context, updatedAt time.Time) ([]database.WorkspaceAgentVolumeResourceMonitor, error) {
1474+
// Ideally, we would return a list of monitors that the user has access to. However, that check would need to
1475+
// be implemented similarly to GetWorkspaces, which is more complex than what we're doing here. Since this query
1476+
// was introduced for telemetry, we perform a simpler check.
1477+
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceWorkspaceAgentResourceMonitor); err != nil {
1478+
return nil, err
1479+
}
1480+
1481+
return q.db.FetchVolumesResourceMonitorsUpdatedAfter(ctx, updatedAt)
1482+
}
1483+
14621484
func (q *querier) GetAPIKeyByID(ctx context.Context, id string) (database.APIKey, error) {
14631485
return fetch(q.log, q.auth, q.db.GetAPIKeyByID)(ctx, id)
14641486
}

coderd/database/dbauthz/dbauthz_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -4919,6 +4919,14 @@ func (s *MethodTestSuite) TestResourcesMonitor() {
49194919
}).Asserts(rbac.ResourceWorkspaceAgentResourceMonitor, policy.ActionUpdate)
49204920
}))
49214921

4922+
s.Run("FetchMemoryResourceMonitorsUpdatedAfter", s.Subtest(func(db database.Store, check *expects) {
4923+
check.Args(dbtime.Now()).Asserts(rbac.ResourceWorkspaceAgentResourceMonitor, policy.ActionRead)
4924+
}))
4925+
4926+
s.Run("FetchVolumesResourceMonitorsUpdatedAfter", s.Subtest(func(db database.Store, check *expects) {
4927+
check.Args(dbtime.Now()).Asserts(rbac.ResourceWorkspaceAgentResourceMonitor, policy.ActionRead)
4928+
}))
4929+
49224930
s.Run("FetchMemoryResourceMonitorsByAgentID", s.Subtest(func(db database.Store, check *expects) {
49234931
agt, w := createAgent(s.T(), db)
49244932

coderd/database/dbmem/dbmem.go

+26
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,19 @@ func (q *FakeQuerier) FetchMemoryResourceMonitorsByAgentID(_ context.Context, ag
25032503
return database.WorkspaceAgentMemoryResourceMonitor{}, sql.ErrNoRows
25042504
}
25052505

2506+
func (q *FakeQuerier) FetchMemoryResourceMonitorsUpdatedAfter(_ context.Context, updatedAt time.Time) ([]database.WorkspaceAgentMemoryResourceMonitor, error) {
2507+
q.mutex.RLock()
2508+
defer q.mutex.RUnlock()
2509+
2510+
monitors := []database.WorkspaceAgentMemoryResourceMonitor{}
2511+
for _, monitor := range q.workspaceAgentMemoryResourceMonitors {
2512+
if monitor.UpdatedAt.After(updatedAt) {
2513+
monitors = append(monitors, monitor)
2514+
}
2515+
}
2516+
return monitors, nil
2517+
}
2518+
25062519
func (q *FakeQuerier) FetchNewMessageMetadata(_ context.Context, arg database.FetchNewMessageMetadataParams) (database.FetchNewMessageMetadataRow, error) {
25072520
err := validateDatabaseType(arg)
25082521
if err != nil {
@@ -2547,6 +2560,19 @@ func (q *FakeQuerier) FetchVolumesResourceMonitorsByAgentID(_ context.Context, a
25472560
return monitors, nil
25482561
}
25492562

2563+
func (q *FakeQuerier) FetchVolumesResourceMonitorsUpdatedAfter(_ context.Context, updatedAt time.Time) ([]database.WorkspaceAgentVolumeResourceMonitor, error) {
2564+
q.mutex.RLock()
2565+
defer q.mutex.RUnlock()
2566+
2567+
monitors := []database.WorkspaceAgentVolumeResourceMonitor{}
2568+
for _, monitor := range q.workspaceAgentVolumeResourceMonitors {
2569+
if monitor.UpdatedAt.After(updatedAt) {
2570+
monitors = append(monitors, monitor)
2571+
}
2572+
}
2573+
return monitors, nil
2574+
}
2575+
25502576
func (q *FakeQuerier) GetAPIKeyByID(_ context.Context, id string) (database.APIKey, error) {
25512577
q.mutex.RLock()
25522578
defer q.mutex.RUnlock()

coderd/database/dbmetrics/querymetrics.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+81
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagentresourcemonitors.sql

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
-- name: FetchVolumesResourceMonitorsUpdatedAfter :many
2+
SELECT
3+
*
4+
FROM
5+
workspace_agent_volume_resource_monitors
6+
WHERE
7+
updated_at > $1;
8+
9+
-- name: FetchMemoryResourceMonitorsUpdatedAfter :many
10+
SELECT
11+
*
12+
FROM
13+
workspace_agent_memory_resource_monitors
14+
WHERE
15+
updated_at > $1;
16+
117
-- name: FetchMemoryResourceMonitorsByAgentID :one
218
SELECT
319
*

0 commit comments

Comments
 (0)