@@ -26,7 +26,12 @@ type WorkspaceAppHealthReporter func(ctx context.Context)
26
26
27
27
// NewWorkspaceAppHealthReporter creates a WorkspaceAppHealthReporter that reports app health to coderd.
28
28
func NewWorkspaceAppHealthReporter (logger slog.Logger , apps []codersdk.WorkspaceApp , postWorkspaceAgentAppHealth PostWorkspaceAgentAppHealth ) WorkspaceAppHealthReporter {
29
+ logger = logger .Named ("apphealth" )
30
+
29
31
runHealthcheckLoop := func (ctx context.Context ) error {
32
+ ctx , cancel := context .WithCancel (ctx )
33
+ defer cancel ()
34
+
30
35
// no need to run this loop if no apps for this workspace.
31
36
if len (apps ) == 0 {
32
37
return nil
@@ -87,6 +92,7 @@ func NewWorkspaceAppHealthReporter(logger slog.Logger, apps []codersdk.Workspace
87
92
return nil
88
93
}()
89
94
if err != nil {
95
+ nowUnhealthy := false
90
96
mu .Lock ()
91
97
if failures [app .ID ] < int (app .Healthcheck .Threshold ) {
92
98
// increment the failure count and keep status the same.
@@ -96,14 +102,21 @@ func NewWorkspaceAppHealthReporter(logger slog.Logger, apps []codersdk.Workspace
96
102
// set to unhealthy if we hit the failure threshold.
97
103
// we stop incrementing at the threshold to prevent the failure value from increasing forever.
98
104
health [app .ID ] = codersdk .WorkspaceAppHealthUnhealthy
105
+ nowUnhealthy = true
99
106
}
100
107
mu .Unlock ()
108
+ logger .Debug (ctx , "error checking app health" ,
109
+ slog .F ("id" , app .ID .String ()),
110
+ slog .F ("slug" , app .Slug ),
111
+ slog .F ("now_unhealthy" , nowUnhealthy ), slog .Error (err ),
112
+ )
101
113
} else {
102
114
mu .Lock ()
103
115
// we only need one successful health check to be considered healthy.
104
116
health [app .ID ] = codersdk .WorkspaceAppHealthHealthy
105
117
failures [app .ID ] = 0
106
118
mu .Unlock ()
119
+ logger .Debug (ctx , "workspace app healthy" , slog .F ("id" , app .ID .String ()), slog .F ("slug" , app .Slug ))
107
120
}
108
121
109
122
t .Reset (time .Duration (app .Healthcheck .Interval ) * time .Second )
@@ -137,7 +150,9 @@ func NewWorkspaceAppHealthReporter(logger slog.Logger, apps []codersdk.Workspace
137
150
Healths : lastHealth ,
138
151
})
139
152
if err != nil {
140
- logger .Error (ctx , "failed to report workspace app stat" , slog .Error (err ))
153
+ logger .Error (ctx , "failed to report workspace app health" , slog .Error (err ))
154
+ } else {
155
+ logger .Debug (ctx , "sent workspace app health" , slog .F ("health" , lastHealth ))
141
156
}
142
157
}
143
158
}
0 commit comments