Skip to content

Commit efab249

Browse files
committed
chore: report ssh stats by getting session type from env
1 parent b2fb6af commit efab249

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

cli/ssh.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"cdr.dev/slog"
3030
"cdr.dev/slog/sloggers/sloghuman"
31+
"github.com/coder/coder/v2/agent/agentssh"
3132
"github.com/coder/coder/v2/cli/cliui"
3233
"github.com/coder/coder/v2/cli/cliutil"
3334
"github.com/coder/coder/v2/coderd/autobuild/notify"
@@ -58,6 +59,7 @@ func (r *RootCmd) ssh() *serpent.Command {
5859
remoteForwards []string
5960
env []string
6061
disableAutostart bool
62+
noUsageTracking bool
6163
)
6264
client := new(codersdk.Client)
6365
cmd := &serpent.Command{
@@ -408,12 +410,27 @@ func (r *RootCmd) ssh() *serpent.Command {
408410
return xerrors.Errorf("start shell: %w", err)
409411
}
410412

411-
// track workspace usage while connection is open
412-
closeUsage := client.UpdateWorkspaceUsageWithBodyContext(ctx, workspace.ID, codersdk.PostWorkspaceUsageRequest{
413-
AgentID: workspaceAgent.ID,
414-
AppName: codersdk.UsageAppNameSSH,
415-
})
416-
defer closeUsage()
413+
if !noUsageTracking {
414+
// check env for magic variable to determine the session type
415+
appName := codersdk.UsageAppNameSSH
416+
for _, kv := range parsedEnv {
417+
if kv[0] == agentssh.MagicSessionTypeEnvironmentVariable {
418+
switch kv[1] {
419+
case agentssh.MagicSessionTypeJetBrains:
420+
appName = codersdk.UsageAppNameJetbrains
421+
case agentssh.MagicSessionTypeVSCode:
422+
appName = codersdk.UsageAppNameVscode
423+
}
424+
}
425+
}
426+
427+
// track workspace usage while connection is open
428+
closeUsage := client.UpdateWorkspaceUsageWithBodyContext(ctx, workspace.ID, codersdk.PostWorkspaceUsageRequest{
429+
AgentID: workspaceAgent.ID,
430+
AppName: appName,
431+
})
432+
defer closeUsage()
433+
}
417434

418435
// Put cancel at the top of the defer stack to initiate
419436
// shutdown of services.
@@ -517,6 +534,12 @@ func (r *RootCmd) ssh() *serpent.Command {
517534
Value: serpent.StringArrayOf(&env),
518535
},
519536
sshDisableAutostartOption(serpent.BoolOf(&disableAutostart)),
537+
{
538+
Flag: "no-usage-tracking",
539+
Description: "Disables tracking of workspace usage.",
540+
Env: "CODER_SSH_NO_USAGE_TRACKING",
541+
Value: serpent.BoolOf(&noUsageTracking),
542+
},
520543
}
521544
return cmd
522545
}

0 commit comments

Comments
 (0)