Skip to content

Commit

Permalink
Fix crash on non-utf8 metric label
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Jun 13, 2024
1 parent f34234a commit 9a8d1a2
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions sql/querycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"slices"
"sync"
"unicode/utf8"

"github.com/hashicorp/golang-lru/v2/simplelru"
)
Expand Down Expand Up @@ -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(
Expand All @@ -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(),
Expand All @@ -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
}
Expand Down

0 comments on commit 9a8d1a2

Please sign in to comment.