Debian Codegraph @

connector/logs_router_test.go

raw
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/plog"
18 "go.opentelemetry.io/collector/pdata/testdata"
19 "go.opentelemetry.io/collector/pipeline"
20 )
21
22 type mutatingLogsSink struct {
23 *consumertest.LogsSink
24 }
25
26 func (mts *mutatingLogsSink) Capabilities() consumer.Capabilities {
27 return consumer.Capabilities{MutatesData: true}
28 }
29
30 func TestLogsRouterMultiplexing(t *testing.T) {
31 num := 20
32 for numIDs := 1; numIDs < num; numIDs++ {
33 for numCons := 1; numCons < num; numCons++ {
34 for numLogs := 1; numLogs < num; numLogs++ {
35 t.Run(
36 fmt.Sprintf("%d-ids/%d-cons/%d-logs", numIDs, numCons, numLogs),
37 fuzzLogs(numIDs, numCons, numLogs),
38 )
39 }
40 }
41 }
42 }
43
44 func fuzzLogs(numIDs, numCons, numLogs int) func(*testing.T) {
45 return func(t *testing.T) {
46 allIDs := make([]pipeline.ID, 0, numCons)
47 allCons := make([]consumer.Logs, 0, numCons)
48 allConsMap := make(map[pipeline.ID]consumer.Logs)
49
50 // If any consumer is mutating, the router must report mutating
51 for i := range numCons {
52 allIDs = append(allIDs, pipeline.NewIDWithName(pipeline.SignalLogs, "sink_"+strconv.Itoa(numCons)))
53 // Random chance for each consumer to be mutating
54 if (numCons+numLogs+i)%4 == 0 {
55 allCons = append(allCons, &mutatingLogsSink{LogsSink: new(consumertest.LogsSink)})
56 } else {
57 allCons = append(allCons, new(consumertest.LogsSink))
58 }
59 allConsMap[allIDs[i]] = allCons[i]
60 }
61
62 r := NewLogsRouter(allConsMap)
63 ld := testdata.GenerateLogs(1)
64
65 // Keep track of how many logs each consumer should receive.
66 // This will be validated after every call to RouteLogs.
67 expected := make(map[pipeline.ID]int, numCons)
68
69 for i := range numLogs {
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.ConsumeLogs(context.Background(), ld))
89
90 // Validate expectations for all consumers
91 for id := range expected {
92 logs := []plog.Logs{}
93 switch con := allConsMap[id].(type) {
94 case *consumertest.LogsSink:
95 logs = con.AllLogs()
96 case *mutatingLogsSink:
97 logs = con.AllLogs()
98 }
99 assert.Len(t, logs, expected[id])
100 for n := 0; n < len(logs); n++ {
101 assert.Equal(t, ld, logs[n])
102 }
103 }
104 }
105 }
106 }
107
108 func TestLogsRouterConsumers(t *testing.T) {
109 ctx := context.Background()
110 ld := testdata.GenerateLogs(1)
111
112 fooID := pipeline.NewIDWithName(pipeline.SignalLogs, "foo")
113 barID := pipeline.NewIDWithName(pipeline.SignalLogs, "bar")
114
115 foo := new(consumertest.LogsSink)
116 bar := new(consumertest.LogsSink)
117 r := NewLogsRouter(map[pipeline.ID]consumer.Logs{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.AllLogs())
124 assert.Empty(t, bar.AllLogs())
125
126 both, err := r.Consumer(fooID, barID)
127 assert.NotNil(t, both)
128 assert.NoError(t, err)
129
130 assert.NoError(t, both.ConsumeLogs(ctx, ld))
131 assert.Len(t, foo.AllLogs(), 1)
132 assert.Len(t, bar.AllLogs(), 1)
133
134 fooOnly, err := r.Consumer(fooID)
135 assert.NotNil(t, fooOnly)
136 assert.NoError(t, err)
137
138 assert.NoError(t, fooOnly.ConsumeLogs(ctx, ld))
139 assert.Len(t, foo.AllLogs(), 2)
140 assert.Len(t, bar.AllLogs(), 1)
141
142 barOnly, err := r.Consumer(barID)
143 assert.NotNil(t, barOnly)
144 assert.NoError(t, err)
145
146 assert.NoError(t, barOnly.ConsumeLogs(ctx, ld))
147 assert.Len(t, foo.AllLogs(), 2)
148 assert.Len(t, bar.AllLogs(), 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.SignalLogs, "fake"))
155 assert.Nil(t, fake)
156 assert.Error(t, err)
157 }