8000 chore: move stat reporting into workspacestats package by f0ssel · Pull Request #13386 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

chore: move stat reporting into workspacestats package #13386

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 7 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
remove legacy test case
  • Loading branch information
f0ssel committed May 28, 2024
commit 1bf8924711546bc1035e107ea47f7faf87c22868
2 changes: 1 addition & 1 deletion coderd/apikey/apikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestGenerate(t *testing.T) {

// Assert that the hashed secret is correct.
hashed := sha256.Sum256([]byte(keytokens[1]))
assert.ElementsMatch(t, hashed, key.HashedSecret[:])
assert.ElementsMatch(t, hashed, key.HashedSecret)

assert.Equal(t, tc.params.UserID, key.UserID)
assert.WithinDuration(t, dbtime.Now(), key.CreatedAt, time.Second*5)
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/dbauthz/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (s *MethodTestSuite) NotAuthorizedErrorTest(ctx context.Context, az *coderd
// any case where the error is nil and the response is an empty slice.
if err != nil || !hasEmptySliceResponse(resp) {
s.Errorf(err, "method should an error with cancellation")
s.ErrorIsf(err, context.Canceled, "error should match context.Cancelled")
s.ErrorIsf(err, context.Canceled, "error should match context.Canceled")
}
})
}
Expand Down
28 changes: 1 addition & 27 deletions coderd/workspaceagents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,32 +939,6 @@ func TestWorkspaceAgentReportStats(t *testing.T) {
agentClient.SetSessionToken(r.AgentToken)

_, 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(), r.Workspace.ID)
require.NoError(t, err)

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

_, err = agentClient.PostStats(context.Background(), &agentsdk.Stats{
ConnectionsByProto: map[string]int64{"TCP": 1},
ConnectionCount: 1,
RxPackets: 1,
Expand All @@ -979,7 +953,7 @@ func TestWorkspaceAgentReportStats(t *testing.T) {
})
require.NoError(t, err)

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

assert.True(t,
Expand Down
2 changes: 1 addition & 1 deletion coderd/workspaceapps/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ func (s *Server) proxyWorkspaceApp(rw http.ResponseWriter, r *http.Request, appT
}

// This strips the session token from a workspace app request.
cookieHeaders := r.Header.Values("Cookie")[:]
cookieHeaders := r.Header.Values("Cookie")
r.Header.Del("Cookie")
for _, cookieHeader := range cookieHeaders {
r.Header.Add("Cookie", httpapi.StripCoderCookies(cookieHeader))
Expand Down
24 changes: 9 additions & 15 deletions coderd/workspacestats/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,16 @@ func (r *Reporter) ReportAgentStats(ctx context.Context, now time.Time, workspac
}
return nil
})
if sessionCount(stats) > 0 {
errGroup.Go(func() error {
err := r.opts.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
ID: workspace.ID,
LastUsedAt: now,
})
if err != nil {
return xerrors.Errorf("update workspace LastUsedAt: %w", err)
}
return nil
errGroup.Go(func() error {
err := r.opts.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
ID: workspace.ID,
LastUsedAt: now,
})
}
if err != nil {
return xerrors.Errorf("update workspace LastUsedAt: %w", err)
}
return nil
})
if r.opts.UpdateAgentMetricsFn != nil {
errGroup.Go(func() error {
user, err := r.opts.Database.GetUserByID(ctx, workspace.OwnerID)
Expand Down Expand Up @@ -194,7 +192,3 @@ func (r *Reporter) ReportAgentStats(ctx context.Context, now time.Time, workspac

return nil
}

func sessionCount(s *agentproto.Stats) int64 {
return s.SessionCountVscode + s.SessionCountJetbrains + s.SessionCountReconnectingPty + s.SessionCountSsh
}
0