Skip to content

fix: only update last_used_at when connection count > 0 #10808

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

Merged
merged 2 commits into from
Nov 22, 2023
Merged
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
20 changes: 11 additions & 9 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -1702,16 +1702,18 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
}
return nil
})
errGroup.Go(func() error {
err := api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
ID: workspace.ID,
LastUsedAt: now,
if req.SessionCount() > 0 {
errGroup.Go(func() error {
err := api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
ID: workspace.ID,
LastUsedAt: now,
})
if err != nil {
return xerrors.Errorf("can't update workspace LastUsedAt: %w", err)
}
return nil
})
if err != nil {
return xerrors.Errorf("can't update workspace LastUsedAt: %w", err)
}
return nil
})
}
if api.Options.UpdateAgentMetrics != nil {
errGroup.Go(func() error {
user, err := api.Database.GetUserByID(ctx, workspace.OwnerID)
Expand Down
34 changes: 30 additions & 4 deletions coderd/workspaceagents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,21 +856,47 @@ func TestWorkspaceAgentReportStats(t *testing.T) {
agentClient.SetSessionToken(authToken)

_, err := agentClient.PostStats(context.Background(), &agentsdk.Stats{
ConnectionsByProto: map[string]int64{"TCP": 1},
// Set connection count to 1 but all session counts to zero to
// assert we aren't updating last_used_at for a connections that may
// be spawned passively by the dashboard.
ConnectionCount: 1,
RxPackets: 1,
RxBytes: 1,
TxPackets: 1,
TxBytes: 1,
SessionCountVSCode: 0,
SessionCountJetBrains: 0,
SessionCountReconnectingPTY: 0,
SessionCountSSH: 0,
ConnectionMedianLatencyMS: 10,
})
require.NoError(t, err)

newWorkspace, err := client.Workspace(context.Background(), ws.ID)
require.NoError(t, err)

assert.True(t,
newWorkspace.LastUsedAt.Equal(ws.LastUsedAt),
"%s and %s should not differ", newWorkspace.LastUsedAt, ws.LastUsedAt,
)

_, err = agentClient.PostStats(context.Background(), &agentsdk.Stats{
ConnectionsByProto: map[string]int64{"TCP": 1},
ConnectionCount: 1,
RxPackets: 1,
RxBytes: 1,
TxPackets: 1,
TxBytes: 1,
SessionCountVSCode: 1,
SessionCountJetBrains: 1,
SessionCountReconnectingPTY: 1,
SessionCountSSH: 1,
SessionCountJetBrains: 0,
SessionCountReconnectingPTY: 0,
SessionCountSSH: 0,
ConnectionMedianLatencyMS: 10,
})
require.NoError(t, err)

newWorkspace, err := client.Workspace(context.Background(), ws.ID)
newWorkspace, err = client.Workspace(context.Background(), ws.ID)
require.NoError(t, err)

assert.True(t,
Expand Down
4 changes: 4 additions & 0 deletions codersdk/agentsdk/agentsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ type Stats struct {
Metrics []AgentMetric `json:"metrics"`
}

func (s Stats) SessionCount() int64 {
return s.SessionCountVSCode + s.SessionCountJetBrains + s.SessionCountReconnectingPTY + s.SessionCountSSH
}

type AgentMetricType string

const (
Expand Down