From c666f4d9cc57eedc65292daaa4c8a104d754fffe Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Thu, 16 Jan 2025 21:33:22 +0100 Subject: [PATCH] feat(metrics): add use labels in cache response --- modules/cache/cache.go | 47 ++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/modules/cache/cache.go b/modules/cache/cache.go index 49bb3bff9f..e1af57faa7 100644 --- a/modules/cache/cache.go +++ b/modules/cache/cache.go @@ -9,32 +9,39 @@ import ( "time" "code.gitea.io/gitea/modules/setting" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" _ "gitea.com/go-chi/cache/memcache" //nolint:depguard // memcache plugin for cache, it is required for config "ADAPTER=memcache" ) -var defaultCache StringCache -var hitCounter = promauto.NewCounter(prometheus.CounterOpts{ - Namespace: "gitea", - Help: "Cache hit count", - Subsystem: "cache", - Name: "hit", -}) -var missCounter = promauto.NewCounter(prometheus.CounterOpts{ - Namespace: "gitea", - Help: "Cache miss count", - Subsystem: "cache", - Name: "miss", -}) -var latencyHistogram = promauto.NewHistogram( - prometheus.HistogramOpts{ - Namespace: "gitea", - Help: "Cache latency", - Subsystem: "cache", - Name: "duration", - }, +var ( + defaultCache StringCache + + // TODO: Combine hit and miss into one + hitCounter = promauto.NewCounter(prometheus.CounterOpts{ + Namespace: "gitea", + Help: "Cache count", + Subsystem: "cache", + Name: "response", + ConstLabels: prometheus.Labels{"state": "hit"}, + }) + missCounter = promauto.NewCounter(prometheus.CounterOpts{ + Namespace: "gitea", + Help: "Cache count", + Subsystem: "cache", + Name: "response", + ConstLabels: prometheus.Labels{"state": "miss"}, + }) + latencyHistogram = promauto.NewHistogram( + prometheus.HistogramOpts{ + Namespace: "gitea", + Help: "Cache latency", + Subsystem: "cache", + Name: "duration", + }, + ) ) // Init start cache service