Skip to content

Avoiding "duplicate metrics collector registration attempted" error while running parallel tests #1937

@vantassell

Description

@vantassell

Is it possible to use the built-in metrics from promauto while using a custom registry?

I'm trying to avoid duplicate metrics collector registration attempt panic when running tests in parallel by using the fix mentioned in #716, but I lose all the built-in metrics that promauto comes with (e.g. go_gc_duration_seconds). If I don't use a custom registry, my parallel tests panic due to duplicate registration attempts. I tried the fix mentioned here but i'm still getting a panic from duplicate registration :/

If my understanding is correct, the default global registry that promauto uses has a bunch of metrics pre-registered with it. I want to keep all of those pre-registered metrics while using a custom registry to avoid the above duplicate registration panic. Is there a way to have it both ways? Or am I missing a step?

Copy/Pasting the sample code for reference from #716 that uses custom registry to avoid duplicate registration panic (but loses built-in metrics)

reg := prometheus.NewRegistry() 
opsProcessed := promauto.With(reg).NewCounter(prometheus.CounterOpts{
		Namespace: "bigpigeon",
		Subsystem: "test",
		Name:      "xxx",
		Help:      "The total number of processed events",
	})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions