prometheus: add status metrics

Add a helper function to register the same metrics
for both the worker and composer - the only difference
being the subsystem name. The function checks if the
metric has already been registered and, if so, returns
the already registered metric.
This commit is contained in:
Gianluca Zuccarelli 2022-11-25 12:50:06 +00:00 committed by Ondřej Budai
parent 8e82b223af
commit 33e53398a6

View file

@ -0,0 +1,30 @@
package prometheus
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
func StatusRequestsCounter(subsystem string) *prometheus.CounterVec {
// return a function so we can use this for both
// composer & worker metrics
reg := prometheus.NewRegistry()
counter := promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Name: "request_count",
Namespace: Namespace,
Subsystem: subsystem,
Help: "total number of http requests",
}, []string{"method", "path", "code", "service"})
err := prometheus.Register(counter)
if err != nil {
registered, ok := err.(prometheus.AlreadyRegisteredError)
if !ok {
panic(err)
}
// return existing counter if metrics already registered
return registered.ExistingCollector.(*prometheus.CounterVec)
}
return counter
}