Allocate tracing attributes up front

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen 2023-11-01 11:53:10 +01:00
parent 46bf26e841
commit ef70a6e6cf

View file

@ -46,10 +46,11 @@ func NewPostingsForMatchersCache(ttl time.Duration, maxItems int, maxBytes int64
calls: &sync.Map{}, calls: &sync.Map{},
cached: list.New(), cached: list.New(),
ttl: ttl, ttl: ttl,
maxItems: maxItems, ttlAttrib: attribute.Stringer("ttl", ttl),
maxBytes: maxBytes, maxItems: maxItems,
force: force, maxBytes: maxBytes,
force: force,
timeNow: time.Now, timeNow: time.Now,
postingsForMatchers: PostingsForMatchers, postingsForMatchers: PostingsForMatchers,
@ -68,10 +69,11 @@ type PostingsForMatchersCache struct {
cached *list.List cached *list.List
cachedBytes int64 cachedBytes int64
ttl time.Duration ttl time.Duration
maxItems int ttlAttrib attribute.KeyValue
maxBytes int64 maxItems int
force bool maxBytes int64
force bool
// timeNow is the time.Now that can be replaced for testing purposes // timeNow is the time.Now that can be replaced for testing purposes
timeNow func() time.Time timeNow func() time.Time
@ -117,8 +119,7 @@ type postingsForMatcherPromise struct {
} }
func (p *postingsForMatcherPromise) result(ctx context.Context) (index.Postings, error) { func (p *postingsForMatcherPromise) result(ctx context.Context) (index.Postings, error) {
ctx, span := p.tracer.Start(ctx, "postingsForMatcherPromise.result") span := trace.SpanFromContext(ctx)
defer span.End()
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -148,9 +149,7 @@ func (p *postingsForMatcherPromise) result(ctx context.Context) (index.Postings,
func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Context, ix IndexPostingsReader, ms []*labels.Matcher) func(context.Context) (index.Postings, error) { func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Context, ix IndexPostingsReader, ms []*labels.Matcher) func(context.Context) (index.Postings, error) {
key := matchersKey(ms) key := matchersKey(ms)
ctx, span := c.tracer.Start(ctx, "PostingsForMatchersCache.postingsForMatchersPromise", trace.WithAttributes( span := trace.SpanFromContext(ctx)
attribute.String("cache_key", key),
))
defer span.End() defer span.End()
promise := &postingsForMatcherPromise{ promise := &postingsForMatcherPromise{
@ -161,7 +160,9 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex
oldPromise, loaded := c.calls.LoadOrStore(key, promise) oldPromise, loaded := c.calls.LoadOrStore(key, promise)
if loaded { if loaded {
// promise was not stored, we return a previously stored promise, that's possibly being fulfilled in another goroutine // promise was not stored, we return a previously stored promise, that's possibly being fulfilled in another goroutine
span.AddEvent("using cached postingsForMatchers promise") span.AddEvent("using cached postingsForMatchers promise", trace.WithAttributes(
attribute.String("cache_key", key),
))
close(promise.done) close(promise.done)
return oldPromise.(*postingsForMatcherPromise).result return oldPromise.(*postingsForMatcherPromise).result
} }
@ -177,11 +178,14 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex
// cancelled their context? // cancelled their context?
if postings, err := c.postingsForMatchers(context.Background(), ix, ms...); err != nil { if postings, err := c.postingsForMatchers(context.Background(), ix, ms...); err != nil {
span.AddEvent("postingsForMatchers failed", trace.WithAttributes( span.AddEvent("postingsForMatchers failed", trace.WithAttributes(
attribute.String("cache_key", key),
attribute.String("err", err.Error()), attribute.String("err", err.Error()),
)) ))
promise.err = err promise.err = err
} else { } else {
span.AddEvent("postingsForMatchers succeeded") span.AddEvent("postingsForMatchers succeeded", trace.WithAttributes(
attribute.String("cache_key", key),
))
promise.cloner = index.NewPostingsCloner(postings) promise.cloner = index.NewPostingsCloner(postings)
} }
@ -251,7 +255,7 @@ func (c *PostingsForMatchersCache) created(ctx context.Context, key string, ts t
if c.ttl <= 0 { if c.ttl <= 0 {
span.AddEvent("deleting cached promise since c.ttl <= 0", trace.WithAttributes( span.AddEvent("deleting cached promise since c.ttl <= 0", trace.WithAttributes(
attribute.Stringer("ttl", c.ttl), c.ttlAttrib,
)) ))
c.calls.Delete(key) c.calls.Delete(key)
return return
@ -267,7 +271,7 @@ func (c *PostingsForMatchersCache) created(ctx context.Context, key string, ts t
}) })
c.cachedBytes += sizeBytes c.cachedBytes += sizeBytes
span.AddEvent("added cached value to expiry queue", trace.WithAttributes( span.AddEvent("added cached value to expiry queue", trace.WithAttributes(
attribute.Stringer("ttl", c.ttl), c.ttlAttrib,
attribute.Stringer("timestamp", ts), attribute.Stringer("timestamp", ts),
attribute.Int64("size in bytes", sizeBytes), attribute.Int64("size in bytes", sizeBytes),
attribute.Int64("cached bytes", c.cachedBytes), attribute.Int64("cached bytes", c.cachedBytes),