Skip to content

Commit 7131f78

Browse files
feat: track cache key prefix
1 parent 7c1e49d commit 7131f78

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

internal/metrics/metrics.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ var (
1212
CounterCacheHit = stats.Int64("cache_hits", "Number of cache hits", "1")
1313
CounterCacheMiss = stats.Int64("cache_misses", "Number of cache misses", "1")
1414

15-
TagOSArch = tag.MustNewKey("os_arch")
16-
TagCacheKey = tag.MustNewKey("cache_key")
15+
TagOSArch = tag.MustNewKey("os_arch")
16+
TagCacheKey = tag.MustNewKey("cache_key")
17+
TagCacheKeyPrefix = tag.MustNewKey("cache_key_prefix")
1718
)
1819

1920
var views = []*view.View{

internal/server/cache.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ type (
1818
cacheKey string
1919
)
2020

21+
func (c cacheKey) Prefix() cacheKeyPrefix {
22+
prefix, _, _ := strings.Cut(string(c), "/")
23+
return cacheKeyPrefix(prefix)
24+
}
25+
2126
const (
2227
cacheKeyPrefixBatchRequest cacheKeyPrefix = "batch"
2328
cacheKeyPrefixRequest cacheKeyPrefix = "request"
@@ -36,25 +41,26 @@ func (s *Server) getCacheKeyWithPrefix(p cacheKeyPrefix, key string) cacheKey {
3641
return cacheKey(fmt.Sprintf("%s/%s", p, key))
3742
}
3843

44+
func getCacheMetricsCtx(ctx context.Context, k cacheKey) context.Context {
45+
ctx, _ = tag.New(ctx, tag.Upsert(metrics.TagCacheKey, string(k)), tag.Upsert(metrics.TagCacheKeyPrefix, string(k.Prefix())))
46+
return ctx
47+
}
48+
3949
func (s *Server) getFromCache(ctx context.Context, k cacheKey) (any, bool) {
40-
strKey := string(k)
41-
val, ok := s.cache.Get(strKey)
50+
val, ok := s.cache.Get(string(k))
4251
if ok {
43-
ctx, _ = tag.New(ctx, tag.Upsert(metrics.TagCacheKey, strKey))
44-
stats.Record(ctx, metrics.CounterCacheHit.M(1))
52+
stats.Record(getCacheMetricsCtx(ctx, k), metrics.CounterCacheHit.M(1))
4553
}
4654
return val, ok
4755
}
4856

4957
func (s *Server) setInCache(ctx context.Context, k cacheKey, v any, expiration ...time.Duration) {
50-
strKey := string(k)
51-
ctx, _ = tag.New(ctx, tag.Upsert(metrics.TagCacheKey, strKey))
52-
stats.Record(ctx, metrics.CounterCacheMiss.M(1))
58+
stats.Record(getCacheMetricsCtx(ctx, k), metrics.CounterCacheMiss.M(1))
5359
exp := cache.DefaultExpiration
5460
if len(expiration) > 0 {
5561
exp = expiration[0]
5662
}
57-
s.cache.Set(strKey, v, exp)
63+
s.cache.Set(string(k), v, exp)
5864
}
5965

6066
func (s *Server) invalidateByPrefix(prefix cacheKey) int {

0 commit comments

Comments
 (0)