@@ -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,17 @@ 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" ,slog .F ("id" ,app .ID .String ()),slog .F ("slug" ,app .Slug ),slog .F ("now_unhealthy" ,nowUnhealthy ),slog .Error (err ))
101
109
}else {
102
110
mu .Lock ()
103
111
// we only need one successful health check to be considered healthy.
104
112
health [app .ID ]= codersdk .WorkspaceAppHealthHealthy
105
113
failures [app .ID ]= 0
106
114
mu .Unlock ()
115
+ logger .Debug (ctx ,"workspace app healthy" ,slog .F ("id" ,app .ID .String ()),slog .F ("slug" ,app .Slug ))
107
116
}
108
117
109
118
t .Reset (time .Duration (app .Healthcheck .Interval )* time .Second )
@@ -137,7 +146,9 @@ func NewWorkspaceAppHealthReporter(logger slog.Logger, apps []codersdk.Workspace
137
146
Healths :lastHealth ,
138
147
})
139
148
if err != nil {
140
- logger .Error (ctx ,"failed to report workspace app stat" ,slog .Error (err ))
149
+ logger .Error (ctx ,"failed to report workspace app health" ,slog .Error (err ))
150
+ }else {
151
+ logger .Debug (ctx ,"sent workspace app health" ,slog .F ("health" ,lastHealth ))
141
152
}
142
153
}
143
154
}