Skip to content

Commit

Permalink
Include cache entry timestamp in trace events emitted by postings for…
Browse files Browse the repository at this point in the history
… matchers cache

Signed-off-by: Charles Korn <[email protected]>
  • Loading branch information
charleskorn committed Jan 15, 2025
1 parent a1e2bcf commit ec8e446
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions tsdb/postings_for_matchers_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ type postingsForMatcherPromise struct {
// callers contexts get canceled.
callersCtxTracker *contextsTracker

// Keep track of the time this promise was evaluated, so we can understand the age of this cache entry in traces.
evaluatedAt time.Time

// The result of the promise is stored either in cloner or err (only of the two is valued).
// Do not access these fields until the done channel is closed.
done chan struct{}
Expand Down Expand Up @@ -169,7 +172,9 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex
span := trace.SpanFromContext(ctx)

promiseCallersCtxTracker, promiseExecCtx := newContextsTracker()
ts := c.timeNow()
promise := &postingsForMatcherPromise{
evaluatedAt: ts,
done: make(chan struct{}),
callersCtxTracker: promiseCallersCtxTracker,
}
Expand Down Expand Up @@ -213,12 +218,16 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex

span.AddEvent("using cached postingsForMatchers promise", trace.WithAttributes(
attribute.String("cache_key", key),
attribute.Int64("cache_entry_evaluated_at", promise.evaluatedAt.Unix()),
))

return oldPromise.result
}

span.AddEvent("no postingsForMatchers promise in cache, executing query", trace.WithAttributes(attribute.String("cache_key", key)))
span.AddEvent("no postingsForMatchers promise in cache, executing query", trace.WithAttributes(
attribute.String("cache_key", key),
attribute.Int64("cache_entry_evaluated_at", promise.evaluatedAt.Unix()),
))

// promise was stored, close its channel after fulfilment
defer close(promise.done)
Expand All @@ -241,7 +250,7 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex

sizeBytes := int64(len(key) + size.Of(promise))

c.onPromiseExecutionDone(ctx, key, c.timeNow(), sizeBytes, promise.err)
c.onPromiseExecutionDone(ctx, key, ts, sizeBytes, promise.err)
return promise.result
}

Expand Down

0 comments on commit ec8e446

Please sign in to comment.