Debian Codegraph @

connector/traces_router_test.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package connector
5
6 import (
7 "context"
8 "fmt"
9 "strconv"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14
15 "go.opentelemetry.io/collector/consumer"
16 "go.opentelemetry.io/collector/consumer/consumertest"
17 "go.opentelemetry.io/collector/pdata/ptrace"
18 "go.opentelemetry.io/collector/pdata/testdata"
19 "go.opentelemetry.io/collector/pipeline"
20 )
21
22 type mutatingTracesSink struct {
23 *consumertest.TracesSink
24 }
25
26 func (mts *mutatingTracesSink) Capabilities() consumer.Capabilities {
27 return consumer.Capabilities{MutatesData: true}
28 }
29
30 func TestTracesRouterMultiplexing(t *testing.T) {
31 num := 20
32 for numIDs := 1; numIDs < num; numIDs++ {
33 for numCons := 1; numCons < num; numCons++ {
34 for numTraces := 1; numTraces < num; numTraces++ {
35 t.Run(
36 fmt.Sprintf("%d-ids/%d-cons/%d-logs", numIDs, numCons, numTraces),
37 fuzzTraces(numIDs, numCons, numTraces),
38 )
39 }
40 }
41 }
42 }
43
44 func fuzzTraces(numIDs, numCons, numTraces int) func(*testing.T) {
45 return func(t *testing.T) {
46 allIDs := make([]pipeline.ID, 0, numCons)
47 allCons := make([]consumer.Traces, 0, numCons)
48 allConsMap := make(map[pipeline.ID]consumer.Traces)
49
50 // If any consumer is mutating, the router must report mutating
51 for i := range numCons {
52 allIDs = append(allIDs, pipeline.NewIDWithName(pipeline.SignalTraces, "sink_"+strconv.Itoa(numCons)))
53 // Random chance for each consumer to be mutating
54 if (numCons+numTraces+i)%4 == 0 {
55 allCons = append(allCons, &mutatingTracesSink{TracesSink: new(consumertest.TracesSink)})
56 } else {
57 allCons = append(allCons, new(consumertest.TracesSink))
58 }
59 allConsMap[allIDs[i]] = allCons[i]
60 }
61
62 r := NewTracesRouter(allConsMap)
63 td := testdata.GenerateTraces(1)
64
65 // Keep track of how many logs each consumer should receive.
66 // This will be validated after every call to RouteTraces.
67 expected := make(map[pipeline.ID]int, numCons)
68
69 for i := range numTraces {
70 // Build a random set of ids (no duplicates)
71 randCons := make(map[pipeline.ID]bool, numIDs)
72 for j := range numIDs {
73 // This number should be pretty random and less than numCons
74 conNum := (numCons + numIDs + i + j) % numCons
75 randCons[allIDs[conNum]] = true
76 }
77
78 // Convert to slice, update expectations
79 conIDs := make([]pipeline.ID, 0, len(randCons))
80 for id := range randCons {
81 conIDs = append(conIDs, id)
82 expected[id]++
83 }
84
85 // Route to list of consumers
86 fanout, err := r.Consumer(conIDs...)
87 assert.NoError(t, err)
88 assert.NoError(t, fanout.ConsumeTraces(context.Background(), td))
89
90 // Validate expectations for all consumers
91 for id := range expected {
92 traces := []ptrace.Traces{}
93 switch con := allConsMap[id].(type) {
94 case *consumertest.TracesSink:
95 traces = con.AllTraces()
96 case *mutatingTracesSink:
97 traces = con.AllTraces()
98 }
99 assert.Len(t, traces, expected[id])
100 for n := 0; n < len(traces); n++ {
101 assert.Equal(t, td, traces[n])
102 }
103 }
104 }
105 }
106 }
107
108 func TestTracesRouterConsumer(t *testing.T) {
109 ctx := context.Background()
110 td := testdata.GenerateTraces(1)
111
112 fooID := pipeline.NewIDWithName(pipeline.SignalTraces, "foo")
113 barID := pipeline.NewIDWithName(pipeline.SignalTraces, "bar")
114
115 foo := new(consumertest.TracesSink)
116 bar := new(consumertest.TracesSink)
117 r := NewTracesRouter(map[pipeline.ID]consumer.Traces{fooID: foo, barID: bar})
118
119 rcs := r.PipelineIDs()
120 assert.Len(t, rcs, 2)
121 assert.ElementsMatch(t, []pipeline.ID{fooID, barID}, rcs)
122
123 assert.Empty(t, foo.AllTraces())
124 assert.Empty(t, bar.AllTraces())
125
126 both, err := r.Consumer(fooID, barID)
127 assert.NotNil(t, both)
128 assert.NoError(t, err)
129
130 assert.NoError(t, both.ConsumeTraces(ctx, td))
131 assert.Len(t, foo.AllTraces(), 1)
132 assert.Len(t, bar.AllTraces(), 1)
133
134 fooOnly, err := r.Consumer(fooID)
135 assert.NotNil(t, fooOnly)
136 assert.NoError(t, err)
137
138 assert.NoError(t, fooOnly.ConsumeTraces(ctx, td))
139 assert.Len(t, foo.AllTraces(), 2)
140 assert.Len(t, bar.AllTraces(), 1)
141
142 barOnly, err := r.Consumer(barID)
143 assert.NotNil(t, barOnly)
144 assert.NoError(t, err)
145
146 assert.NoError(t, barOnly.ConsumeTraces(ctx, td))
147 assert.Len(t, foo.AllTraces(), 2)
148 assert.Len(t, bar.AllTraces(), 2)
149
150 none, err := r.Consumer()
151 assert.Nil(t, none)
152 require.Error(t, err)
153
154 fake, err := r.Consumer(pipeline.NewIDWithName(pipeline.SignalTraces, "fake"))
155 assert.Nil(t, fake)
156 assert.Error(t, err)
157 }