feat: add WatchAllWorkspaceBuilds endpoint for autostart scaletests (#22057)

This PR adds a `WatchAllWorkspaces` function with `watch-all-workspaces`
endpoint, which can be used to listen on a single global pubsub channel
for _all_ workspace build updates, and makes use of it in the autostart
scaletest.

This negates the need to use a workspace watch pubsub channel _per_
workspace, which has auth overhead associated with each call. This is
especially relevant in situations such as the autostart scaletest, where
we need to start/stop a set of workspaces before we can configure their
autostart config. The overhead associated with all the watch requests
skews the scaletest results and makes it harder to reason about the
performance of the autostart feature itself.

The autostart scaletest also no longer generates its own metrics nor
does it wait for all the workspaces to actually start via autostart. We
should update the scaletest dashboard after both PRs are merged to
measure autostart performance via the new metrics.



The new function/endpoint and its usage in the autostart scaletest are
gated behind an experiment feature flag, this is something we should
discuss whether we want to enable the endpoint in prod by default or
not. If so, we can remove the experiment.

---------

Signed-off-by: Callum Styan <callumstyan@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Callum Styan <callum@coder.com>
This commit is contained in:
Callum Styan
2026-03-13 20:37:41 -07:00
committed by GitHub
co-authored by Claude Opus 4.5 Callum Styan
parent b492c42624
commit 36665e17b2
23 changed files with 1296 additions and 249 deletions
+74
View File
@@ -43,6 +43,8 @@ import (
"github.com/coder/coder/v2/coderd/wspubsub"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
"github.com/coder/coder/v2/codersdk/wsjson"
"github.com/coder/websocket"
)
var (
@@ -2175,6 +2177,78 @@ func (api *API) watchWorkspace(
}
}
// @Summary Watch all workspace builds
// @ID watch-all-workspace-builds
// @Security CoderSessionToken
// @Produce json
// @Tags Workspaces
// @Success 101
// @Router /experimental/watch-all-workspacebuilds [get]
// @x-apidocgen {"skip": true}
func (api *API) watchAllWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Buffer enough updates to avoid blocking the pubsub callback while we're
// accepting the WebSocket connection. Accepting the connection signals to
// the client that the server is subscribed and ready to forward events.
updates := make(chan codersdk.WorkspaceBuildUpdate, 256)
cancelSubscribe, err := api.Pubsub.SubscribeWithErr(wspubsub.AllWorkspaceEventChannel,
wspubsub.HandleWorkspaceBuildUpdate(
func(_ context.Context, update codersdk.WorkspaceBuildUpdate, err error) {
if err != nil {
api.Logger.Warn(ctx, "workspace build update subscription error", slog.Error(err))
return
}
select {
case updates <- update:
default:
api.Logger.Warn(ctx, "workspace build update dropped, client too slow")
}
}))
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error subscribing to workspace build events.",
Detail: err.Error(),
})
return
}
defer cancelSubscribe()
conn, err := websocket.Accept(rw, r, nil)
if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Failed to accept WebSocket.",
Detail: err.Error(),
})
return
}
defer conn.Close(websocket.StatusNormalClosure, "done")
// CloseRead starts a goroutine to read and discard messages from the client,
// including Pong messages sent in response to our Ping heartbeats.
_ = conn.CloseRead(context.Background())
ctx, cancel := context.WithCancel(ctx)
go httpapi.HeartbeatClose(ctx, api.Logger, cancel, conn)
defer cancel()
enc := wsjson.NewEncoder[codersdk.WorkspaceBuildUpdate](conn, websocket.MessageText)
for {
select {
case <-ctx.Done():
return
case update, ok := <-updates:
if !ok {
return
}
if err := enc.Encode(update); err != nil {
return
}
}
}
}
// @Summary Get workspace timings by ID
// @ID get-workspace-timings-by-id
// @Security CoderSessionToken