Files
coreth/core/blockchain_ext.go
2025-12-13 00:01:08 -08:00

30 lines
1.1 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package core
import "github.com/luxfi/geth/metrics"
// getOrOverrideAsRegisteredCounter searches for a metric already registered
// with `name`. If a metric is found and it is a [*metrics.Counter], it is returned. If a
// metric is found and it is not a [*metrics.Counter], it is unregistered and replaced with
// a new registered [*metrics.Counter]. If no metric is found, a new [*metrics.Counter] is constructed
// and registered.
//
// This is necessary for a metric defined in geth with the same name but a
// different type to what we expect.
func getOrOverrideAsRegisteredCounter(name string, r metrics.Registry) *metrics.Counter {
if r == nil {
r = metrics.DefaultRegistry
}
// Try to get existing metric first
if existing := r.Get(name); existing != nil {
if c, ok := existing.(*metrics.Counter); ok {
return c
}
// `name` was already registered with a different type, unregister it
r.Unregister(name)
}
return metrics.NewRegisteredCounter(name, r)
}