fix(healthcheck): ensure detail is included on error
Some checks are pending
Docker Image CI (nightly) / build-nightly (push) Waiting to run
Docker Image CI (nightly) / build-nightly-agent (push) Waiting to run

This commit is contained in:
yusing 2025-05-09 12:26:31 +08:00
parent 99eccd0b95
commit 6797897814

View file

@ -188,15 +188,13 @@ func (mon *monitor) String() string {
return mon.Name() return mon.Name()
} }
var resHealthy = &health.HealthCheckResult{ var resHealthy = health.HealthCheckResult{Healthy: true}
Healthy: true,
}
// MarshalJSON implements health.HealthMonitor. // MarshalJSON implements health.HealthMonitor.
func (mon *monitor) MarshalJSON() ([]byte, error) { func (mon *monitor) MarshalJSON() ([]byte, error) {
res := mon.lastResult.Load() res := mon.lastResult.Load()
if res == nil { if res == nil {
res = resHealthy res = &resHealthy
} }
return (&health.JSONRepresentation{ return (&health.JSONRepresentation{
@ -215,24 +213,22 @@ func (mon *monitor) MarshalJSON() ([]byte, error) {
func (mon *monitor) checkUpdateHealth() error { func (mon *monitor) checkUpdateHealth() error {
logger := logging.With().Str("name", mon.Name()).Logger() logger := logging.With().Str("name", mon.Name()).Logger()
result, err := mon.checkHealth() result, err := mon.checkHealth()
if err != nil {
defer mon.task.Finish(err)
mon.status.Store(health.StatusError)
if !errors.Is(err, context.Canceled) {
return fmt.Errorf("check health: %w", err)
}
return nil
}
mon.lastResult.Store(result) var lastStatus health.Status
var status health.Status if err != nil {
if result.Healthy { if result == nil {
status = health.StatusHealthy result = &health.HealthCheckResult{Healthy: false, Detail: err.Error()}
}
lastStatus = mon.status.Swap(health.StatusError)
} else if result.Healthy {
lastStatus = mon.status.Swap(health.StatusHealthy)
UpdateLastSeen(mon.service) UpdateLastSeen(mon.service)
} else { } else {
status = health.StatusUnhealthy lastStatus = mon.status.Swap(health.StatusUnhealthy)
} }
if result.Healthy != (mon.status.Swap(status) == health.StatusHealthy) { mon.lastResult.Store(result)
if result.Healthy != (lastStatus == health.StatusHealthy) {
extras := notif.FieldsBody{ extras := notif.FieldsBody{
{Name: "Service Name", Value: mon.service}, {Name: "Service Name", Value: mon.service},
{Name: "Time", Value: strutils.FormatTime(time.Now())}, {Name: "Time", Value: strutils.FormatTime(time.Now())},
@ -264,5 +260,5 @@ func (mon *monitor) checkUpdateHealth() error {
} }
} }
return nil return err
} }