Repository navigation
Record the connection method for session app usage #31012
Description
Follow-up to #29796 (review thread).
Problem
Template insights reports each session app with a method (connection_log_method values). #29796 derives it at read time because session usage is stored per app name only: Stats.session_counts (agent proto) -> workspace_agent_stats.session_counts -> template_usage_stats_session_apps(start_time, user_id, template_id, app_name, usage_mins).
Derivation rule: app name reconnecting_pty -> reconnecting_pty, port_forwarding -> port_forwarding, any other session app -> ssh, template apps -> workspace_app. It is wrong whenever a client picks a name that does not match how it connected.
Session counts have two sources:
- The agent's
Stats.session_counts, from its SSH and web terminal servers. POST /workspaces/{id}/usage(coderd/workspaces.go), which turns the client-chosenapp_nameintosession_countsdirectly.coder port-forwardreportsport_forwarding,coder ssh --usage-appany name (defaultssh),coder vscodesshvscode, andcoder exp rptyreconnecting_pty.
Scope (database only)
Store the connection method of each session app. The agent protocol, the /usage endpoint, the CLI and the HTTP API do not change. Each current reporter uses an app name that matches its method, so coderd derives the method once, at ingest: reconnecting_pty → reconnecting_pty, port_forwarding → port_forwarding, everything else → ssh.
- Migration: add
method connection_log_method NOT NULLtotemplate_usage_stats_session_apps, fill in existing rows with the rule above, addmethodto the primary key, and allow onlyssh,reconnecting_ptyandport_forwarding. The down migration adds each app's minutes across methods, capped at 30 per bucket. - Store
workspace_agent_stats.session_countsas{method: {app: count}}. - The rollup and template insights group by (method, app). Apps past the limit fold into one
overflowrow per method. - Prometheus uses the stored method through feat!: label the VS Code family and report built-in app usage per app #29906, which is stacked on this PR. Telemetry and metricscache add up all methods, so their output does not change.
Out of scope
A method field in the agent protocol and in PostWorkspaceUsageRequest. The first reporter whose app name does not match its method adds it (see the port-forward counting follow-up, CLIENT-1053).
Done when
Part of #27413.
Generated with Coder Agents on behalf of @EhabY.
Scope narrowed to database-only storage by Coder Agents on behalf of @EhabY.
Activity
linear-code commentedon Oct 19, 2026
ENG-3474
feat!: report template insights session usage per app (#29796)
feat: record the connection method for session app usage
feat: record the connection method for session app usage
feat: record the connection method for session app usage
feat: record the connection method for session app usage
feat: record the connection method for session app usage