mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
fix: uptime metrics
This commit is contained in:
parent
af8d2c74f6
commit
3947152336
3 changed files with 47 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Add table
Reference in a new issue