-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
traces.go
81 lines (68 loc) · 2.72 KB
/
traces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package fanoutconsumer // import "go.opentelemetry.io/collector/internal/fanoutconsumer"
import (
"context"
"go.uber.org/multierr"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/ptrace"
)
// NewTraces wraps multiple trace consumers in a single one.
// It fanouts the incoming data to all the consumers, and does smart routing:
// - Clones only to the consumer that needs to mutate the data.
// - If all consumers needs to mutate the data one will get the original mutable data.
func NewTraces(tcs []consumer.Traces) consumer.Traces {
// Don't wrap if there is only one non-mutating consumer.
if len(tcs) == 1 && !tcs[0].Capabilities().MutatesData {
return tcs[0]
}
tc := &tracesConsumer{}
for i := 0; i < len(tcs); i++ {
if tcs[i].Capabilities().MutatesData {
tc.mutable = append(tc.mutable, tcs[i])
} else {
tc.readonly = append(tc.readonly, tcs[i])
}
}
return tc
}
type tracesConsumer struct {
mutable []consumer.Traces
readonly []consumer.Traces
}
func (tsc *tracesConsumer) Capabilities() consumer.Capabilities {
// If all consumers are mutating, then the original data will be passed to one of them.
return consumer.Capabilities{MutatesData: len(tsc.mutable) > 0 && len(tsc.readonly) == 0}
}
// ConsumeTraces exports the ptrace.Traces to all consumers wrapped by the current one.
func (tsc *tracesConsumer) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
var errs error
if len(tsc.mutable) > 0 {
// Clone the data before sending to all mutating consumers except the last one.
for i := 0; i < len(tsc.mutable)-1; i++ {
errs = multierr.Append(errs, tsc.mutable[i].ConsumeTraces(ctx, cloneTraces(td)))
}
// Send data as is to the last mutating consumer only if there are no other non-mutating consumers and the
// data is mutable. Never share the same data between a mutating and a non-mutating consumer since the
// non-mutating consumer may process data async and the mutating consumer may change the data before that.
lastConsumer := tsc.mutable[len(tsc.mutable)-1]
if len(tsc.readonly) == 0 && !td.IsReadOnly() {
errs = multierr.Append(errs, lastConsumer.ConsumeTraces(ctx, td))
} else {
errs = multierr.Append(errs, lastConsumer.ConsumeTraces(ctx, cloneTraces(td)))
}
}
// Mark the data as read-only if it will be sent to more than one read-only consumer.
if len(tsc.readonly) > 1 && !td.IsReadOnly() {
td.MarkReadOnly()
}
for _, tc := range tsc.readonly {
errs = multierr.Append(errs, tc.ConsumeTraces(ctx, td))
}
return errs
}
func cloneTraces(td ptrace.Traces) ptrace.Traces {
clonedTraces := ptrace.NewTraces()
td.CopyTo(clonedTraces)
return clonedTraces
}