Files
Trong Huu Nguyen cb14d1624f fix(metrics): record on the collector that is actually registered
When an equal collector was already registered, the caller kept using
its own instance, whose observations are never scraped. RegisterCollector
now returns the registered collector so callers can adopt it.
2026-07-28 12:52:45 +02:00

29 lines
883 B
Go

package metrics_test
import (
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/nais/wonderwall/pkg/metrics"
)
func TestRegisterCollector(t *testing.T) {
newCounter := func() *prometheus.CounterVec {
return prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "wonderwall_test_register_collector_total",
Help: "counter used to assert registration behaviour",
}, []string{"label"})
}
first := newCounter()
assert.Same(t, first, metrics.RegisterCollector(first), "the first registration is used as-is")
// an equal collector is already registered, so observations on the second one would
// never be scraped; the registered one must be returned instead
second := newCounter()
assert.Same(t, first, metrics.RegisterCollector(second))
assert.NotSame(t, second, metrics.RegisterCollector(second))
}