mirror of
https://github.com/nais/wonderwall.git
synced 2026-08-23 21:16:14 +00:00
fix(metrics): log collector registration failures
Registration errors were discarded, leaving the collector silently absent from the metrics endpoint while the vectors still recorded. Duplicate registrations remain ignored; they are expected when several instances are constructed in the same process.
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -131,6 +133,19 @@ func Register() {
|
||||
)
|
||||
}
|
||||
|
||||
func RegisterCollector(collector prometheus.Collector) {
|
||||
err := prometheus.DefaultRegisterer.Register(collector)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := errors.AsType[prometheus.AlreadyRegisteredError](err); ok {
|
||||
return
|
||||
}
|
||||
|
||||
log.Warnf("metrics: registering collector: %+v", err)
|
||||
}
|
||||
|
||||
func ObserveRedisLatency(operation string, fun func() error) error {
|
||||
timer := time.Now()
|
||||
err := fun()
|
||||
|
||||
@@ -56,8 +56,8 @@ func Prometheus(provider string, buckets ...float64) *PrometheusMiddleware {
|
||||
[]string{"code", "method", "path", "host"},
|
||||
)
|
||||
|
||||
prometheus.Register(m.reqs)
|
||||
prometheus.Register(m.latency)
|
||||
metrics.RegisterCollector(m.reqs)
|
||||
metrics.RegisterCollector(m.latency)
|
||||
|
||||
return &m
|
||||
}
|
||||
|
||||
@@ -5,12 +5,12 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/redis/go-redis/extra/redisotel/v9"
|
||||
"github.com/redis/go-redis/extra/redisprometheus/v9"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/nais/wonderwall/pkg/config"
|
||||
"github.com/nais/wonderwall/pkg/metrics"
|
||||
)
|
||||
|
||||
type Store interface {
|
||||
@@ -34,7 +34,7 @@ func NewStore(cfg *config.Config) (Store, error) {
|
||||
}
|
||||
|
||||
collector := redisprometheus.NewCollector("wonderwall", "", redisClient)
|
||||
prometheus.Register(collector)
|
||||
metrics.RegisterCollector(collector)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
||||
defer cancel()
|
||||
|
||||
Reference in New Issue
Block a user