Debian Codegraph @

service/internal/graph/util_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package graph
5
6 import (
7 "context"
8 "errors"
9 "hash/fnv"
10 "sync"
11 "testing"
12
13 "github.com/stretchr/testify/require"
14
15 "go.opentelemetry.io/collector/component"
16 "go.opentelemetry.io/collector/connector"
17 "go.opentelemetry.io/collector/connector/xconnector"
18 "go.opentelemetry.io/collector/consumer"
19 "go.opentelemetry.io/collector/consumer/consumertest"
20 "go.opentelemetry.io/collector/consumer/xconsumer"
21 "go.opentelemetry.io/collector/exporter"
22 "go.opentelemetry.io/collector/exporter/xexporter"
23 "go.opentelemetry.io/collector/featuregate"
24 "go.opentelemetry.io/collector/internal/telemetry"
25 "go.opentelemetry.io/collector/pipeline"
26 "go.opentelemetry.io/collector/pipeline/xpipeline"
27 "go.opentelemetry.io/collector/processor"
28 "go.opentelemetry.io/collector/processor/xprocessor"
29 "go.opentelemetry.io/collector/receiver"
30 "go.opentelemetry.io/collector/receiver/xreceiver"
31 "go.opentelemetry.io/collector/service/pipelines"
32 )
33
34 var _ component.Component = (*testNode)(nil)
35
36 type testNode struct {
37 id component.ID
38 startErr error
39 shutdownErr error
40 }
41
42 // ID satisfies the graph.Node interface, allowing
43 // testNode to be used in a simple.DirectedGraph
44 func (n *testNode) ID() int64 {
45 h := fnv.New64a()
46 h.Write([]byte(n.id.String()))
47
48 // The graph identifies nodes by an int64 ID, but fnv gives us a uint64.
49 // It is safe to cast because the meaning of the number is irrelevant.
50 // We only care that each node has a unique 64 bit ID, which is unaltered by this cast.
51 return int64(h.Sum64()) // #nosec G115
52 }
53
54 func (n *testNode) Start(ctx context.Context, _ component.Host) error {
55 if n.startErr != nil {
56 return n.startErr
57 }
58 if cwo, ok := ctx.(*contextWithOrder); ok {
59 cwo.record(n.id)
60 }
61 return nil
62 }
63
64 func (n *testNode) Shutdown(ctx context.Context) error {
65 if n.shutdownErr != nil {
66 return n.shutdownErr
67 }
68 if cwo, ok := ctx.(*contextWithOrder); ok {
69 cwo.record(n.id)
70 }
71 return nil
72 }
73
74 type contextWithOrder struct {
75 context.Context
76 sync.Mutex
77 next int
78 order map[component.ID]int
79 }
80
81 func (c *contextWithOrder) record(id component.ID) {
82 c.Lock()
83 c.order[id] = c.next
84 c.next++
85 c.Unlock()
86 }
87
88 func (g *Graph) getReceivers() map[pipeline.Signal]map[component.ID]component.Component {
89 receiversMap := make(map[pipeline.Signal]map[component.ID]component.Component)
90 receiversMap[pipeline.SignalTraces] = make(map[component.ID]component.Component)
91 receiversMap[pipeline.SignalMetrics] = make(map[component.ID]component.Component)
92 receiversMap[pipeline.SignalLogs] = make(map[component.ID]component.Component)
93 receiversMap[xpipeline.SignalProfiles] = make(map[component.ID]component.Component)
94
95 for _, pg := range g.pipelines {
96 for _, rcvrNode := range pg.receivers {
97 rcvrOrConnNode := g.componentGraph.Node(rcvrNode.ID())
98 rcvrNode, ok := rcvrOrConnNode.(*receiverNode)
99 if !ok {
100 continue
101 }
102 receiversMap[rcvrNode.pipelineType][rcvrNode.componentID] = rcvrNode.Component
103 }
104 }
105 return receiversMap
106 }
107
108 // Calculates the expected number of receiver and exporter instances in the specified pipeline.
109 //
110 // Expect one instance of each receiver and exporter, unless it is a connector.
111 //
112 // For Connectors:
113 // - Let E equal the number of pipeline types in which the connector is used as an exporter.
114 // - Let R equal the number of pipeline types in which the connector is used as a receiver.
115 //
116 // Within the graph as a whole, we expect E*R instances, i.e. one per combination of data types.
117 //
118 // However, within an individual pipeline, we expect:
119 // - E instances of the connector as a receiver.
120 // - R instances of the connector as an exporter.
121 func expectedInstances(m pipelines.Config, pID pipeline.ID) (int, int) {
122 exConnectorType := component.MustNewType("exampleconnector")
123 var r, e int
124 for _, rID := range m[pID].Receivers {
125 if rID.Type() != exConnectorType {
126 r++
127 continue
128 }
129
130 // This is a connector. Count the pipeline types where it is an exporter.
131 typeMap := map[pipeline.Signal]bool{}
132 for pID, pCfg := range m {
133 for _, eID := range pCfg.Exporters {
134 if eID == rID {
135 typeMap[pID.Signal()] = true
136 }
137 }
138 }
139 r += len(typeMap)
140 }
141 for _, eID := range m[pID].Exporters {
142 if eID.Type() != exConnectorType {
143 e++
144 continue
145 }
146
147 // This is a connector. Count the pipeline types where it is a receiver.
148 typeMap := map[pipeline.Signal]bool{}
149 for pID, pCfg := range m {
150 for _, rID := range pCfg.Receivers {
151 if rID == eID {
152 typeMap[pID.Signal()] = true
153 }
154 }
155 }
156 e += len(typeMap)
157 }
158 return r, e
159 }
160
161 func newBadReceiverFactory() receiver.Factory {
162 return receiver.NewFactory(component.MustNewType("bf"), func() component.Config {
163 return &struct{}{}
164 })
165 }
166
167 func newBadProcessorFactory() processor.Factory {
168 return processor.NewFactory(component.MustNewType("bf"), func() component.Config {
169 return &struct{}{}
170 })
171 }
172
173 func newBadExporterFactory() exporter.Factory {
174 return exporter.NewFactory(component.MustNewType("bf"), func() component.Config {
175 return &struct{}{}
176 })
177 }
178
179 func newBadConnectorFactory() connector.Factory {
180 return connector.NewFactory(component.MustNewType("bf"), func() component.Config {
181 return &struct{}{}
182 })
183 }
184
185 func newErrReceiverFactory() receiver.Factory {
186 return xreceiver.NewFactory(component.MustNewType("err"),
187 func() component.Config { return &struct{}{} },
188 xreceiver.WithTraces(func(context.Context, receiver.Settings, component.Config, consumer.Traces) (receiver.Traces, error) {
189 return &errComponent{}, nil
190 }, component.StabilityLevelUndefined),
191 xreceiver.WithLogs(func(context.Context, receiver.Settings, component.Config, consumer.Logs) (receiver.Logs, error) {
192 return &errComponent{}, nil
193 }, component.StabilityLevelUndefined),
194 xreceiver.WithMetrics(func(context.Context, receiver.Settings, component.Config, consumer.Metrics) (receiver.Metrics, error) {
195 return &errComponent{}, nil
196 }, component.StabilityLevelUndefined),
197 xreceiver.WithProfiles(func(context.Context, receiver.Settings, component.Config, xconsumer.Profiles) (xreceiver.Profiles, error) {
198 return &errComponent{}, nil
199 }, component.StabilityLevelUndefined),
200 )
201 }
202
203 func newErrProcessorFactory() processor.Factory {
204 return xprocessor.NewFactory(component.MustNewType("err"),
205 func() component.Config { return &struct{}{} },
206 xprocessor.WithTraces(func(context.Context, processor.Settings, component.Config, consumer.Traces) (processor.Traces, error) {
207 return &errComponent{}, nil
208 }, component.StabilityLevelUndefined),
209 xprocessor.WithLogs(func(context.Context, processor.Settings, component.Config, consumer.Logs) (processor.Logs, error) {
210 return &errComponent{}, nil
211 }, component.StabilityLevelUndefined),
212 xprocessor.WithMetrics(func(context.Context, processor.Settings, component.Config, consumer.Metrics) (processor.Metrics, error) {
213 return &errComponent{}, nil
214 }, component.StabilityLevelUndefined),
215 xprocessor.WithProfiles(func(context.Context, processor.Settings, component.Config, xconsumer.Profiles) (xprocessor.Profiles, error) {
216 return &errComponent{}, nil
217 }, component.StabilityLevelUndefined),
218 )
219 }
220
221 func newErrExporterFactory() exporter.Factory {
222 return xexporter.NewFactory(component.MustNewType("err"),
223 func() component.Config { return &struct{}{} },
224 xexporter.WithTraces(func(context.Context, exporter.Settings, component.Config) (exporter.Traces, error) {
225 return &errComponent{}, nil
226 }, component.StabilityLevelUndefined),
227 xexporter.WithLogs(func(context.Context, exporter.Settings, component.Config) (exporter.Logs, error) {
228 return &errComponent{}, nil
229 }, component.StabilityLevelUndefined),
230 xexporter.WithMetrics(func(context.Context, exporter.Settings, component.Config) (exporter.Metrics, error) {
231 return &errComponent{}, nil
232 }, component.StabilityLevelUndefined),
233 xexporter.WithProfiles(func(context.Context, exporter.Settings, component.Config) (xexporter.Profiles, error) {
234 return &errComponent{}, nil
235 }, component.StabilityLevelUndefined),
236 )
237 }
238
239 func newErrConnectorFactory() connector.Factory {
240 return xconnector.NewFactory(component.MustNewType("err"), func() component.Config {
241 return &struct{}{}
242 },
243 xconnector.WithTracesToTraces(func(context.Context, connector.Settings, component.Config, consumer.Traces) (connector.Traces, error) {
244 return &errComponent{}, nil
245 }, component.StabilityLevelUnmaintained),
246 xconnector.WithTracesToMetrics(func(context.Context, connector.Settings, component.Config, consumer.Metrics) (connector.Traces, error) {
247 return &errComponent{}, nil
248 }, component.StabilityLevelUnmaintained),
249 xconnector.WithTracesToLogs(func(context.Context, connector.Settings, component.Config, consumer.Logs) (connector.Traces, error) {
250 return &errComponent{}, nil
251 }, component.StabilityLevelUnmaintained),
252 xconnector.WithTracesToProfiles(func(context.Context, connector.Settings, component.Config, xconsumer.Profiles) (connector.Traces, error) {
253 return &errComponent{}, nil
254 }, component.StabilityLevelUnmaintained),
255
256 xconnector.WithMetricsToTraces(func(context.Context, connector.Settings, component.Config, consumer.Traces) (connector.Metrics, error) {
257 return &errComponent{}, nil
258 }, component.StabilityLevelUnmaintained),
259 xconnector.WithMetricsToMetrics(func(context.Context, connector.Settings, component.Config, consumer.Metrics) (connector.Metrics, error) {
260 return &errComponent{}, nil
261 }, component.StabilityLevelUnmaintained),
262 xconnector.WithMetricsToLogs(func(context.Context, connector.Settings, component.Config, consumer.Logs) (connector.Metrics, error) {
263 return &errComponent{}, nil
264 }, component.StabilityLevelUnmaintained),
265 xconnector.WithMetricsToProfiles(func(context.Context, connector.Settings, component.Config, xconsumer.Profiles) (connector.Metrics, error) {
266 return &errComponent{}, nil
267 }, component.StabilityLevelUnmaintained),
268
269 xconnector.WithLogsToTraces(func(context.Context, connector.Settings, component.Config, consumer.Traces) (connector.Logs, error) {
270 return &errComponent{}, nil
271 }, component.StabilityLevelUnmaintained),
272 xconnector.WithLogsToMetrics(func(context.Context, connector.Settings, component.Config, consumer.Metrics) (connector.Logs, error) {
273 return &errComponent{}, nil
274 }, component.StabilityLevelUnmaintained),
275 xconnector.WithLogsToLogs(func(context.Context, connector.Settings, component.Config, consumer.Logs) (connector.Logs, error) {
276 return &errComponent{}, nil
277 }, component.StabilityLevelUnmaintained),
278 xconnector.WithLogsToProfiles(func(context.Context, connector.Settings, component.Config, xconsumer.Profiles) (connector.Logs, error) {
279 return &errComponent{}, nil
280 }, component.StabilityLevelUnmaintained),
281
282 xconnector.WithProfilesToTraces(func(context.Context, connector.Settings, component.Config, consumer.Traces) (xconnector.Profiles, error) {
283 return &errComponent{}, nil
284 }, component.StabilityLevelUnmaintained),
285 xconnector.WithProfilesToMetrics(func(context.Context, connector.Settings, component.Config, consumer.Metrics) (xconnector.Profiles, error) {
286 return &errComponent{}, nil
287 }, component.StabilityLevelUnmaintained),
288 xconnector.WithProfilesToLogs(func(context.Context, connector.Settings, component.Config, consumer.Logs) (xconnector.Profiles, error) {
289 return &errComponent{}, nil
290 }, component.StabilityLevelUnmaintained),
291 xconnector.WithProfilesToProfiles(func(context.Context, connector.Settings, component.Config, xconsumer.Profiles) (xconnector.Profiles, error) {
292 return &errComponent{}, nil
293 }, component.StabilityLevelUnmaintained),
294 )
295 }
296
297 type errComponent struct {
298 consumertest.Consumer
299 }
300
301 func (e errComponent) Capabilities() consumer.Capabilities {
302 return consumer.Capabilities{MutatesData: false}
303 }
304
305 func (e errComponent) Start(context.Context, component.Host) error {
306 return errors.New("my error")
307 }
308
309 func (e errComponent) Shutdown(context.Context) error {
310 return errors.New("my error")
311 }
312
313 func setObsConsumerGateForTest(t *testing.T, enabled bool) {
314 initial := telemetry.NewPipelineTelemetryGate.IsEnabled()
315 require.NoError(t, featuregate.GlobalRegistry().Set(telemetry.NewPipelineTelemetryGate.ID(), enabled))
316 t.Cleanup(func() {
317 require.NoError(t, featuregate.GlobalRegistry().Set(telemetry.NewPipelineTelemetryGate.ID(), initial))
318 })
319 }