MCPcopy
hub / github.com/grafana/tempo / RandomAttrFromTrace

Function RandomAttrFromTrace

pkg/util/trace_info.go:365–416  ·  view source on GitHub ↗

RandomAttrFromTrace returns a random attribute from the trace for use in search validation. Integer attributes are never chosen: they are not unique enough for search.

(t *tempopb.Trace)

Source from the content-addressed store, hash-verified

363// RandomAttrFromTrace returns a random attribute from the trace for use in search validation.
364// Integer attributes are never chosen: they are not unique enough for search.
365func RandomAttrFromTrace(t *tempopb.Trace) *v1common.KeyValue {
366 r := newRand(time.Now())
367
368 if len(t.ResourceSpans) == 0 {
369 return nil
370 }
371 batch := randFrom(r, t.ResourceSpans)
372
373 // maybe choose resource attribute
374 res := batch.Resource
375 if len(res.Attributes) > 0 && r.Int()%2 == 1 {
376 attr := randFrom(r, res.Attributes)
377 // skip service.name because service names have low cardinality and produce queries with
378 // too many results in tempo-vulture
379 if attr.Key != "service.name" {
380 if attr.Value == nil {
381 return attr
382 }
383 if _, ok := attr.Value.Value.(*v1common.AnyValue_IntValue); !ok {
384 return attr
385 }
386 }
387 }
388
389 if len(batch.ScopeSpans) == 0 {
390 return nil
391 }
392 ss := randFrom(r, batch.ScopeSpans)
393
394 if len(ss.Spans) == 0 {
395 return nil
396 }
397 span := randFrom(r, ss.Spans)
398
399 if len(span.Attributes) == 0 {
400 return nil
401 }
402
403 // Pick only from non-integer attributes (integers are not unique enough for search).
404 nonIntAttrs := make([]*v1common.KeyValue, 0, len(span.Attributes))
405 for _, a := range span.Attributes {
406 if a.Value == nil {
407 nonIntAttrs = append(nonIntAttrs, a)
408 } else if _, ok := a.Value.Value.(*v1common.AnyValue_IntValue); !ok {
409 nonIntAttrs = append(nonIntAttrs, a)
410 }
411 }
412 if len(nonIntAttrs) == 0 {
413 return nil
414 }
415 return randFrom(r, nonIntAttrs)
416}
417
418func randFrom[T any](r *rand.Rand, s []T) T {
419 return s[r.Intn(len(s))]

Callers 4

validateTraceSearchMethod · 0.92
searchTagFunction · 0.92
searchTraceqlFunction · 0.92
queryMetricsFunction · 0.92

Calls 4

newRandFunction · 0.85
randFromFunction · 0.85
IntMethod · 0.80
NowMethod · 0.65

Tested by

no test coverage detected