Repository navigation
feat: export workspace last used time to Prometheus - #31346
Merged
Merged
Conversation
Contributor
Docs previewCheck off each page once it's been reviewed. If a page changes in a later push, its checkbox clears automatically so it gets a fresh look. Pages not yet wired into the docs navigation aren't listed here. |
EhabY
force-pushed
the
feat/workspace-last-used-metric
branch
from
October 27, 2026 14:08
5241b0b to
1f50af0
Compare
EhabY
marked this pull request as ready for review
October 27, 2026 14:10
EhabY
force-pushed
the
feat/workspace-last-used-metric
branch
from
October 27, 2026 14:19
1f50af0 to
90104b5
Compare
jeremyruppel
reviewed
Nov 9, 2026
jeremyruppel
left a comment
Collaborator
There was a problem hiding this comment.
A couple of small simplifications to WorkspaceStats. None of these are blocking.
I know the file's other collectors use a goroutine with a select loop. I think using TickerFunc here is the right choice: newer code in coderd uses it (metricscache, notifications, agentapi, cryptokeys), and it lets TestWorkspaceStats use Advance().MustWait() instead of polling the way TestUsers does. So these comments only tighten up how TickerFunc is used.
Generated by Coder Agents on behalf of @jeremyruppel.
EhabY
force-pushed
the
feat/workspace-last-used-metric
branch
from
November 10, 2026 14:02
6ac22d3 to
994fd25
Compare
jeremyruppel
approved these changes
Nov 10, 2026
Adds an opt-in coderd_workspace_last_used_timestamp_seconds gauge behind --prometheus-collect-workspace-stats, collected by a separate WorkspaceStats collector from a new GetWorkspaceLastUsedForMetrics query. Closes CLIENT-1065.
Add a workspace_id label so a workspace that reuses a deleted workspace's name gets its own series, and skip workspaces owned by deleted users, whose usernames can be reused.
…ckground Run the first collect in the collector goroutine so server startup does not wait on the query, pass collect straight to TickerFunc, and drop the unreachable ticker error branch.
The new-code errcheck config rejects blank-discarded errors, so collect no longer returns an error and the ticker's Wait result is checked.
EhabY
force-pushed
the
feat/workspace-last-used-metric
branch
from
November 11, 2026 14:41
5516c95 to
1e00035
Compare
EhabY
enabled auto-merge (squash)
November 11, 2026 14:52
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds an opt-in
coderd_workspace_last_used_timestamp_seconds{organization_name, template_name, workspace_owner, workspace_name, workspace_id}gauge. It reportsworkspaces.last_used_at, the value the dormancy threshold measures from.Closes CLIENT-1065. Follow-up to a comment on CLIENT-1034 (#29940), which only covers per-template app usage time.
Changes
--prometheus-collect-workspace-stats/CODER_PROMETHEUS_COLLECT_WORKSPACE_STATS, defaultfalse.prometheusmetrics.WorkspaceStatscollector, which runs only when the flag is set and refreshes once a minute on aquartzticker.GetWorkspaceLastUsedForMetricsquery, with a dbauthz entry.Decisions
--prometheus-collect-agent-statsand--prometheus-collect-db-metrics.Workspaces()or add a collector?Workspaces()is control coupling. The separate query skips the latest-build lookup, so it costs about a tenth of the shared scan, andWorkspaces()stays unchanged.collect-workspace-statscollect-agent-statsand theworkspacestatspackage. "Workspace usage" is already the name of an experiment.workspace_ownercoderd_workspace_latest_build_status.workspace_idworkspace_owner="prebuilds". A deleted user's username can be reused, so their rows are excluded rather than colliding with a live user's series.Accuracy
With default settings, the value trails real use by up to about 2.5 minutes plus the scrape interval:
"Use" means traffic on any agent connection, app traffic through Coder, or the default
coder ssh/coder port-forwardheartbeat (once a minute, even when idle). The value is also set to the current time on creation, on activating a dormant workspace, and when a template admin resets inactivity.Benchmarks and verification
Postgres 17, 5,000 users, 50 templates, 2 builds per workspace. pgbench, 1 client:
GetWorkspaceLastUsedForMetricsGetWorkspacesForWorkspaceMetrics(existing)Gauge with 4 labels (GOMAXPROCS=12):
coder serveragainst 20,000 workspaces:TestWorkspaceStatsuses a mock clock. It covers running, stopped and prebuilt workspaces, excludes deleted workspaces and workspaces of deleted users, checks that a recreated workspace with the same name gets a new series, and checks that alast_used_atupdate and a deletion are picked up on the next tick. It waits for the first collect by trapping theTickerFunccall. It fails on a mutated value, a skipped commit, and a skipped first collect, and passes 300 times under-race -cpu 1,2,8.make pre-commitpasses. The doc-check self-review found no remaining blocking findings.Possible follow-up: incremental refresh
Each tick reloads every live workspace. On Postgres 17 with 147k live workspaces (98k users, 300 templates), the full query takes about 183 ms end to end for 14 MB of rows. The plan is already the cheapest available. Returning epoch seconds or resolving names in Go did not help meaningfully.
Loading only rows whose
last_used_atmoved since the previous tick:It still needs a periodic full refresh to drop deleted workspaces and pick up renames and deleted owners. With one every 10 ticks, the average cost per tick is about 2.5x to 6x lower. It also needs an overlapping time window for late saves from other replicas, and the gauge would need per-series updates instead of the full rebuild
CachedGaugeVecdoes on each commit. Not worth the complexity at this scale; worth revisiting if a large deployment shows the cost matters. An index onlast_used_atwas rejected: it barely helped, and it would slow the table's most frequent update.Generated by Coder Agents on behalf of @EhabY.