Skip to content

let redis cache logs log with context #4785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/chunk/cache/redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *RedisCache) Fetch(ctx context.Context, keys []string) (found []string,
items, err = c.redis.MGet(ctx, keys)
if err != nil {
log.Error(err)
level.Error(c.logger).Log("msg", "failed to get from redis", "name", c.name, "err", err)
level.Error(util_log.WithContext(ctx, c.logger)).Log("msg", "failed to get from redis", "name", c.name, "err", err)
return err
}

Expand Down Expand Up @@ -97,7 +97,7 @@ func (c *RedisCache) Fetch(ctx context.Context, keys []string) (found []string,
func (c *RedisCache) Store(ctx context.Context, keys []string, bufs [][]byte) {
err := c.redis.MSet(ctx, keys, bufs)
if err != nil {
level.Error(c.logger).Log("msg", "failed to put to redis", "name", c.name, "err", err)
level.Error(util_log.WithContext(ctx, c.logger)).Log("msg", "failed to put to redis", "name", c.name, "err", err)
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/chunk/cache/snappy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/golang/snappy"

util_log "github.com/cortexproject/cortex/pkg/util/log"
)

type snappyCache struct {
Expand Down Expand Up @@ -36,7 +38,7 @@ func (s *snappyCache) Fetch(ctx context.Context, keys []string) ([]string, [][]b
for _, buf := range bufs {
d, err := snappy.Decode(nil, buf)
if err != nil {
level.Error(s.logger).Log("msg", "failed to decode cache entry", "err", err)
level.Error(util_log.WithContext(ctx, s.logger)).Log("msg", "failed to decode cache entry", "err", err)
return nil, nil, keys
}
ds = append(ds, d)
Expand Down
14 changes: 7 additions & 7 deletions pkg/querier/queryrange/results_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ func (s resultsCache) shouldCacheResponse(ctx context.Context, req Request, r Re
headerValues := getHeaderValuesWithName(r, cacheControlHeader)
for _, v := range headerValues {
if v == noStoreValue {
level.Debug(s.logger).Log("msg", fmt.Sprintf("%s header in response is equal to %s, not caching the response", cacheControlHeader, noStoreValue))
level.Debug(util_log.WithContext(ctx, s.logger)).Log("msg", fmt.Sprintf("%s header in response is equal to %s, not caching the response", cacheControlHeader, noStoreValue))
return false
}
}

if !s.isAtModifierCachable(req, maxCacheTime) {
if !s.isAtModifierCachable(ctx, req, maxCacheTime) {
return false
}

Expand All @@ -253,13 +253,13 @@ func (s resultsCache) shouldCacheResponse(ctx context.Context, req Request, r Re
genNumberFromCtx := cache.ExtractCacheGenNumber(ctx)

if len(genNumbersFromResp) == 0 && genNumberFromCtx != "" {
level.Debug(s.logger).Log("msg", fmt.Sprintf("we found results cache gen number %s set in store but none in headers", genNumberFromCtx))
level.Debug(util_log.WithContext(ctx, s.logger)).Log("msg", fmt.Sprintf("we found results cache gen number %s set in store but none in headers", genNumberFromCtx))
return false
}

for _, gen := range genNumbersFromResp {
if gen != genNumberFromCtx {
level.Debug(s.logger).Log("msg", fmt.Sprintf("inconsistency in results cache gen numbers %s (GEN-FROM-RESPONSE) != %s (GEN-FROM-STORE), not caching the response", gen, genNumberFromCtx))
level.Debug(util_log.WithContext(ctx, s.logger)).Log("msg", fmt.Sprintf("inconsistency in results cache gen numbers %s (GEN-FROM-RESPONSE) != %s (GEN-FROM-STORE), not caching the response", gen, genNumberFromCtx))
return false
}
}
Expand All @@ -271,7 +271,7 @@ var errAtModifierAfterEnd = errors.New("at modifier after end")

// isAtModifierCachable returns true if the @ modifier result
// is safe to cache.
func (s resultsCache) isAtModifierCachable(r Request, maxCacheTime int64) bool {
func (s resultsCache) isAtModifierCachable(ctx context.Context, r Request, maxCacheTime int64) bool {
// There are 2 cases when @ modifier is not safe to cache:
// 1. When @ modifier points to time beyond the maxCacheTime.
// 2. If the @ modifier time is > the query range end while being
Expand All @@ -285,7 +285,7 @@ func (s resultsCache) isAtModifierCachable(r Request, maxCacheTime int64) bool {
expr, err := parser.ParseExpr(query)
if err != nil {
// We are being pessimistic in such cases.
level.Warn(s.logger).Log("msg", "failed to parse query, considering @ modifier as not cachable", "query", query, "err", err)
level.Warn(util_log.WithContext(ctx, s.logger)).Log("msg", "failed to parse query, considering @ modifier as not cachable", "query", query, "err", err)
return false
}

Expand Down Expand Up @@ -596,7 +596,7 @@ func (s resultsCache) put(ctx context.Context, key string, extents []Extent) {
Extents: extents,
})
if err != nil {
level.Error(s.logger).Log("msg", "error marshalling cached value", "err", err)
level.Error(util_log.WithContext(ctx, s.logger)).Log("msg", "error marshalling cached value", "err", err)
return
}

Expand Down