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

Method FindByTraceID

modules/livestore/instance_search.go:632–695  ·  view source on GitHub ↗
(ctx context.Context, traceID []byte, allowPartialTrace bool)

Source from the content-addressed store, hash-verified

630}
631
632func (i *instance) FindByTraceID(ctx context.Context, traceID []byte, allowPartialTrace bool) (*tempopb.TraceByIDResponse, error) {
633 var (
634 metricsMtx sync.Mutex
635 metrics = tempopb.TraceByIDMetrics{}
636 maxBytes = i.overrides.MaxBytesPerTrace(i.tenantID)
637 searchOpts = common.DefaultSearchOptions()
638 combiner = trace.NewCombiner(maxBytes, allowPartialTrace)
639 )
640
641 if !allowPartialTrace {
642 searchOpts = common.DefaultSearchOptionsWithMaxBytes(maxBytes)
643 }
644
645 // Check live traces first
646 i.liveTracesMtx.Lock()
647 if liveTrace, ok := i.liveTraces.Traces[util.HashForTraceID(traceID)]; ok {
648 tempTrace := &tempopb.Trace{}
649 tempTrace.ResourceSpans = liveTrace.Batches
650 // Previously there was some logic here to add inspected bytes in the ingester. But its hard to do with the different
651 // live traces format and feels inaccurate.
652 _, err := combiner.Consume(tempTrace)
653 if err != nil {
654 i.liveTracesMtx.Unlock()
655 return nil, fmt.Errorf("unable to unmarshal liveTrace: %w", err)
656 }
657 }
658 i.liveTracesMtx.Unlock()
659
660 search := func(ctx context.Context, _ *backend.BlockMeta, b block) error {
661 trace, err := b.FindTraceByID(ctx, traceID, searchOpts)
662 if err != nil {
663 return err
664 }
665 if trace == nil {
666 return nil
667 }
668
669 _, err = combiner.Consume(trace.Trace)
670 if err != nil {
671 return err
672 }
673
674 if trace.Metrics != nil {
675 metricsMtx.Lock()
676 defer metricsMtx.Unlock()
677
678 metrics.InspectedBytes += trace.Metrics.InspectedBytes
679 }
680 return nil
681 }
682
683 err := i.iterateBlocks(ctx, time.Unix(0, 0), time.Unix(0, 0), search)
684 if err != nil {
685 level.Error(i.logger).Log("msg", "error in FindTraceByID", "err", err)
686 return nil, fmt.Errorf("error searching for trace: %w", err)
687 }
688
689 result, _ := combiner.Result()

Callers 4

TestInstanceBackpressureFunction · 0.80
FindTraceByIDMethod · 0.80

Calls 11

iterateBlocksMethod · 0.95
DefaultSearchOptionsFunction · 0.92
NewCombinerFunction · 0.92
HashForTraceIDFunction · 0.92
MaxBytesPerTraceMethod · 0.65
FindTraceByIDMethod · 0.65
LogMethod · 0.65
ErrorMethod · 0.65
ResultMethod · 0.65
ConsumeMethod · 0.45

Tested by 3

TestInstanceBackpressureFunction · 0.64