diff --git a/internal/idlewatcher/debug.go b/internal/idlewatcher/debug.go index a8aa7d3f..527d3fcd 100644 --- a/internal/idlewatcher/debug.go +++ b/internal/idlewatcher/debug.go @@ -1,6 +1,7 @@ package idlewatcher import ( + "encoding/json" "iter" "strconv" @@ -11,9 +12,9 @@ type watcherDebug struct { *Watcher } -func (w watcherDebug) MarshalMap() map[string]any { +func (w watcherDebug) MarshalJSON() ([]byte, error) { state := w.state.Load() - return map[string]any{ + return json.Marshal(map[string]any{ "name": w.Name(), "state": map[string]string{ "status": string(state.status), @@ -23,7 +24,7 @@ func (w watcherDebug) MarshalMap() map[string]any { "expires": strutils.FormatTime(w.expires()), "last_reset": strutils.FormatTime(w.lastReset.Load()), "config": w.cfg, - } + }) } func Watchers() iter.Seq2[string, watcherDebug] { diff --git a/internal/idlewatcher/health.go b/internal/idlewatcher/health.go index ec305f71..5a256a49 100644 --- a/internal/idlewatcher/health.go +++ b/internal/idlewatcher/health.go @@ -102,8 +102,8 @@ func checkUpdateState(key string) (w *Watcher, ready bool, err error) { return w, false, nil } -// MarshalMap implements health.HealthMonitor. -func (w *Watcher) MarshalMap() map[string]any { +// MarshalJSON implements health.HealthMonitor. +func (w *Watcher) MarshalJSON() ([]byte, error) { url := w.hc.URL() if url.Port() == "0" { url = nil @@ -118,5 +118,5 @@ func (w *Watcher) MarshalMap() map[string]any { Config: dummyHealthCheckConfig, URL: url, Detail: detail, - }).MarshalMap() + }).MarshalJSON() } diff --git a/internal/net/gphttp/loadbalancer/loadbalancer.go b/internal/net/gphttp/loadbalancer/loadbalancer.go index 7487b89c..ce718dd1 100644 --- a/internal/net/gphttp/loadbalancer/loadbalancer.go +++ b/internal/net/gphttp/loadbalancer/loadbalancer.go @@ -237,8 +237,8 @@ func (lb *LoadBalancer) ServeHTTP(rw http.ResponseWriter, r *http.Request) { lb.impl.ServeHTTP(srvs, rw, r) } -// MarshalMap implements health.HealthMonitor. -func (lb *LoadBalancer) MarshalMap() map[string]any { +// MarshalJSON implements health.HealthMonitor. +func (lb *LoadBalancer) MarshalJSON() ([]byte, error) { extra := make(map[string]any) for _, srv := range lb.pool.Iter { extra[srv.Key()] = srv @@ -252,11 +252,12 @@ func (lb *LoadBalancer) MarshalMap() map[string]any { Detail: fmt.Sprintf("%d/%d servers are healthy", numHealthy, lb.pool.Size()), Started: lb.startTime, Uptime: lb.Uptime(), + Latency: lb.Latency(), Extra: map[string]any{ "config": lb.Config, "pool": extra, }, - }).MarshalMap() + }).MarshalJSON() } // Name implements health.HealthMonitor. diff --git a/internal/proxmox/client.go b/internal/proxmox/client.go index d218e011..bccaafd2 100644 --- a/internal/proxmox/client.go +++ b/internal/proxmox/client.go @@ -2,6 +2,7 @@ package proxmox import ( "context" + "encoding/json" "fmt" "github.com/luthermonson/go-proxmox" @@ -45,9 +46,8 @@ func (c *Client) Name() string { return c.Cluster.Name } -// MarshalMap implements pool.Object -func (c *Client) MarshalMap() map[string]any { - return map[string]any{ +func (c *Client) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]any{ "version": c.Version, "cluster": map[string]any{ "name": c.Cluster.Name, @@ -56,7 +56,7 @@ func (c *Client) MarshalMap() map[string]any { "nodes": c.Cluster.Nodes, "quorate": c.Cluster.Quorate, }, - } + }) } func (c *Client) NumNodes() int { diff --git a/internal/proxmox/node.go b/internal/proxmox/node.go index 5ecd9068..6fc45ba2 100644 --- a/internal/proxmox/node.go +++ b/internal/proxmox/node.go @@ -2,6 +2,7 @@ package proxmox import ( "context" + "encoding/json" "fmt" "strings" @@ -38,11 +39,11 @@ func (n *Node) String() string { return fmt.Sprintf("%s (%s)", n.name, n.id) } -func (n *Node) MarshalMap() map[string]any { - return map[string]any{ +func (n *Node) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]any{ "name": n.name, "id": n.id, - } + }) } func (n *Node) Get(ctx context.Context, path string, v any) error { diff --git a/internal/watcher/health/json.go b/internal/watcher/health/json.go index 9059f5c4..b8b10dea 100644 --- a/internal/watcher/health/json.go +++ b/internal/watcher/health/json.go @@ -1,6 +1,7 @@ package health import ( + "encoding/json" "net/url" "strconv" "time" @@ -21,7 +22,7 @@ type JSONRepresentation struct { Extra map[string]any } -func (jsonRepr *JSONRepresentation) MarshalMap() map[string]any { +func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) { var url string if jsonRepr.URL != nil { url = jsonRepr.URL.String() @@ -29,7 +30,7 @@ func (jsonRepr *JSONRepresentation) MarshalMap() map[string]any { if url == "http://:0" { url = "" } - return map[string]any{ + return json.Marshal(map[string]any{ "name": jsonRepr.Name, "config": jsonRepr.Config, "started": jsonRepr.Started.Unix(), @@ -44,5 +45,5 @@ func (jsonRepr *JSONRepresentation) MarshalMap() map[string]any { "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 1aa2b9d5..2eded602 100644 --- a/internal/watcher/health/monitor/monitor.go +++ b/internal/watcher/health/monitor/monitor.go @@ -179,8 +179,8 @@ func (mon *monitor) String() string { return mon.Name() } -// MarshalMap implements health.HealthMonitor. -func (mon *monitor) MarshalMap() map[string]any { +// MarshalJSON implements health.HealthMonitor. +func (mon *monitor) MarshalJSON() ([]byte, error) { res := mon.lastResult.Load() if res == nil { res = &health.HealthCheckResult{ @@ -198,7 +198,7 @@ func (mon *monitor) MarshalMap() map[string]any { LastSeen: GetLastSeen(mon.service), Detail: res.Detail, URL: mon.url.Load(), - }).MarshalMap() + }).MarshalJSON() } func (mon *monitor) checkUpdateHealth() error { diff --git a/internal/watcher/health/types.go b/internal/watcher/health/types.go index fe89533f..0f6610e7 100644 --- a/internal/watcher/health/types.go +++ b/internal/watcher/health/types.go @@ -1,6 +1,7 @@ package health import ( + "encoding/json" "fmt" "net/url" "time" @@ -25,6 +26,7 @@ type ( fmt.Stringer WithHealthInfo Name() string + json.Marshaler } HealthChecker interface { CheckHealth() (result *HealthCheckResult, err error)