Skip to content

chore: report ssh stats by getting session type from env #13598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"github.com/coder/coder/v2/agent/agentssh"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/cli/cliutil"
"github.com/coder/coder/v2/coderd/autobuild/notify"
Expand Down Expand Up @@ -58,6 +59,7 @@ func (r *RootCmd) ssh() *serpent.Command {
remoteForwards []string
env []string
disableAutostart bool
noUsageTracking bool
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Expand Down Expand Up @@ -408,12 +410,27 @@ func (r *RootCmd) ssh() *serpent.Command {
return xerrors.Errorf("start shell: %w", err)
}

// track workspace usage while connection is open
closeUsage := client.UpdateWorkspaceUsageWithBodyContext(ctx, workspace.ID, codersdk.PostWorkspaceUsageRequest{
AgentID: workspaceAgent.ID,
AppName: codersdk.UsageAppNameSSH,
})
defer closeUsage()
if !noUsageTracking {
// check env for magic variable to determine the session type
appName := codersdk.UsageAppNameSSH
for _, kv := range parsedEnv {
if kv[0] == agentssh.MagicSessionTypeEnvironmentVariable {
switch kv[1] {
case agentssh.MagicSessionTypeJetBrains:
appName = codersdk.UsageAppNameJetbrains
case agentssh.MagicSessionTypeVSCode:
appName = codersdk.UsageAppNameVscode
}
}
}

// track workspace usage while connection is open
closeUsage := client.UpdateWorkspaceUsageWithBodyContext(ctx, workspace.ID, codersdk.PostWorkspaceUsageRequest{
AgentID: workspaceAgent.ID,
AppName: appName,
})
defer closeUsage()
}

// Put cancel at the top of the defer stack to initiate
// shutdown of services.
Expand Down Expand Up @@ -517,6 +534,12 @@ func (r *RootCmd) ssh() *serpent.Command {
Value: serpent.StringArrayOf(&env),
},
sshDisableAutostartOption(serpent.BoolOf(&disableAutostart)),
{
Flag: "no-usage-tracking",
Description: "Disables tracking of workspace usage.",
Env: "CODER_SSH_NO_USAGE_TRACKING",
Value: serpent.BoolOf(&noUsageTracking),
},
}
return cmd
}
Expand Down
3 changes: 3 additions & 0 deletions cli/testdata/coder_ssh_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ OPTIONS:
-l, --log-dir string, $CODER_SSH_LOG_DIR
Specify the directory containing SSH diagnostic log files.

--no-usage-tracking bool, $CODER_SSH_NO_USAGE_TRACKING
Disables tracking of workspace usage.

--no-wait bool, $CODER_SSH_NO_WAIT
Enter workspace immediately after the agent has connected. This is the
default if the template has configured the agent startup script
Expand Down
9 changes: 9 additions & 0 deletions docs/cli/ssh.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading