Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 165b6fb

Browse files
authored
fix: Use app slugs instead of the display name to report health (#4944)
All applications without display names were reporting broken health.
1 parent 50ad4a8 commit 165b6fb

File tree

5 files changed

+39
-47
lines changed

5 files changed

+39
-47
lines changed

agent/apphealth.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"golang.org/x/xerrors"
10+
"github.com/google/uuid"
1011

1112
"cdr.dev/slog"
1213
"github.com/coder/coder/codersdk"
@@ -31,9 +32,9 @@ func NewWorkspaceAppHealthReporter(logger slog.Logger, apps []codersdk.Workspace
3132
}
3233

3334
hasHealthchecksEnabled := false
34-
health := make(map[string]codersdk.WorkspaceAppHealth, 0)
35+
health := make(map[uuid.UUID]codersdk.WorkspaceAppHealth, 0)
3536
for _, app := range apps {
36-
health[app.DisplayName] = app.Health
37+
health[app.ID] = app.Health
3738
if !hasHealthchecksEnabled && app.Health != codersdk.WorkspaceAppHealthDisabled {
3839
hasHealthchecksEnabled = true
3940
}
@@ -46,7 +47,7 @@ func NewWorkspaceAppHealthReporter(logger slog.Logger, apps []codersdk.Workspace
4647

4748
// run a ticker for each app health check.
4849
var mu sync.RWMutex
49-
failures := make(map[string]int, 0)
50+
failures := make(map[uuid.UUID]int, 0)
5051
for _, nextApp := range apps {
5152
if !shouldStartTicker(nextApp) {
5253
continue
@@ -85,21 +86,21 @@ func NewWorkspaceAppHealthReporter(logger slog.Logger, apps []codersdk.Workspace
8586
}()
8687
if err != nil {
8788
mu.Lock()
88-
if failures[app.DisplayName] < int(app.Healthcheck.Threshold) {
89+
if failures[app.ID] < int(app.Healthcheck.Threshold) {
8990
// increment the failure count and keep status the same.
9091
// we will change it when we hit the threshold.
91-
failures[app.DisplayName]++
92+
failures[app.ID]++
9293
} else {
9394
// set to unhealthy if we hit the failure threshold.
9495
// we stop incrementing at the threshold to prevent the failure value from increasing forever.
95-
health[app.DisplayName] = codersdk.WorkspaceAppHealthUnhealthy
96+
health[app.ID] = codersdk.WorkspaceAppHealthUnhealthy
9697
}
9798
mu.Unlock()
9899
} else {
99100
mu.Lock()
100101
// we only need one successful health check to be considered healthy.
101-
health[app.DisplayName] = codersdk.WorkspaceAppHealthHealthy
102-
failures[app.DisplayName] = 0
102+
health[app.ID] = codersdk.WorkspaceAppHealthHealthy
103+
failures[app.ID] = 0
103104
mu.Unlock()
104105
}
105106

@@ -155,7 +156,7 @@ func shouldStartTicker(app codersdk.WorkspaceApp) bool {
155156
return app.Healthcheck.URL != "" && app.Healthcheck.Interval > 0 && app.Healthcheck.Threshold > 0
156157
}
157158

158-
func healthChanged(old map[string]codersdk.WorkspaceAppHealth, new map[string]codersdk.WorkspaceAppHealth) bool {
159+
func healthChanged(old map[uuid.UUID]codersdk.WorkspaceAppHealth, new map[uuid.UUID]codersdk.WorkspaceAppHealth) bool {
159160
for name, newValue := range new {
160161
oldValue, found := old[name]
161162
if !found {
@@ -169,8 +170,8 @@ func healthChanged(old map[string]codersdk.WorkspaceAppHealth, new map[string]co
169170
return false
170171
}
171172

172-
func copyHealth(h1 map[string]codersdk.WorkspaceAppHealth) map[string]codersdk.WorkspaceAppHealth {
173-
h2 := make(map[string]codersdk.WorkspaceAppHealth, 0)
173+
func copyHealth(h1 map[uuid.UUID]codersdk.WorkspaceAppHealth) map[uuid.UUID]codersdk.WorkspaceAppHealth {
174+
h2 := make(map[uuid.UUID]codersdk.WorkspaceAppHealth, 0)
174175
for k, v := range h1 {
175176
h2[k] = v
176177
}

agent/apphealth_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ func TestAppHealth(t *testing.T) {
2727
defer cancel()
2828
apps := []codersdk.WorkspaceApp{
2929
{
30-
DisplayName: "app1",
30+
Slug: "app1",
3131
Healthcheck: codersdk.Healthcheck{},
3232
Health: codersdk.WorkspaceAppHealthDisabled,
3333
},
3434
{
35-
DisplayName: "app2",
35+
Slug: "app2",
3636
Healthcheck: codersdk.Healthcheck{
3737
// URL: We don't set the URL for this test because the setup will
3838
// create a httptest server for us and set it for us.
@@ -69,7 +69,7 @@ func TestAppHealth(t *testing.T) {
6969
defer cancel()
7070
apps := []codersdk.WorkspaceApp{
7171
{
72-
DisplayName: "app2",
72+
Slug: "app2",
7373
Healthcheck: codersdk.Healthcheck{
7474
// URL: We don't set the URL for this test because the setup will
7575
// create a httptest server for us and set it for us.
@@ -102,7 +102,7 @@ func TestAppHealth(t *testing.T) {
102102
defer cancel()
103103
apps := []codersdk.WorkspaceApp{
104104
{
105-
DisplayName: "app2",
105+
Slug: "app2",
106106
Healthcheck: codersdk.Healthcheck{
107107
// URL: We don't set the URL for this test because the setup will
108108
// create a httptest server for us and set it for us.
@@ -137,7 +137,7 @@ func TestAppHealth(t *testing.T) {
137137
defer cancel()
138138
apps := []codersdk.WorkspaceApp{
139139
{
140-
DisplayName: "app2",
140+
Slug: "app2",
141141
Healthcheck: codersdk.Healthcheck{
142142
// URL: We don't set the URL for this test because the setup will
143143
// create a httptest server for us and set it for us.
@@ -185,9 +185,9 @@ func setupAppReporter(ctx context.Context, t *testing.T, apps []codersdk.Workspa
185185
}
186186
postWorkspaceAgentAppHealth := func(_ context.Context, req codersdk.PostWorkspaceAppHealthsRequest) error {
187187
mu.Lock()
188-
for name, health := range req.Healths {
188+
for id, health := range req.Healths {
189189
for i, app := range apps {
190-
if app.DisplayName != name {
190+
if app.ID != id {
191191
continue
192192
}
193193
app.Health = health

coderd/workspaceagents.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,10 @@ func (api *API) postWorkspaceAppHealth(rw http.ResponseWriter, r *http.Request)
913913
}
914914

915915
var newApps []database.WorkspaceApp
916-
for name, newHealth := range req.Healths {
916+
for id, newHealth := range req.Healths {
917917
old := func() *database.WorkspaceApp {
918918
for _, app := range apps {
919-
if app.DisplayName == name {
919+
if app.ID == id {
920920
return &app
921921
}
922922
}
@@ -926,15 +926,15 @@ func (api *API) postWorkspaceAppHealth(rw http.ResponseWriter, r *http.Request)
926926
if old == nil {
927927
httpapi.Write(r.Context(), rw, http.StatusNotFound, codersdk.Response{
928928
Message: "Error setting workspace app health",
929-
Detail: xerrors.Errorf("workspace app name %s not found", name).Error(),
929+
Detail: xerrors.Errorf("workspace app name %s not found", id).Error(),
930930
})
931931
return
932932
}
933933

934934
if old.HealthcheckUrl == "" {
935935
httpapi.Write(r.Context(), rw, http.StatusNotFound, codersdk.Response{
936936
Message: "Error setting workspace app health",
937-
Detail: xerrors.Errorf("health checking is disabled for workspace app %s", name).Error(),
937+
Detail: xerrors.Errorf("health checking is disabled for workspace app %s", id).Error(),
938938
})
939939
return
940940
}

coderd/workspaceagents_test.go

+15-24
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,8 @@ func TestWorkspaceAgentListeningPorts(t *testing.T) {
555555
// should not exist in the response.
556556
_, appLPort := generateUnfilteredPort(t)
557557
app := &proto.App{
558-
Slug: "test-app",
559-
DisplayName: "test-app",
560-
Url: fmt.Sprintf("http://localhost:%d", appLPort),
558+
Slug: "test-app",
559+
Url: fmt.Sprintf("http://localhost:%d", appLPort),
561560
}
562561

563562
// Generate a filtered port that should not exist in the response.
@@ -624,11 +623,10 @@ func TestWorkspaceAgentAppHealth(t *testing.T) {
624623
authToken := uuid.NewString()
625624
apps := []*proto.App{
626625
{
627-
Slug: "code-server",
628-
DisplayName: "code-server",
629-
Command: "some-command",
630-
Url: "http://localhost:3000",
631-
Icon: "/code.svg",
626+
Slug: "code-server",
627+
Command: "some-command",
628+
Url: "http://localhost:3000",
629+
Icon: "/code.svg",
632630
},
633631
{
634632
Slug: "code-server-2",
@@ -683,31 +681,24 @@ func TestWorkspaceAgentAppHealth(t *testing.T) {
683681
// empty
684682
err = agentClient.PostWorkspaceAgentAppHealth(ctx, codersdk.PostWorkspaceAppHealthsRequest{})
685683
require.Error(t, err)
686-
// invalid name
687-
err = agentClient.PostWorkspaceAgentAppHealth(ctx, codersdk.PostWorkspaceAppHealthsRequest{
688-
Healths: map[string]codersdk.WorkspaceAppHealth{
689-
"bad-name": codersdk.WorkspaceAppHealthDisabled,
690-
},
691-
})
692-
require.Error(t, err)
693-
// healcheck disabled
684+
// healthcheck disabled
694685
err = agentClient.PostWorkspaceAgentAppHealth(ctx, codersdk.PostWorkspaceAppHealthsRequest{
695-
Healths: map[string]codersdk.WorkspaceAppHealth{
696-
"code-server": codersdk.WorkspaceAppHealthInitializing,
686+
Healths: map[uuid.UUID]codersdk.WorkspaceAppHealth{
687+
metadata.Apps[0].ID: codersdk.WorkspaceAppHealthInitializing,
697688
},
698689
})
699690
require.Error(t, err)
700691
// invalid value
701692
err = agentClient.PostWorkspaceAgentAppHealth(ctx, codersdk.PostWorkspaceAppHealthsRequest{
702-
Healths: map[string]codersdk.WorkspaceAppHealth{
703-
"code-server-2": codersdk.WorkspaceAppHealth("bad-value"),
693+
Healths: map[uuid.UUID]codersdk.WorkspaceAppHealth{
694+
metadata.Apps[1].ID: codersdk.WorkspaceAppHealth("bad-value"),
704695
},
705696
})
706697
require.Error(t, err)
707698
// update to healthy
708699
err = agentClient.PostWorkspaceAgentAppHealth(ctx, codersdk.PostWorkspaceAppHealthsRequest{
709-
Healths: map[string]codersdk.WorkspaceAppHealth{
710-
"code-server-2": codersdk.WorkspaceAppHealthHealthy,
700+
Healths: map[uuid.UUID]codersdk.WorkspaceAppHealth{
701+
metadata.Apps[1].ID: codersdk.WorkspaceAppHealthHealthy,
711702
},
712703
})
713704
require.NoError(t, err)
@@ -716,8 +707,8 @@ func TestWorkspaceAgentAppHealth(t *testing.T) {
716707
require.EqualValues(t, codersdk.WorkspaceAppHealthHealthy, metadata.Apps[1].Health)
717708
// update to unhealthy
718709
err = agentClient.PostWorkspaceAgentAppHealth(ctx, codersdk.PostWorkspaceAppHealthsRequest{
719-
Healths: map[string]codersdk.WorkspaceAppHealth{
720-
"code-server-2": codersdk.WorkspaceAppHealthUnhealthy,
710+
Healths: map[uuid.UUID]codersdk.WorkspaceAppHealth{
711+
metadata.Apps[1].ID: codersdk.WorkspaceAppHealthUnhealthy,
721712
},
722713
})
723714
require.NoError(t, err)

codersdk/workspaceapps.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ type Healthcheck struct {
5454
// @typescript-ignore PostWorkspaceAppHealthsRequest
5555
type PostWorkspaceAppHealthsRequest struct {
5656
// Healths is a map of the workspace app name and the health of the app.
57-
Healths map[string]WorkspaceAppHealth
57+
Healths map[uuid.UUID]WorkspaceAppHealth
5858
}

0 commit comments

Comments
 (0)