Skip to content

Commit

Permalink
fix healthcheck last seen
Browse files Browse the repository at this point in the history
  • Loading branch information
yusing committed Jan 3, 2025
1 parent 3ecc0f9 commit 7e60d18
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
22 changes: 22 additions & 0 deletions internal/watcher/health/monitor/last_seen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package monitor

import (
"time"

F "github.com/yusing/go-proxy/internal/utils/functional"
)

var lastSeenMap = F.NewMapOf[string, time.Time]()

func SetLastSeen(service string, lastSeen time.Time) {
lastSeenMap.Store(service, lastSeen)
}

func UpdateLastSeen(service string) {
SetLastSeen(service, time.Now())
}

func GetLastSeen(service string) time.Time {
lastSeen, _ := lastSeenMap.Load(service)
return lastSeen
}
10 changes: 6 additions & 4 deletions internal/watcher/health/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type (

status atomic.Value[health.Status]
lastResult *health.HealthCheckResult
lastSeen time.Time

checkHealth HealthCheckFunc
startTime time.Time
Expand Down Expand Up @@ -164,7 +163,7 @@ func (mon *monitor) MarshalJSON() ([]byte, error) {
Started: mon.startTime,
Uptime: mon.Uptime(),
Latency: res.Latency,
LastSeen: mon.lastSeen,
LastSeen: GetLastSeen(mon.service),
Detail: res.Detail,
URL: mon.url.Load(),
}).MarshalJSON()
Expand All @@ -186,14 +185,17 @@ func (mon *monitor) checkUpdateHealth() error {
var status health.Status
if result.Healthy {
status = health.StatusHealthy
mon.lastSeen = time.Now()
UpdateLastSeen(mon.service)
} else {
status = health.StatusUnhealthy
}
if result.Healthy != (mon.status.Swap(status) == health.StatusHealthy) {
extras := map[string]any{
"Service Name": mon.service,
"Last Seen": strutils.FormatLastSeen(mon.lastSeen),
"Time": strutils.FormatTime(time.Now()),
}
if !result.Healthy {
extras["Last Seen"] = strutils.FormatLastSeen(GetLastSeen(mon.service))
}
if !mon.url.Load().Nil() {
extras["Service URL"] = mon.url.Load().String()
Expand Down

0 comments on commit 7e60d18

Please sign in to comment.