From 33e53398a60230129583c8a8ad2e463eb58e2515 Mon Sep 17 00:00:00 2001 From: Gianluca Zuccarelli Date: Fri, 25 Nov 2022 12:50:06 +0000 Subject: [PATCH] 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. --- internal/prometheus/status_metrics.go | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 internal/prometheus/status_metrics.go diff --git a/internal/prometheus/status_metrics.go b/internal/prometheus/status_metrics.go new file mode 100644 index 000000000..e9d26dfb2 --- /dev/null +++ b/internal/prometheus/status_metrics.go @@ -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 +}