diff --git a/tsdb/postings_for_matchers_cache.go b/tsdb/postings_for_matchers_cache.go index 8397163f6..8ec373b8e 100644 --- a/tsdb/postings_for_matchers_cache.go +++ b/tsdb/postings_for_matchers_cache.go @@ -179,6 +179,12 @@ func (p *postingsForMatcherPromise) result(ctx context.Context) (index.Postings, if p.err != nil { return nil, fmt.Errorf("postingsForMatchers promise completed with error: %w", p.err) } + + trace.SpanFromContext(ctx).AddEvent("completed postingsForMatchers promise", trace.WithAttributes( + // Do not format the timestamp to introduce a performance regression. + attribute.Int64("evaluation completed at (epoch seconds)", p.evaluationCompletedAt.Unix()), + )) + return p.cloner.Clone(), nil } } @@ -212,7 +218,6 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex promise.callersCtxTracker.close() oldPromise := oldPromiseValue.(*postingsForMatcherPromise) - oldPromiseEvaluationCompletedAt := time.Time{} // Check if the promise already completed the execution and its TTL has not expired yet. // If the TTL has expired, we don't want to return it, so we just recompute the postings @@ -221,10 +226,6 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex if c.ttl > 0 { select { case <-oldPromise.done: - // Keep track of when the evaluation was completed. This field can be safely accessed only when - // the promise has done. - oldPromiseEvaluationCompletedAt = oldPromise.evaluationCompletedAt - if c.timeNow().Sub(oldPromise.evaluationCompletedAt) >= c.ttl { // The cached promise already expired, but it has not been evicted. span.AddEvent("skipping cached postingsForMatchers promise because its TTL already expired", trace.WithAttributes( @@ -261,8 +262,7 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex } c.metrics.hits.Inc() - span.AddEvent("using cached postingsForMatchers promise", trace.WithAttributes( - attribute.Stringer("cached promise evaluation completed at", evaluationCompletedAtTime(oldPromiseEvaluationCompletedAt)), + span.AddEvent("waiting cached postingsForMatchers promise", trace.WithAttributes( attribute.String("cache_key", key), )) @@ -300,16 +300,6 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex return promise.result } -type evaluationCompletedAtTime time.Time - -func (t evaluationCompletedAtTime) String() string { - if time.Time(t).IsZero() { - return "not completed (in-flight)" - } - - return time.Time(t).String() -} - type postingsForMatchersCachedCall struct { key string ts time.Time