Skip to content

Commit

Permalink
reset metric registry when creating a new cluster to have metrics reg…
Browse files Browse the repository at this point in the history
…istered just for the running test
  • Loading branch information
sandeepsukhani committed May 17, 2023
1 parent 4148dd2 commit 18c4fe5
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions integration/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import (
)

var (
wrapRegistryOnce sync.Once

configTemplate = template.Must(template.New("").Parse(`
auth_enabled: true
Expand Down Expand Up @@ -108,18 +106,18 @@ ruler:
`))
)

func wrapRegistry() {
wrapRegistryOnce.Do(func() {
prometheus.DefaultRegisterer = &wrappedRegisterer{Registerer: prometheus.DefaultRegisterer}
})
func resetMetricRegistry() {
registry := &wrappedRegisterer{Registry: prometheus.NewRegistry()}
prometheus.DefaultRegisterer = registry
prometheus.DefaultGatherer = registry
}

type wrappedRegisterer struct {
prometheus.Registerer
*prometheus.Registry
}

func (w *wrappedRegisterer) Register(collector prometheus.Collector) error {
if err := w.Registerer.Register(collector); err != nil {
if err := w.Registry.Register(collector); err != nil {
var aErr prometheus.AlreadyRegisteredError
if errors.As(err, &aErr) {
return nil
Expand Down Expand Up @@ -151,7 +149,7 @@ func New(logLevel level.Value, opts ...func(*Cluster)) *Cluster {
util_log.Logger = level.NewFilter(log.NewLogfmtLogger(os.Stderr), level.Allow(logLevel))
}

wrapRegistry()
resetMetricRegistry()
sharedPath, err := os.MkdirTemp("", "loki-shared-data")
if err != nil {
panic(err.Error())
Expand Down

0 comments on commit 18c4fe5

Please sign in to comment.