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:
parent
8e82b223af
commit
33e53398a6
1 changed files with 30 additions and 0 deletions
30
internal/prometheus/status_metrics.go
Normal file
30
internal/prometheus/status_metrics.go
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue