uptime metrics

This commit is contained in:
yusing 2024-11-07 11:44:01 +08:00
parent 6be3aef367
commit 5214ae1760
2 changed files with 39 additions and 2 deletions

View file

@ -14,10 +14,15 @@ type (
HTTP4xx, HTTP4xx,
HTTP5xx *Counter HTTP5xx *Counter
HTTPReqElapsed *Gauge HTTPReqElapsed *Gauge
HealthStatus *Gauge
} }
HTTPRouteMetricLabels struct { HTTPRouteMetricLabels struct {
Service, Method, Host, Visitor, Path string Service, Method, Host, Visitor, Path string
} }
StreamRouteMetricLabels struct {
Service, Visitor string
}
HealthMetricLabels string
) )
var rm RouteMetrics var rm RouteMetrics
@ -25,13 +30,15 @@ var rm RouteMetrics
const ( const (
routerNamespace = "router" routerNamespace = "router"
routerHTTPSubsystem = "http" routerHTTPSubsystem = "http"
serviceNamespace = "service"
) )
func GetRouteMetrics() *RouteMetrics { func GetRouteMetrics() *RouteMetrics {
return &rm return &rm
} }
func (lbl HTTPRouteMetricLabels) toPromLabels() prometheus.Labels { func (lbl *HTTPRouteMetricLabels) toPromLabels() prometheus.Labels {
return prometheus.Labels{ return prometheus.Labels{
"service": lbl.Service, "service": lbl.Service,
"method": lbl.Method, "method": lbl.Method,
@ -41,6 +48,19 @@ func (lbl HTTPRouteMetricLabels) toPromLabels() prometheus.Labels {
} }
} }
func (lbl *StreamRouteMetricLabels) toPromLabels() prometheus.Labels {
return prometheus.Labels{
"service": lbl.Service,
"visitor": lbl.Visitor,
}
}
func (lbl HealthMetricLabels) toPromLabels() prometheus.Labels {
return prometheus.Labels{
"service": string(lbl),
}
}
func init() { func init() {
if !common.PrometheusEnabled { if !common.PrometheusEnabled {
return return
@ -76,7 +96,12 @@ func init() {
Namespace: routerNamespace, Namespace: routerNamespace,
Subsystem: routerHTTPSubsystem, Subsystem: routerHTTPSubsystem,
Name: "req_elapsed_ms", Name: "req_elapsed_ms",
Help: "How long it took to process the request" + partitionsHelp, Help: "How long it took to process the request and respond a status code" + partitionsHelp,
}, lbls...), }, lbls...),
HealthStatus: NewGauge(prometheus.GaugeOpts{
Namespace: serviceNamespace,
Name: "health_status",
Help: "The health status of the router by service",
}, "service"),
} }
} }

View file

@ -7,8 +7,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/yusing/go-proxy/internal/common"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/metrics"
"github.com/yusing/go-proxy/internal/net/types" "github.com/yusing/go-proxy/internal/net/types"
"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"
@ -172,6 +174,16 @@ func (mon *monitor) checkUpdateHealth() error {
logger.Debug().Msg(detail) logger.Debug().Msg(detail)
notif.Notify(mon.service, "server is down") notif.Notify(mon.service, "server is down")
} }
if common.PrometheusEnabled {
go func() {
m := metrics.GetRouteMetrics()
var up float64
if healthy {
up = 1
}
m.HealthStatus.With(metrics.HealthMetricLabels(mon.service)).Set(up)
}()
}
} }
return nil return nil