fix docker healthcheck formatting

This commit is contained in:
yusing 2024-12-13 12:44:20 +08:00
parent 17e8532e6f
commit 68b2d79700
2 changed files with 8 additions and 12 deletions

View file

@ -1,8 +1,6 @@
package monitor package monitor
import ( import (
"bytes"
"github.com/yusing/go-proxy/internal/docker" "github.com/yusing/go-proxy/internal/docker"
"github.com/yusing/go-proxy/internal/net/types" "github.com/yusing/go-proxy/internal/net/types"
@ -36,16 +34,10 @@ func (mon *DockerHealthMonitor) CheckHealth() (result *health.HealthCheckResult,
} }
result = new(health.HealthCheckResult) result = new(health.HealthCheckResult)
result.Healthy = cont.State.Health.Status == dockerTypes.Healthy result.Healthy = cont.State.Health.Status == dockerTypes.Healthy
detail := new(bytes.Buffer)
for _, status := range cont.State.Health.Log {
detail.WriteString(status.Output)
detail.WriteString("\n")
}
result.Detail = detail.String()
if len(cont.State.Health.Log) > 0 { if len(cont.State.Health.Log) > 0 {
last := cont.State.Health.Log[len(cont.State.Health.Log)-1].End lastLog := cont.State.Health.Log[len(cont.State.Health.Log)-1]
first := cont.State.Health.Log[0].Start result.Detail = lastLog.Output
result.Latency = last.Sub(first) result.Latency = lastLog.End.Sub(lastLog.Start)
} }
return return
} }

View file

@ -28,6 +28,10 @@ func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) {
if url == "http://:0" { if url == "http://:0" {
url = "" url = ""
} }
lastSeen := "never"
if !jsonRepr.LastSeen.IsZero() {
lastSeen = strutils.FormatTime(jsonRepr.LastSeen)
}
return json.Marshal(map[string]any{ return json.Marshal(map[string]any{
"name": jsonRepr.Name, "name": jsonRepr.Name,
"config": jsonRepr.Config, "config": jsonRepr.Config,
@ -39,7 +43,7 @@ func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) {
"latency": jsonRepr.Latency.Seconds(), "latency": jsonRepr.Latency.Seconds(),
"latencyStr": strconv.Itoa(int(jsonRepr.Latency.Milliseconds())) + " ms", "latencyStr": strconv.Itoa(int(jsonRepr.Latency.Milliseconds())) + " ms",
"lastSeen": jsonRepr.LastSeen.Unix(), "lastSeen": jsonRepr.LastSeen.Unix(),
"lastSeenStr": strutils.FormatTime(jsonRepr.LastSeen), "lastSeenStr": lastSeen,
"detail": jsonRepr.Detail, "detail": jsonRepr.Detail,
"url": url, "url": url,
"extra": jsonRepr.Extra, "extra": jsonRepr.Extra,