Debian Codegraph @

connector/xconnector/profiles_router_test.go

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