metric unregistration on route removal, fixed multi-ips as visitor label detected from x headers

This commit is contained in:
yusing
2024-11-10 06:47:59 +08:00
parent a1d1325ad6
commit 6194bac4c4
7 changed files with 100 additions and 48 deletions

View File

@@ -7,6 +7,7 @@ import (
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusing/go-proxy/internal/common"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/logging"
@@ -28,6 +29,8 @@ type (
checkHealth HealthCheckFunc
startTime time.Time
metric *metrics.Gauge
task task.Task
}
)
@@ -61,6 +64,10 @@ func (mon *monitor) Start(routeSubtask task.Task) E.Error {
return E.From(ErrNegativeInterval)
}
if common.PrometheusEnabled {
mon.metric = metrics.GetServiceMetrics().HealthStatus.With(metrics.HealthMetricLabels(mon.service))
}
go func() {
logger := logging.With().Str("name", mon.service).Logger()
@@ -69,6 +76,9 @@ func (mon *monitor) Start(routeSubtask task.Task) E.Error {
mon.status.Store(StatusUnknown)
}
mon.task.Finish(nil)
if mon.metric != nil {
prometheus.Unregister(mon.metric)
}
}()
if err := mon.checkUpdateHealth(); err != nil {
@@ -175,15 +185,8 @@ func (mon *monitor) checkUpdateHealth() error {
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)
}()
if mon.metric != nil {
mon.metric.Set(float64(status))
}
return nil