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:
Trong Huu Nguyen
2026-07-28 09:05:04 +02:00
parent c56625d842
commit ea20e96f87
3 changed files with 19 additions and 4 deletions
+15
View File
@@ -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()
+2 -2
View File
@@ -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
}
+2 -2
View File
@@ -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()