Skip to content

Commit 51b58cf

Browse files
authored
fix: only update last_used_at when connection count > 0 (#10808)
1 parent 782fe84 commit 51b58cf

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

coderd/workspaceagents.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -1702,16 +1702,18 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
17021702
}
17031703
return nil
17041704
})
1705-
errGroup.Go(func() error {
1706-
err := api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
1707-
ID: workspace.ID,
1708-
LastUsedAt: now,
1705+
if req.SessionCount() > 0 {
1706+
errGroup.Go(func() error {
1707+
err := api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
1708+
ID: workspace.ID,
1709+
LastUsedAt: now,
1710+
})
1711+
if err != nil {
1712+
return xerrors.Errorf("can't update workspace LastUsedAt: %w", err)
1713+
}
1714+
return nil
17091715
})
1710-
if err != nil {
1711-
return xerrors.Errorf("can't update workspace LastUsedAt: %w", err)
1712-
}
1713-
return nil
1714-
})
1716+
}
17151717
if api.Options.UpdateAgentMetrics != nil {
17161718
errGroup.Go(func() error {
17171719
user, err := api.Database.GetUserByID(ctx, workspace.OwnerID)

coderd/workspaceagents_test.go

+30-4
Original file line numberDiff line numberDiff line change
@@ -856,21 +856,47 @@ func TestWorkspaceAgentReportStats(t *testing.T) {
856856
agentClient.SetSessionToken(authToken)
857857

858858
_, err := agentClient.PostStats(context.Background(), &agentsdk.Stats{
859+
ConnectionsByProto: map[string]int64{"TCP": 1},
860+
// Set connection count to 1 but all session counts to zero to
861+
// assert we aren't updating last_used_at for a connections that may
862+
// be spawned passively by the dashboard.
863+
ConnectionCount: 1,
864+
RxPackets: 1,
865+
RxBytes: 1,
866+
TxPackets: 1,
867+
TxBytes: 1,
868+
SessionCountVSCode: 0,
869+
SessionCountJetBrains: 0,
870+
SessionCountReconnectingPTY: 0,
871+
SessionCountSSH: 0,
872+
ConnectionMedianLatencyMS: 10,
873+
})
874+
require.NoError(t, err)
875+
876+
newWorkspace, err := client.Workspace(context.Background(), ws.ID)
877+
require.NoError(t, err)
878+
879+
assert.True(t,
880+
newWorkspace.LastUsedAt.Equal(ws.LastUsedAt),
881+
"%s and %s should not differ", newWorkspace.LastUsedAt, ws.LastUsedAt,
882+
)
883+
884+
_, err = agentClient.PostStats(context.Background(), &agentsdk.Stats{
859885
ConnectionsByProto: map[string]int64{"TCP": 1},
860886
ConnectionCount: 1,
861887
RxPackets: 1,
862888
RxBytes: 1,
863889
TxPackets: 1,
864890
TxBytes: 1,
865891
SessionCountVSCode: 1,
866-
SessionCountJetBrains: 1,
867-
SessionCountReconnectingPTY: 1,
868-
SessionCountSSH: 1,
892+
SessionCountJetBrains: 0,
893+
SessionCountReconnectingPTY: 0,
894+
SessionCountSSH: 0,
869895
ConnectionMedianLatencyMS: 10,
870896
})
871897
require.NoError(t, err)
872898

873-
newWorkspace, err := client.Workspace(context.Background(), ws.ID)
899+
newWorkspace, err = client.Workspace(context.Background(), ws.ID)
874900
require.NoError(t, err)
875901

876902
assert.True(t,

codersdk/agentsdk/agentsdk.go

+4
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ type Stats struct {
573573
Metrics []AgentMetric `json:"metrics"`
574574
}
575575

576+
func (s Stats) SessionCount() int64 {
577+
return s.SessionCountVSCode + s.SessionCountJetBrains + s.SessionCountReconnectingPTY + s.SessionCountSSH
578+
}
579+
576580
type AgentMetricType string
577581

578582
const (

0 commit comments

Comments
 (0)