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

Method ConsumeWithFinal

pkg/model/trace/combine.go:52–135  ·  view source on GitHub ↗

ConsumeWithFinal consumes the trace, but allows for performance savings when it is known that this is the last expected input trace.

(tr *tempopb.Trace, final bool)

Source from the content-addressed store, hash-verified

50// ConsumeWithFinal consumes the trace, but allows for performance savings when
51// it is known that this is the last expected input trace.
52func (c *Combiner) ConsumeWithFinal(tr *tempopb.Trace, final bool) (int, error) {
53 c.mtx.Lock()
54 defer c.mtx.Unlock()
55
56 var spanCount int
57 if tr == nil || c.IsPartialTrace() {
58 return spanCount, nil
59 }
60
61 h := util.NewTokenHasher()
62 buffer := make([]byte, 4)
63
64 // First call?
65 if c.result == nil {
66 c.result = tr
67
68 // Pre-alloc map with input size. This saves having to grow the
69 // map from the small starting size.
70 n := 0
71 for _, b := range c.result.ResourceSpans {
72 for _, ils := range b.ScopeSpans {
73 n += len(ils.Spans)
74 }
75 }
76 c.spans = make(map[token]struct{}, n)
77
78 for _, b := range c.result.ResourceSpans {
79 for _, ils := range b.ScopeSpans {
80 for _, s := range ils.Spans {
81 c.spans[token(util.TokenForID(h, buffer, int32(s.Kind), s.SpanId))] = struct{}{}
82 }
83 }
84 }
85
86 maxSizeErr := c.sizeError()
87 if c.IsPartialTrace() {
88 return spanCount, nil
89 }
90 return spanCount, maxSizeErr
91 }
92
93 // loop through every span and copy spans in B that don't exist to A
94 for _, b := range tr.ResourceSpans {
95 notFoundILS := b.ScopeSpans[:0]
96
97 for _, ils := range b.ScopeSpans {
98 notFoundSpans := ils.Spans[:0]
99 for _, s := range ils.Spans {
100 // if not already encountered, then keep
101 token := token(util.TokenForID(h, buffer, int32(s.Kind), s.SpanId))
102 _, ok := c.spans[token]
103 if !ok {
104 notFoundSpans = append(notFoundSpans, s)
105
106 // If last expected input, then we don't need to record
107 // the visited spans. Optimization has significant savings.
108 if !final {
109 c.spans[token] = struct{}{}

Callers 7

PrepareForReadMethod · 0.95
CombineMethod · 0.95
PrepareForReadMethod · 0.95
CombineMethod · 0.95
BenchmarkCombineFunction · 0.95
ConsumeMethod · 0.95
RunMethod · 0.45

Calls 5

IsPartialTraceMethod · 0.95
sizeErrorMethod · 0.95
NewTokenHasherFunction · 0.92
TokenForIDFunction · 0.92
tokenTypeAlias · 0.70

Tested by 1

BenchmarkCombineFunction · 0.76