feat(health): add health check detail to health api

This commit is contained in:
yusing 2025-05-05 13:27:00 +08:00
parent 2cec88d3ce
commit c55c6c84bc
5 changed files with 33 additions and 0 deletions

View file

@ -65,6 +65,21 @@ func (w *Watcher) Status() health.Status {
return health.StatusNapping
}
// Detail implements health.HealthMonitor.
func (w *Watcher) Detail() string {
state := w.state.Load()
if state.err != nil {
return state.err.Error()
}
if !state.ready {
return "not ready"
}
if state.status == idlewatcher.ContainerStatusRunning {
return "starting"
}
return "napping"
}
func checkUpdateState(key string) (w *Watcher, ready bool, err error) {
watcherMapMu.RLock()
w, ok := watcherMap[key]

View file

@ -266,6 +266,12 @@ func (lb *LoadBalancer) Status() health.Status {
return status
}
// Detail implements health.HealthMonitor.
func (lb *LoadBalancer) Detail() string {
_, numHealthy := lb.status()
return fmt.Sprintf("%d/%d servers are healthy", numHealthy, lb.pool.Size())
}
func (lb *LoadBalancer) status() (status health.Status, numHealthy int) {
if lb.pool.Size() == 0 {
return health.StatusUnknown, 0

View file

@ -16,12 +16,14 @@ func getHealthInfo(r Route) map[string]string {
"status": "unknown",
"uptime": "n/a",
"latency": "n/a",
"detail": "n/a",
}
}
return map[string]string{
"status": mon.Status().String(),
"uptime": mon.Uptime().Round(time.Second).String(),
"latency": mon.Latency().Round(time.Microsecond).String(),
"detail": mon.Detail(),
}
}

View file

@ -168,6 +168,15 @@ func (mon *monitor) Latency() time.Duration {
return res.Latency
}
// Detail implements HealthMonitor.
func (mon *monitor) Detail() string {
res := mon.lastResult.Load()
if res == nil {
return ""
}
return res.Detail
}
// Name implements HealthMonitor.
func (mon *monitor) Name() string {
parts := strutils.SplitRune(mon.service, '/')

View file

@ -19,6 +19,7 @@ type (
Status() Status
Uptime() time.Duration
Latency() time.Duration
Detail() string
}
HealthMonitor interface {
task.TaskStarter