return T from GetOrRegister instead of any

This commit is contained in:
Ömer Faruk IRMAK 2025-06-05 18:33:39 +03:00
parent d6af162a15
commit 75b3e77eda
10 changed files with 18 additions and 18 deletions

View file

@ -7,7 +7,7 @@ import (
// GetOrRegisterCounter returns an existing Counter or constructs and registers
// a new Counter.
func GetOrRegisterCounter(name string, r Registry) *Counter {
return GetOrRegister(name, NewCounter, r).(*Counter)
return GetOrRegister(name, NewCounter, r)
}
// NewCounter constructs a new Counter.

View file

@ -8,7 +8,7 @@ import (
// GetOrRegisterCounterFloat64 returns an existing *CounterFloat64 or constructs and registers
// a new CounterFloat64.
func GetOrRegisterCounterFloat64(name string, r Registry) *CounterFloat64 {
return GetOrRegister(name, NewCounterFloat64, r).(*CounterFloat64)
return GetOrRegister(name, NewCounterFloat64, r)
}
// NewCounterFloat64 constructs a new CounterFloat64.

View file

@ -11,7 +11,7 @@ func (g GaugeSnapshot) Value() int64 { return int64(g) }
// GetOrRegisterGauge returns an existing Gauge or constructs and registers a
// new Gauge.
func GetOrRegisterGauge(name string, r Registry) *Gauge {
return GetOrRegister(name, NewGauge, r).(*Gauge)
return GetOrRegister(name, NewGauge, r)
}
// NewGauge constructs a new Gauge.

View file

@ -8,7 +8,7 @@ import (
// GetOrRegisterGaugeFloat64 returns an existing GaugeFloat64 or constructs and registers a
// new GaugeFloat64.
func GetOrRegisterGaugeFloat64(name string, r Registry) *GaugeFloat64 {
return GetOrRegister(name, NewGaugeFloat64, r).(*GaugeFloat64)
return GetOrRegister(name, NewGaugeFloat64, r)
}
// GaugeFloat64Snapshot is a read-only copy of a GaugeFloat64.

View file

@ -16,7 +16,7 @@ func (val GaugeInfoValue) String() string {
// GetOrRegisterGaugeInfo returns an existing GaugeInfo or constructs and registers a
// new GaugeInfo.
func GetOrRegisterGaugeInfo(name string, r Registry) *GaugeInfo {
return GetOrRegister(name, NewGaugeInfo, r).(*GaugeInfo)
return GetOrRegister(name, NewGaugeInfo, r)
}
// NewGaugeInfo constructs a new GaugeInfo.

View file

@ -12,7 +12,7 @@ import (
// Be sure to unregister the meter from the registry once it is of no use to
// allow for garbage collection.
func GetOrRegisterMeter(name string, r Registry) *Meter {
return GetOrRegister(name, NewMeter, r).(*Meter)
return GetOrRegister(name, NewMeter, r)
}
// NewMeter constructs a new Meter and launches a goroutine.

View file

@ -327,11 +327,11 @@ func Get(name string) interface{} {
// GetOrRegister gets an existing metric or creates and registers a new one. Threadsafe
// alternative to calling Get and Register on failure.
func GetOrRegister[T any](name string, ctor func() T, r Registry) interface{} {
func GetOrRegister[T any](name string, ctor func() T, r Registry) T {
if r == nil {
r = DefaultRegistry
}
return r.GetOrRegister(name, func() any { return ctor() })
return r.GetOrRegister(name, func() any { return ctor() }).(T)
}
// Register the given metric under the given name. Returns a ErrDuplicateMetric

View file

@ -98,10 +98,10 @@ func TestRegistryGetOrRegister(t *testing.T) {
r := NewRegistry()
// First metric wins with GetOrRegister
_ = GetOrRegister("foo", NewCounter, r)
m := GetOrRegister("foo", NewGauge, r)
if _, ok := m.(*Counter); !ok {
t.Fatal(m)
c1 := GetOrRegister("foo", NewCounter, r)
c2 := GetOrRegister("foo", NewCounter, r)
if c1 != c2 {
t.Fatal("counters should've matched")
}
i := 0
@ -123,10 +123,10 @@ func TestRegistryGetOrRegisterWithLazyInstantiation(t *testing.T) {
r := NewRegistry()
// First metric wins with GetOrRegister
_ = GetOrRegister("foo", NewCounter, r)
m := GetOrRegister("foo", NewGauge, r)
if _, ok := m.(*Counter); !ok {
t.Fatal(m)
c1 := GetOrRegister("foo", NewCounter, r)
c2 := GetOrRegister("foo", NewCounter, r)
if c1 != c2 {
t.Fatal("counters should've matched")
}
i := 0

View file

@ -8,7 +8,7 @@ import (
// GetOrRegisterResettingTimer returns an existing ResettingTimer or constructs and registers a
// new ResettingTimer.
func GetOrRegisterResettingTimer(name string, r Registry) *ResettingTimer {
return GetOrRegister(name, NewResettingTimer, r).(*ResettingTimer)
return GetOrRegister(name, NewResettingTimer, r)
}
// NewRegisteredResettingTimer constructs and registers a new ResettingTimer.

View file

@ -10,7 +10,7 @@ import (
// Be sure to unregister the meter from the registry once it is of no use to
// allow for garbage collection.
func GetOrRegisterTimer(name string, r Registry) *Timer {
return GetOrRegister(name, NewTimer, r).(*Timer)
return GetOrRegister(name, NewTimer, r)
}
// NewCustomTimer constructs a new Timer from a Histogram and a Meter.