client_golang icon indicating copy to clipboard operation
client_golang copied to clipboard

How to avoid duplicate metrics collector registration attempted in parallel tests

Open krasi-georgiev opened this issue 3 years ago • 0 comments

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()
}

krasi-georgiev avatar Sep 24 '22 08:09 krasi-georgiev