|  | 
|  | 1 | +// Copyright 2026 the libevm authors. | 
|  | 2 | +// | 
|  | 3 | +// The libevm additions to go-ethereum are free software: you can redistribute | 
|  | 4 | +// them and/or modify them under the terms of the GNU Lesser General Public License | 
|  | 5 | +// as published by the Free Software Foundation, either version 3 of the License, | 
|  | 6 | +// or (at your option) any later version. | 
|  | 7 | +// | 
|  | 8 | +// The libevm additions are distributed in the hope that they will be useful, | 
|  | 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | 
|  | 11 | +// General Public License for more details. | 
|  | 12 | +// | 
|  | 13 | +// You should have received a copy of the GNU Lesser General Public License | 
|  | 14 | +// along with the go-ethereum library. If not, see | 
|  | 15 | +// <http://www.gnu.org/licenses/>. | 
|  | 16 | + | 
|  | 17 | +package metrics | 
|  | 18 | + | 
|  | 19 | +// GetOrRegisterOrOverrideCounter searches for a metric already registered | 
|  | 20 | +// with the `name` given. If a metric is found and it is a [Counter], this one | 
|  | 21 | +// is returned. If a metric is found and it is not a [Counter], it is unregistered | 
|  | 22 | +// and replaced with a new registered [Counter]. If no metric is found, a new | 
|  | 23 | +// [Counter] is constructed and registered. | 
|  | 24 | +// This is especially useful for a metric defined in this project with a different | 
|  | 25 | +// type than a metric defined in a consumer of this project, for the same name. | 
|  | 26 | +func GetOrRegisterOrOverrideCounter(name string, r Registry) Counter { | 
|  | 27 | +	if r == nil { | 
|  | 28 | +		r = DefaultRegistry | 
|  | 29 | +	} | 
|  | 30 | +	counter, ok := r.GetOrRegister(name, NewCounter).(Counter) | 
|  | 31 | +	if ok { | 
|  | 32 | +		return counter | 
|  | 33 | +	} | 
|  | 34 | +	r.Unregister(name) | 
|  | 35 | +	counter = NewCounter() | 
|  | 36 | +	_ = r.Register(name, counter) | 
|  | 37 | +	return counter | 
|  | 38 | +} | 
0 commit comments