debian-forge-composer/internal/prometheus/status_metrics.go
Gianluca Zuccarelli 33e53398a6 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.
2022-11-30 11:14:29 +01:00

30 lines
856 B
Go

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
}