From 17e8532e6f2a1357be92f0fca1f2723f46038869 Mon Sep 17 00:00:00 2001 From: yusing Date: Fri, 13 Dec 2024 12:35:59 +0800 Subject: [PATCH] enrich health check result details --- internal/watcher/health/monitor/json.go | 41 +++++++++++++--------- internal/watcher/health/monitor/monitor.go | 39 +++++++++++++------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/internal/watcher/health/monitor/json.go b/internal/watcher/health/monitor/json.go index cce8c15..2bcb21e 100644 --- a/internal/watcher/health/monitor/json.go +++ b/internal/watcher/health/monitor/json.go @@ -2,6 +2,7 @@ package monitor import ( "encoding/json" + "strconv" "time" "github.com/yusing/go-proxy/internal/net/types" @@ -10,13 +11,16 @@ import ( ) type JSONRepresentation struct { - Name string - Config *health.HealthCheckConfig - Status health.Status - Started time.Time - Uptime time.Duration - URL types.URL - Extra map[string]any + Name string + Config *health.HealthCheckConfig + Status health.Status + Started time.Time + Uptime time.Duration + Latency time.Duration + LastSeen time.Time + Detail string + URL types.URL + Extra map[string]any } func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) { @@ -25,14 +29,19 @@ func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) { url = "" } return json.Marshal(map[string]any{ - "name": jsonRepr.Name, - "config": jsonRepr.Config, - "started": jsonRepr.Started.Unix(), - "startedStr": strutils.FormatTime(jsonRepr.Started), - "status": jsonRepr.Status.String(), - "uptime": jsonRepr.Uptime.Seconds(), - "uptimeStr": strutils.FormatDuration(jsonRepr.Uptime), - "url": url, - "extra": jsonRepr.Extra, + "name": jsonRepr.Name, + "config": jsonRepr.Config, + "started": jsonRepr.Started.Unix(), + "startedStr": strutils.FormatTime(jsonRepr.Started), + "status": jsonRepr.Status.String(), + "uptime": jsonRepr.Uptime.Seconds(), + "uptimeStr": strutils.FormatDuration(jsonRepr.Uptime), + "latency": jsonRepr.Latency.Seconds(), + "latencyStr": strconv.Itoa(int(jsonRepr.Latency.Milliseconds())) + " ms", + "lastSeen": jsonRepr.LastSeen.Unix(), + "lastSeenStr": strutils.FormatTime(jsonRepr.LastSeen), + "detail": jsonRepr.Detail, + "url": url, + "extra": jsonRepr.Extra, }) } diff --git a/internal/watcher/health/monitor/monitor.go b/internal/watcher/health/monitor/monitor.go index 3ead713..ef4250f 100644 --- a/internal/watcher/health/monitor/monitor.go +++ b/internal/watcher/health/monitor/monitor.go @@ -16,6 +16,7 @@ import ( "github.com/yusing/go-proxy/internal/notif" "github.com/yusing/go-proxy/internal/task" U "github.com/yusing/go-proxy/internal/utils" + "github.com/yusing/go-proxy/internal/utils/strutils" "github.com/yusing/go-proxy/internal/watcher/health" ) @@ -26,7 +27,10 @@ type ( config *health.HealthCheckConfig url U.AtomicValue[types.URL] - status U.AtomicValue[health.Status] + status U.AtomicValue[health.Status] + lastResult *health.HealthCheckResult + lastSeen time.Time + checkHealth HealthCheckFunc startTime time.Time @@ -149,13 +153,17 @@ func (mon *monitor) String() string { // MarshalJSON implements json.Marshaler of HealthMonitor. func (mon *monitor) MarshalJSON() ([]byte, error) { + res := mon.lastResult return (&JSONRepresentation{ - Name: mon.service, - Config: mon.config, - Status: mon.status.Load(), - Started: mon.startTime, - Uptime: mon.Uptime(), - URL: mon.url.Load(), + Name: mon.service, + Config: mon.config, + Status: mon.status.Load(), + Started: mon.startTime, + Uptime: mon.Uptime(), + Latency: res.Latency, + LastSeen: mon.lastSeen, + Detail: res.Detail, + URL: mon.url.Load(), }).MarshalJSON() } @@ -170,19 +178,28 @@ func (mon *monitor) checkUpdateHealth() error { } return nil } + + mon.lastResult = result var status health.Status if result.Healthy { status = health.StatusHealthy + mon.lastSeen = time.Now() } else { status = health.StatusUnhealthy } if result.Healthy != (mon.status.Swap(status) == health.StatusHealthy) { extras := map[string]any{ "Service Name": mon.service, - "Service URL": mon.url.Load().String(), + "Last Seen": strutils.FormatTime(mon.lastSeen), + } + if !mon.url.Load().Nil() { + extras["Service URL"] = mon.url.Load().String() + } + if result.Detail != "" { + extras["Detail"] = result.Detail } if result.Healthy { - logger.Info().Msg("server is up") + logger.Info().Msg("service is up") extras["Ping"] = fmt.Sprintf("%d ms", result.Latency.Milliseconds()) notif.Notify(¬if.LogMessage{ Title: "✅ Service is up ✅", @@ -190,9 +207,7 @@ func (mon *monitor) checkUpdateHealth() error { Color: notif.Green, }) } else { - logger.Warn().Msg("server is down") - logger.Debug().Msg(result.Detail) - extras["Detail"] = result.Detail + logger.Warn().Msg("service went down") notif.Notify(¬if.LogMessage{ Title: "❌ Service went down ❌", Extras: extras,