From 9a8d1a253dd5ab3825439c10971d24a85f76313b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20R=C3=B3=C5=BCa=C5=84ski?= Date: Thu, 13 Jun 2024 21:49:06 +0200 Subject: [PATCH] Fix crash on non-utf8 metric label --- sql/querycache.go | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/sql/querycache.go b/sql/querycache.go index dbf5027ec9..7aa1740230 100644 --- a/sql/querycache.go +++ b/sql/querycache.go @@ -4,6 +4,7 @@ import ( "context" "slices" "sync" + "unicode/utf8" "github.com/hashicorp/golang-lru/v2/simplelru" ) @@ -222,24 +223,11 @@ func (c *queryCache) IsCached() bool { return c != nil } -func reportHit(key queryCacheKey) { - switch key.Kind { - case "atx-blob": - // don't use the key as it would create too many labels - queryCacheHits.WithLabelValues(string(key.Kind), "").Inc() - default: - queryCacheHits.WithLabelValues(string(key.Kind), key.Key).Inc() - } -} - -func reportMiss(key queryCacheKey) { - switch key.Kind { - case "atx-blob": - // don't use the key as it would create too many labels - queryCacheMisses.WithLabelValues(string(key.Kind), "").Inc() - default: - queryCacheMisses.WithLabelValues(string(key.Kind), key.Key).Inc() +func keyLabel(key queryCacheKey) string { + if key.Kind == "atx-blob" || key.Kind == "activeset-blob" || !utf8.ValidString(key.Key) { + return "" } + return key.Key } func (c *queryCache) GetValue( @@ -259,7 +247,7 @@ func (c *queryCache) GetValue( v, found := c.get(key, subKey) var err error if !found { - reportMiss(key) + queryCacheMisses.WithLabelValues(string(key.Kind), keyLabel(key)).Inc() // This may seem like a race, but at worst, retrieve() will be // called several times when populating this cached entry. // That's better than locking for the duration of retrieve(), @@ -269,7 +257,7 @@ func (c *queryCache) GetValue( c.set(key, subKey, v) } } else { - reportHit(key) + queryCacheHits.WithLabelValues(string(key.Kind), keyLabel(key)).Inc() } return v, err }