simplify SingleLabeled metrics: just accept a string when registering a metric

Signed-off-by: Eugene <eugene@amberpixels.io>
This commit is contained in:
Eugene 2024-09-20 00:19:09 +03:00
parent 80149ab4d4
commit 6724abaa0b
No known key found for this signature in database
GPG Key ID: 51AC89611A689305
2 changed files with 2 additions and 22 deletions

View File

@ -163,28 +163,8 @@ func NewCounterFuncT(opts prometheus.CounterOpts, function func() float64) prome
// Shorthand for Metrics with a single label
//
// singleLabelProviderMarker is a marker interface for enforcing type-safety of SingleLabelProvider.
type singleLabelProviderMarker interface {
singleLabelProviderMarker()
}
// SingleLabelProvider is a type used for declaring a single label only.
// When declaring a metric it's values used as a label name
// When calling With() it's values used as a label value
type SingleLabelProvider string
var _ singleLabelProviderMarker = SingleLabelProvider("")
func (s SingleLabelProvider) singleLabelProviderMarker() {
panic("singleLabelProviderMarker interface method should never be called")
}
// NewCounterVecT1 creates a new CounterVecT with the only single label
func NewCounterVecT1(opts prometheus.CounterOpts, singleLabelProvider singleLabelProviderMarker) *CounterVecT1 {
// labelName is the string itself
// and singleLabelProviderMarker here can ONLY be SingleLabelProvider
labelName := string(singleLabelProvider.(SingleLabelProvider))
func NewCounterVecT1(opts prometheus.CounterOpts, labelName string) *CounterVecT1 {
var inner *prometheus.CounterVec
if factory != nil {
inner = factory.NewCounterVec(opts, []string{labelName})

View File

@ -124,7 +124,7 @@ func ExampleNewCounterVecT_single_label_manual() {
c := promsafe.NewCounterVecT1(prometheus.CounterOpts{
Name: "items_counted_by_status",
}, promsafe.SingleLabelProvider("status"))
}, "status")
// Manually register the counter
if err := prometheus.Register(c.Unsafe()); err != nil {