From 3aa4d594853b593e6c74a7117e9515fc51bd615c Mon Sep 17 00:00:00 2001
From: Janne Mareike Koschinski <janne@kuschku.de>
Date: Sun, 18 Jul 2021 20:12:51 +0200
Subject: [PATCH] Cleanup metric handling

Signed-off-by: Janne Mareike Koschinski <janne@kuschku.de>
---
 main.go    | 43 +++++++++++++------------------------------
 process.go |  8 ++++----
 2 files changed, 17 insertions(+), 34 deletions(-)

diff --git a/main.go b/main.go
index 8250df1..8f282c3 100644
--- a/main.go
+++ b/main.go
@@ -9,37 +9,21 @@ import (
 	"net/http"
 )
 
-var queueGauge = promauto.NewGauge(prometheus.GaugeOpts{
-	Name: "imghost_waiting_images",
-	Help: "The number of waiting image events",
-	ConstLabels: map[string]string{
-		"state": "queued",
+var queueGauge = promauto.NewGaugeVec(
+	prometheus.GaugeOpts{
+		Name: "imghost_waiting_images",
+		Help: "The number of waiting image events",
 	},
-})
-
-var inProgressGauge = promauto.NewGauge(prometheus.GaugeOpts{
-	Name: "imghost_waiting_images",
-	Help: "The number of image events in progress",
-	ConstLabels: map[string]string{
-		"state": "in_progress",
-	},
-})
-
-var imageCounterSuccess = promauto.NewCounter(prometheus.CounterOpts{
-	Name: "imghost_processed_images_total",
-	Help: "The total number of successfully processed image events",
-	ConstLabels: map[string]string{
-		"success": "true",
-	},
-})
+	[]string{"state"},
+)
 
-var imageCounterFailure = promauto.NewCounter(prometheus.CounterOpts{
-	Name: "imghost_processed_images_total",
-	Help: "The total number of errored image events",
-	ConstLabels: map[string]string{
-		"success": "false",
+var imageCounter = promauto.NewCounterVec(
+	prometheus.CounterOpts{
+		Name: "imghost_processed_images_total",
+		Help: "The total number of successfully processed image events",
 	},
-})
+	[]string{"result"},
+)
 
 var imageProcessDuration = promauto.NewCounter(prometheus.CounterOpts{
 	Name: "imghost_process_duration",
@@ -63,8 +47,7 @@ func main() {
 	})
 
 	go serveQueue(client, config.ImageQueue, func(value string) {
-		queueGauge.Dec()
-		inProgressGauge.Inc()
+		queueGauge.WithLabelValues("queued").Dec()
 		ProcessImage(&config, client, value)
 	})
 	if err := http.ListenAndServe(":2112", nil); err != nil {
diff --git a/process.go b/process.go
index acb81e9..996cf1b 100644
--- a/process.go
+++ b/process.go
@@ -14,8 +14,8 @@ func trackTime(start time.Time, function func(time.Duration)) {
 }
 
 func ProcessImage(config *Config, client *redis.Client, value string) {
-	inProgressGauge.Inc()
-	defer inProgressGauge.Dec()
+	queueGauge.WithLabelValues("in_progress").Inc()
+	defer queueGauge.WithLabelValues("in_progress").Dec()
 	defer trackTime(time.Now(), func(duration time.Duration) {
 		imageProcessDuration.Add(float64(duration.Milliseconds()))
 	})
@@ -35,14 +35,14 @@ func ProcessImage(config *Config, client *redis.Client, value string) {
 	}
 
 	if len(errors) != 0 {
-		imageCounterFailure.Inc()
+		imageCounter.WithLabelValues("error").Inc()
 		returnResult(config, client, Result{
 			Id:      image.Id,
 			Success: false,
 			Errors:  errorMessages,
 		})
 	} else {
-		imageCounterSuccess.Inc()
+		imageCounter.WithLabelValues("success").Inc()
 		returnResult(config, client, Result{
 			Id:      image.Id,
 			Success: true,
-- 
GitLab