From 1c2884b8072e80bd4367516b119eb716e79bb276 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 13 Feb 2020 22:08:18 +0100 Subject: [PATCH] Add NewUntypedFunc to promauto This constructor was simply forgotten. Signed-off-by: beorn7 --- prometheus/promauto/auto.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/prometheus/promauto/auto.go b/prometheus/promauto/auto.go index 070a4f571..3c10c8524 100644 --- a/prometheus/promauto/auto.go +++ b/prometheus/promauto/auto.go @@ -234,6 +234,14 @@ func NewHistogramVec(opts prometheus.HistogramOpts, labelNames []string) *promet return With(prometheus.DefaultRegisterer).NewHistogramVec(opts, labelNames) } +// NewUntypedFunc works like the function of the same name in the prometheus +// package but it automatically registers the UntypedFunc with the +// prometheus.DefaultRegisterer. If the registration fails, NewUntypedFunc +// panics. +func NewUntypedFunc(opts prometheus.UntypedOpts, function func() float64) prometheus.UntypedFunc { + return With(prometheus.DefaultRegisterer).NewUntypedFunc(opts, function) +} + // Factory provides factory methods to create Collectors that are automatically // registered with a Registerer. Create a Factory with the With function, // providing a Registerer to auto-register created Collectors with. The zero @@ -353,3 +361,14 @@ func (f Factory) NewHistogramVec(opts prometheus.HistogramOpts, labelNames []str } return h } + +// NewUntypedFunc works like the function of the same name in the prometheus +// package but it automatically registers the UntypedFunc with the Factory's +// Registerer. +func (f Factory) NewUntypedFunc(opts prometheus.UntypedOpts, function func() float64) prometheus.UntypedFunc { + u := prometheus.NewUntypedFunc(opts, function) + if f.r != nil { + f.r.MustRegister(u) + } + return u +}