fix: uptime metrics

This commit is contained in:
yusing 2025-04-25 11:26:24 +08:00
parent af8d2c74f6
commit 3947152336
3 changed files with 47 additions and 0 deletions

View file

@ -37,6 +37,14 @@ func getStatuses(ctx context.Context, _ *StatusByAlias) (*StatusByAlias, error)
}, nil
}
func (s *Status) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"status": s.Status.String(),
"latency": s.Latency,
"timestamp": s.Timestamp,
})
}
func aggregateStatuses(entries []*StatusByAlias, query url.Values) (int, Aggregated) {
limit := metricsutils.QueryInt(query, "limit", 0)
offset := metricsutils.QueryInt(query, "offset", 0)

View file

@ -1,6 +1,7 @@
package routes
import (
"encoding/json"
"time"
"github.com/yusing/go-proxy/internal/homepage"
@ -29,6 +30,27 @@ type HealthInfoRaw struct {
Latency time.Duration `json:"latency"`
}
func (info *HealthInfoRaw) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"status": info.Status.String(),
"latency": info.Latency.Milliseconds(),
})
}
func (info *HealthInfoRaw) UnmarshalJSON(data []byte) error {
var v map[string]any
if err := json.Unmarshal(data, &v); err != nil {
return err
}
if status, ok := v["status"].(string); ok {
info.Status = health.NewStatus(status)
}
if latency, ok := v["latency"].(float64); ok {
info.Latency = time.Duration(latency)
}
return nil
}
func getHealthInfoRaw(r Route) *HealthInfoRaw {
mon := r.HealthMonitor()
if mon == nil {

View file

@ -16,6 +16,23 @@ const (
IdlingMask = StatusNapping | StatusStarting
)
func NewStatus(s string) Status {
switch s {
case "healthy":
return StatusHealthy
case "unhealthy":
return StatusUnhealthy
case "napping":
return StatusNapping
case "starting":
return StatusStarting
case "error":
return StatusError
default:
return StatusUnknown
}
}
func (s Status) String() string {
switch s {
case StatusHealthy: