mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-09 04:52:35 +02:00
enrich health check result details
This commit is contained in:
parent
be81415a75
commit
17e8532e6f
2 changed files with 52 additions and 28 deletions
|
@ -2,6 +2,7 @@ package monitor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
"github.com/yusing/go-proxy/internal/net/types"
|
||||||
|
@ -15,6 +16,9 @@ type JSONRepresentation struct {
|
||||||
Status health.Status
|
Status health.Status
|
||||||
Started time.Time
|
Started time.Time
|
||||||
Uptime time.Duration
|
Uptime time.Duration
|
||||||
|
Latency time.Duration
|
||||||
|
LastSeen time.Time
|
||||||
|
Detail string
|
||||||
URL types.URL
|
URL types.URL
|
||||||
Extra map[string]any
|
Extra map[string]any
|
||||||
}
|
}
|
||||||
|
@ -32,6 +36,11 @@ func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) {
|
||||||
"status": jsonRepr.Status.String(),
|
"status": jsonRepr.Status.String(),
|
||||||
"uptime": jsonRepr.Uptime.Seconds(),
|
"uptime": jsonRepr.Uptime.Seconds(),
|
||||||
"uptimeStr": strutils.FormatDuration(jsonRepr.Uptime),
|
"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,
|
"url": url,
|
||||||
"extra": jsonRepr.Extra,
|
"extra": jsonRepr.Extra,
|
||||||
})
|
})
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"github.com/yusing/go-proxy/internal/notif"
|
"github.com/yusing/go-proxy/internal/notif"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
U "github.com/yusing/go-proxy/internal/utils"
|
U "github.com/yusing/go-proxy/internal/utils"
|
||||||
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,6 +28,9 @@ type (
|
||||||
url U.AtomicValue[types.URL]
|
url U.AtomicValue[types.URL]
|
||||||
|
|
||||||
status U.AtomicValue[health.Status]
|
status U.AtomicValue[health.Status]
|
||||||
|
lastResult *health.HealthCheckResult
|
||||||
|
lastSeen time.Time
|
||||||
|
|
||||||
checkHealth HealthCheckFunc
|
checkHealth HealthCheckFunc
|
||||||
startTime time.Time
|
startTime time.Time
|
||||||
|
|
||||||
|
@ -149,12 +153,16 @@ func (mon *monitor) String() string {
|
||||||
|
|
||||||
// MarshalJSON implements json.Marshaler of HealthMonitor.
|
// MarshalJSON implements json.Marshaler of HealthMonitor.
|
||||||
func (mon *monitor) MarshalJSON() ([]byte, error) {
|
func (mon *monitor) MarshalJSON() ([]byte, error) {
|
||||||
|
res := mon.lastResult
|
||||||
return (&JSONRepresentation{
|
return (&JSONRepresentation{
|
||||||
Name: mon.service,
|
Name: mon.service,
|
||||||
Config: mon.config,
|
Config: mon.config,
|
||||||
Status: mon.status.Load(),
|
Status: mon.status.Load(),
|
||||||
Started: mon.startTime,
|
Started: mon.startTime,
|
||||||
Uptime: mon.Uptime(),
|
Uptime: mon.Uptime(),
|
||||||
|
Latency: res.Latency,
|
||||||
|
LastSeen: mon.lastSeen,
|
||||||
|
Detail: res.Detail,
|
||||||
URL: mon.url.Load(),
|
URL: mon.url.Load(),
|
||||||
}).MarshalJSON()
|
}).MarshalJSON()
|
||||||
}
|
}
|
||||||
|
@ -170,19 +178,28 @@ func (mon *monitor) checkUpdateHealth() error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mon.lastResult = result
|
||||||
var status health.Status
|
var status health.Status
|
||||||
if result.Healthy {
|
if result.Healthy {
|
||||||
status = health.StatusHealthy
|
status = health.StatusHealthy
|
||||||
|
mon.lastSeen = time.Now()
|
||||||
} else {
|
} else {
|
||||||
status = health.StatusUnhealthy
|
status = health.StatusUnhealthy
|
||||||
}
|
}
|
||||||
if result.Healthy != (mon.status.Swap(status) == health.StatusHealthy) {
|
if result.Healthy != (mon.status.Swap(status) == health.StatusHealthy) {
|
||||||
extras := map[string]any{
|
extras := map[string]any{
|
||||||
"Service Name": mon.service,
|
"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 {
|
if result.Healthy {
|
||||||
logger.Info().Msg("server is up")
|
logger.Info().Msg("service is up")
|
||||||
extras["Ping"] = fmt.Sprintf("%d ms", result.Latency.Milliseconds())
|
extras["Ping"] = fmt.Sprintf("%d ms", result.Latency.Milliseconds())
|
||||||
notif.Notify(¬if.LogMessage{
|
notif.Notify(¬if.LogMessage{
|
||||||
Title: "✅ Service is up ✅",
|
Title: "✅ Service is up ✅",
|
||||||
|
@ -190,9 +207,7 @@ func (mon *monitor) checkUpdateHealth() error {
|
||||||
Color: notif.Green,
|
Color: notif.Green,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
logger.Warn().Msg("server is down")
|
logger.Warn().Msg("service went down")
|
||||||
logger.Debug().Msg(result.Detail)
|
|
||||||
extras["Detail"] = result.Detail
|
|
||||||
notif.Notify(¬if.LogMessage{
|
notif.Notify(¬if.LogMessage{
|
||||||
Title: "❌ Service went down ❌",
|
Title: "❌ Service went down ❌",
|
||||||
Extras: extras,
|
Extras: extras,
|
||||||
|
|
Loading…
Add table
Reference in a new issue