Skip to content

Commit

Permalink
Query: Put cache name on tracing spans (#4696)
Browse files Browse the repository at this point in the history
* Put cache name on tracing spans

With multiple caches, this change lets you see clearly which spans
relate to which cache.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>

* Fix up mock cache with new Name() method

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
  • Loading branch information
bboreham authored Sep 23, 2021
1 parent 177b4f2 commit c15594a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
### Added

- [#4680](https://github.com/thanos-io/thanos/pull/4680) Query: add `exemplar.partial-response` flag to control partial response.
- [#4696](https://github.com/thanos-io/thanos/pull/4696) Query: add cache name to tracing spans.

## v0.23.0 - In Progress

Expand Down
2 changes: 2 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ type Cache interface {
// Fetch multiple keys from cache. Returns map of input keys to data.
// If key isn't in the map, data for given key was not found.
Fetch(ctx context.Context, keys []string) map[string][]byte

Name() string
}
6 changes: 6 additions & 0 deletions pkg/cache/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type InMemoryCache struct {
logger log.Logger
maxSizeBytes uint64
maxItemSizeBytes uint64
name string

mtx sync.Mutex
curSize uint64
Expand Down Expand Up @@ -100,6 +101,7 @@ func NewInMemoryCacheWithConfig(name string, logger log.Logger, reg prometheus.R
logger: logger,
maxSizeBytes: uint64(config.MaxSize),
maxItemSizeBytes: uint64(config.MaxItemSize),
name: name,
}

c.evicted = promauto.With(reg).NewCounter(prometheus.CounterOpts{
Expand Down Expand Up @@ -303,3 +305,7 @@ func (c *InMemoryCache) Fetch(ctx context.Context, keys []string) map[string][]b
}
return results
}

func (c *InMemoryCache) Name() string {
return c.name
}
6 changes: 6 additions & 0 deletions pkg/cache/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type MemcachedCache struct {
logger log.Logger
memcached cacheutil.MemcachedClient
name string

// Metrics.
requests prometheus.Counter
Expand All @@ -30,6 +31,7 @@ func NewMemcachedCache(name string, logger log.Logger, memcached cacheutil.Memca
c := &MemcachedCache{
logger: logger,
memcached: memcached,
name: name,
}

c.requests = promauto.With(reg).NewCounter(prometheus.CounterOpts{
Expand Down Expand Up @@ -81,3 +83,7 @@ func (c *MemcachedCache) Fetch(ctx context.Context, keys []string) map[string][]
c.hits.Add(float64(len(results)))
return results
}

func (c *MemcachedCache) Name() string {
return c.name
}
5 changes: 5 additions & 0 deletions pkg/cache/tracing_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (t TracingCache) Store(ctx context.Context, data map[string][]byte, ttl tim

func (t TracingCache) Fetch(ctx context.Context, keys []string) (result map[string][]byte) {
tracing.DoWithSpan(ctx, "cache_fetch", func(spanCtx context.Context, span opentracing.Span) {
span.SetTag("name", t.Name())
span.LogKV("requested keys", len(keys))

result = t.c.Fetch(spanCtx, keys)
Expand All @@ -39,3 +40,7 @@ func (t TracingCache) Fetch(ctx context.Context, keys []string) (result map[stri
})
return
}

func (t TracingCache) Name() string {
return t.c.Name()
}
4 changes: 4 additions & 0 deletions pkg/store/cache/caching_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ func (m *mockCache) Fetch(_ context.Context, keys []string) map[string][]byte {
return found
}

func (m *mockCache) Name() string {
return "mockCache"
}

func (m *mockCache) flush() {
m.cache = map[string]cacheItem{}
}
Expand Down

0 comments on commit c15594a

Please sign in to comment.