-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: add workspace agent session count protocol #28125
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package workspacestats | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "maps" | ||
| "slices" | ||
|
|
||
| agentproto "github.com/coder/coder/v2/agent/proto" | ||
| "github.com/coder/coder/v2/codersdk" | ||
| ) | ||
|
|
||
| // deprecatedSessionCounts returns the fixed session_count_* fields keyed by | ||
| // normalized app name. Agents predating API v2.11 set these instead of | ||
| // session_counts, never both. | ||
| func deprecatedSessionCounts(st *agentproto.Stats) map[string]int64 { | ||
| return map[string]int64{ | ||
| string(codersdk.AppFamilyVSCode): st.SessionCountVscode, | ||
| string(codersdk.AppFamilyJetBrains): st.SessionCountJetbrains, | ||
| string(codersdk.AppFamilyReconnectingPTY): st.SessionCountReconnectingPty, | ||
| string(codersdk.AppFamilySSH): st.SessionCountSsh, | ||
| } | ||
| } | ||
|
|
||
| // normalizedSessionCounts returns per-app session counts, names normalized and | ||
| // non-positive counts dropped. | ||
| func normalizedSessionCounts(st *agentproto.Stats) map[string]int64 { | ||
| reported := st.GetSessionCounts() | ||
| if len(reported) == 0 { | ||
| reported = deprecatedSessionCounts(st) | ||
| } | ||
| counts := make(map[string]int64, len(reported)) | ||
| for app, count := range reported { | ||
| if count > 0 { | ||
| counts[codersdk.NormalizeAppName(app)] += count | ||
| } | ||
| } | ||
| return counts | ||
| } | ||
|
|
||
| // maxSessionCountEntries bounds distinct app names per stats report. Overflow | ||
| // aggregates under AppFamilyUnknown. | ||
| const maxSessionCountEntries = 64 | ||
|
|
||
| // capSessionCounts keeps the busiest maxSessionCountEntries names, preferring | ||
| // known apps, and sums the rest into AppFamilyUnknown, so the result can hold | ||
| // one name past the cap. | ||
| func capSessionCounts(counts map[string]int64) map[string]int64 { | ||
| if len(counts) <= maxSessionCountEntries { | ||
| return counts | ||
| } | ||
| // Known apps rank 0, unknown 1, so known apps win the cap. | ||
| rank := func(name string) int { | ||
| if codersdk.AppNameFamily(name) == codersdk.AppFamilyUnknown { | ||
| return 1 | ||
| } | ||
| return 0 | ||
| } | ||
| ranked := slices.SortedFunc(maps.Keys(counts), func(a, b string) int { | ||
| return cmp.Or( | ||
| cmp.Compare(rank(a), rank(b)), // known apps first | ||
| cmp.Compare(counts[b], counts[a]), // then busiest | ||
| cmp.Compare(a, b), // then name, so the cut is stable | ||
| ) | ||
| }) | ||
| capped := make(map[string]int64, maxSessionCountEntries+1) | ||
| for _, name := range ranked[:maxSessionCountEntries] { | ||
| capped[name] = counts[name] | ||
| } | ||
| for _, name := range ranked[maxSessionCountEntries:] { | ||
| capped[string(codersdk.AppFamilyUnknown)] += counts[name] | ||
| } | ||
| return capped | ||
| } | ||
|
|
||
| // HasSessionCounts reports whether the stats contain any active session. | ||
| func HasSessionCounts(st *agentproto.Stats) bool { | ||
| return len(normalizedSessionCounts(st)) > 0 | ||
| } | ||
|
|
||
| // ClearSessionCounts zeroes every session count on the given stats. | ||
| func ClearSessionCounts(st *agentproto.Stats) { | ||
| st.SessionCounts = nil | ||
| st.SessionCountVscode = 0 | ||
| st.SessionCountJetbrains = 0 | ||
| st.SessionCountReconnectingPty = 0 | ||
| st.SessionCountSsh = 0 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package workspacestats | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| agentproto "github.com/coder/coder/v2/agent/proto" | ||
| ) | ||
|
|
||
| // sessionCountsFromProto is what the batcher applies on ingest. | ||
| func sessionCountsFromProto(st *agentproto.Stats) map[string]int64 { | ||
| return capSessionCounts(normalizedSessionCounts(st)) | ||
| } | ||
|
|
||
| func TestSessionCountsFromProto(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // The deprecated fields simulate agents older than API v2.11. | ||
| for _, tc := range []struct { | ||
| name string | ||
| stats *agentproto.Stats | ||
| want map[string]int64 | ||
| }{ | ||
| { | ||
| "Empty", | ||
| &agentproto.Stats{}, | ||
| map[string]int64{}, | ||
| }, | ||
| { | ||
| "NormalizesAndMergesNames", | ||
| &agentproto.Stats{SessionCounts: map[string]int64{"VSCode": 1, "vscode": 2, "Reconnecting-PTY": 1}}, | ||
| map[string]int64{"vscode": 3, "reconnecting_pty": 1}, | ||
| }, | ||
| { | ||
| "DropsNonPositiveEntries", | ||
| &agentproto.Stats{SessionCounts: map[string]int64{"vscode": 1, "reconnecting_pty": 0, "bogus": -1}}, | ||
| map[string]int64{"vscode": 1}, | ||
| }, | ||
| { | ||
| "MapTakesPrecedenceOverDeprecatedFields", | ||
| &agentproto.Stats{SessionCounts: map[string]int64{"cursor": 2, "ssh": 1}, SessionCountVscode: 9}, | ||
| map[string]int64{"cursor": 2, "ssh": 1}, | ||
| }, | ||
| { | ||
| "OldAgentFallback", | ||
| &agentproto.Stats{SessionCountVscode: 3, SessionCountJetbrains: 1, SessionCountSsh: 2}, | ||
| map[string]int64{"vscode": 3, "jetbrains": 1, "ssh": 2}, | ||
| }, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| require.Equal(t, tc.want, sessionCountsFromProto(tc.stats)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCapSessionCounts(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Known names carry the lowest counts and the busiest name sorts last, so | ||
| // only the ordering rules can keep them under the cap. | ||
| counts := map[string]int64{"ssh": 1, "vscode": 1, "zzz-busy-ide": 5} | ||
| const overflowing = 200 | ||
| for i := range overflowing { | ||
| counts[fmt.Sprintf("0ide-%03d", i)] = 2 | ||
| } | ||
|
|
||
| got := sessionCountsFromProto(&agentproto.Stats{SessionCounts: counts}) | ||
|
|
||
| require.Len(t, got, maxSessionCountEntries+1, "cap plus the unknown bucket") | ||
| require.EqualValues(t, 1, got["ssh"]) | ||
| require.EqualValues(t, 1, got["vscode"]) | ||
| require.EqualValues(t, 5, got["zzz_busy_ide"]) | ||
| // Those three plus 61 more fill the cap; the other 139 sum into unknown. | ||
| require.EqualValues(t, 2, got["0ide_000"]) | ||
| require.NotContains(t, got, "0ide_199") | ||
| require.EqualValues(t, (overflowing-61)*2, got["unknown"]) | ||
|
|
||
| var reported, stored int64 | ||
| for _, count := range counts { | ||
| reported += count | ||
| } | ||
| for _, count := range got { | ||
| stored += count | ||
| } | ||
| require.Equal(t, reported, stored, "capping must not lose counts") | ||
| } | ||
|
|
||
| func TestHasSessionCounts(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| for _, tc := range []struct { | ||
| name string | ||
| stats *agentproto.Stats | ||
| want bool | ||
| }{ | ||
| {"Empty", &agentproto.Stats{}, false}, | ||
| {"MapPositive", &agentproto.Stats{SessionCounts: map[string]int64{"cursor": 1}}, true}, | ||
| {"MapZeroOnly", &agentproto.Stats{SessionCounts: map[string]int64{"ssh": 0}}, false}, | ||
| {"DeprecatedFieldPositive", &agentproto.Stats{SessionCountSsh: 2}, true}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| require.Equal(t, tc.want, HasSessionCounts(tc.stats)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestClearSessionCounts(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| st := &agentproto.Stats{ | ||
| SessionCounts: map[string]int64{"vscode": 1}, | ||
| SessionCountVscode: 1, | ||
| SessionCountSsh: 2, | ||
| } | ||
| ClearSessionCounts(st) | ||
| require.Empty(t, sessionCountsFromProto(st)) | ||
| require.False(t, HasSessionCounts(st)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.