client_golang
client_golang copied to clipboard
How to avoid duplicate metrics collector registration attempted in parallel tests
The code below panics for duplicate registration because by default golang runs tests in parallel.
The actual code would work since New() is called only once so wondering if there could be any workaround that can use for the testing.
I really want to avoid having to use a custom register.
type TT struct {
a prometheus.Gauge
}
func New() *TT {
return &TT{
a: promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "a",
Subsystem: "a",
Name: "a",
Help: "a",
}),
}
}
func Test1(t *testing.T) {
New()
}
func Test2(t *testing.T) {
New()
}