Skip to content

Record the connection method for session app usage #31012

Open

Description

@EhabY

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-chosen app_name into session_counts directly. coder port-forward reports port_forwarding, coder ssh --usage-app any name (default ssh), coder vscodessh vscode, and coder exp rpty reconnecting_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 NULL to template_usage_stats_session_apps, fill in existing rows with the rule above, add method to the primary key, and allow only ssh, reconnecting_pty and port_forwarding. The down migration adds each app's minutes across methods, capped at 30 per bucket.
  • Store workspace_agent_stats.session_counts as {method: {app: count}}.
  • The rollup and template insights group by (method, app). Apps past the limit fold into one overflow row 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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions