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

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

diff --git a/main.go b/main.go
index 8f282c3..b7024c8 100644
--- a/main.go
+++ b/main.go
@@ -16,6 +16,8 @@ var queueGauge = promauto.NewGaugeVec(
 	},
 	[]string{"state"},
 )
+var queueGaugeQueued = queueGauge.WithLabelValues("queued")
+var queueGaugeInProgress = queueGauge.WithLabelValues("inProgress")
 
 var imageCounter = promauto.NewCounterVec(
 	prometheus.CounterOpts{
@@ -24,6 +26,8 @@ var imageCounter = promauto.NewCounterVec(
 	},
 	[]string{"result"},
 )
+var imageCounterSuccess = imageCounter.WithLabelValues("success")
+var imageCounterFailure = imageCounter.WithLabelValues("failure")
 
 var imageProcessDuration = promauto.NewCounter(prometheus.CounterOpts{
 	Name: "imghost_process_duration",
@@ -47,7 +51,7 @@ func main() {
 	})
 
 	go serveQueue(client, config.ImageQueue, func(value string) {
-		queueGauge.WithLabelValues("queued").Dec()
+		queueGaugeQueued.Dec()
 		ProcessImage(&config, client, value)
 	})
 	if err := http.ListenAndServe(":2112", nil); err != nil {
diff --git a/process.go b/process.go
index 996cf1b..a5f8e94 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) {
-	queueGauge.WithLabelValues("in_progress").Inc()
-	defer queueGauge.WithLabelValues("in_progress").Dec()
+	queueGaugeInProgress.Inc()
+	defer queueGaugeInProgress.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 {
-		imageCounter.WithLabelValues("error").Inc()
+		imageCounterFailure.Inc()
 		returnResult(config, client, Result{
 			Id:      image.Id,
 			Success: false,
 			Errors:  errorMessages,
 		})
 	} else {
-		imageCounter.WithLabelValues("success").Inc()
+		imageCounterSuccess.Inc()
 		returnResult(config, client, Result{
 			Id:      image.Id,
 			Success: true,
diff --git a/redis.go b/redis.go
index df91b42..6f6ec92 100644
--- a/redis.go
+++ b/redis.go
@@ -11,7 +11,7 @@ func serveQueue(client *redis.Client, queue string, function func(value string))
 		element := client.BLPop(0, fmt.Sprintf("queue:%s", queue))
 		if len(element.Val()) == 2 {
 			value := element.Val()[1]
-			queueGauge.Inc()
+			queueGaugeQueued.Inc()
 			go function(value)
 		}
 	}
-- 
GitLab