forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
299 lines (269 loc) · 11.7 KB
/
util_test.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package graph
import (
"context"
"errors"
"sync"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componentprofiles"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/connector/connectorprofiles"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumerprofiles"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterprofiles"
"go.opentelemetry.io/collector/pipeline"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processorprofiles"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receiverprofiles"
"go.opentelemetry.io/collector/service/pipelines"
)
var _ component.Component = (*testNode)(nil)
type testNode struct {
id component.ID
startErr error
shutdownErr error
}
// ID satisfies the graph.Node interface, allowing
// testNode to be used in a simple.DirectedGraph
func (n *testNode) ID() int64 {
return int64(newNodeID(n.id.String()))
}
func (n *testNode) Start(ctx context.Context, _ component.Host) error {
if n.startErr != nil {
return n.startErr
}
if cwo, ok := ctx.(*contextWithOrder); ok {
cwo.record(n.id)
}
return nil
}
func (n *testNode) Shutdown(ctx context.Context) error {
if n.shutdownErr != nil {
return n.shutdownErr
}
if cwo, ok := ctx.(*contextWithOrder); ok {
cwo.record(n.id)
}
return nil
}
type contextWithOrder struct {
context.Context
sync.Mutex
next int
order map[component.ID]int
}
func (c *contextWithOrder) record(id component.ID) {
c.Lock()
c.order[id] = c.next
c.next++
c.Unlock()
}
func (g *Graph) getReceivers() map[pipeline.Signal]map[component.ID]component.Component {
receiversMap := make(map[pipeline.Signal]map[component.ID]component.Component)
receiversMap[pipeline.SignalTraces] = make(map[component.ID]component.Component)
receiversMap[pipeline.SignalMetrics] = make(map[component.ID]component.Component)
receiversMap[pipeline.SignalLogs] = make(map[component.ID]component.Component)
receiversMap[componentprofiles.SignalProfiles] = make(map[component.ID]component.Component)
for _, pg := range g.pipelines {
for _, rcvrNode := range pg.receivers {
rcvrOrConnNode := g.componentGraph.Node(rcvrNode.ID())
rcvrNode, ok := rcvrOrConnNode.(*receiverNode)
if !ok {
continue
}
receiversMap[rcvrNode.pipelineType][rcvrNode.componentID] = rcvrNode.Component
}
}
return receiversMap
}
// Calculates the expected number of receiver and exporter instances in the specified pipeline.
//
// Expect one instance of each receiver and exporter, unless it is a connector.
//
// For Connectors:
// - Let E equal the number of pipeline types in which the connector is used as an exporter.
// - Let R equal the number of pipeline types in which the connector is used as a receiver.
//
// Within the graph as a whole, we expect E*R instances, i.e. one per combination of data types.
//
// However, within an individual pipeline, we expect:
// - E instances of the connector as a receiver.
// - R instances of the connector as an exporter.
func expectedInstances(m pipelines.Config, pID pipeline.ID) (int, int) {
exConnectorType := component.MustNewType("exampleconnector")
var r, e int
for _, rID := range m[pID].Receivers {
if rID.Type() != exConnectorType {
r++
continue
}
// This is a connector. Count the pipeline types where it is an exporter.
typeMap := map[pipeline.Signal]bool{}
for pID, pCfg := range m {
for _, eID := range pCfg.Exporters {
if eID == rID {
typeMap[pID.Signal()] = true
}
}
}
r += len(typeMap)
}
for _, eID := range m[pID].Exporters {
if eID.Type() != exConnectorType {
e++
continue
}
// This is a connector. Count the pipeline types where it is a receiver.
typeMap := map[pipeline.Signal]bool{}
for pID, pCfg := range m {
for _, rID := range pCfg.Receivers {
if rID == eID {
typeMap[pID.Signal()] = true
}
}
}
e += len(typeMap)
}
return r, e
}
func newBadReceiverFactory() receiver.Factory {
return receiver.NewFactory(component.MustNewType("bf"), func() component.Config {
return &struct{}{}
})
}
func newBadProcessorFactory() processor.Factory {
return processor.NewFactory(component.MustNewType("bf"), func() component.Config {
return &struct{}{}
})
}
func newBadExporterFactory() exporter.Factory {
return exporter.NewFactory(component.MustNewType("bf"), func() component.Config {
return &struct{}{}
})
}
func newBadConnectorFactory() connector.Factory {
return connector.NewFactory(component.MustNewType("bf"), func() component.Config {
return &struct{}{}
})
}
func newErrReceiverFactory() receiver.Factory {
return receiverprofiles.NewFactory(component.MustNewType("err"),
func() component.Config { return &struct{}{} },
receiverprofiles.WithTraces(func(context.Context, receiver.Settings, component.Config, consumer.Traces) (receiver.Traces, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
receiverprofiles.WithLogs(func(context.Context, receiver.Settings, component.Config, consumer.Logs) (receiver.Logs, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
receiverprofiles.WithMetrics(func(context.Context, receiver.Settings, component.Config, consumer.Metrics) (receiver.Metrics, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
receiverprofiles.WithProfiles(func(context.Context, receiver.Settings, component.Config, consumerprofiles.Profiles) (receiverprofiles.Profiles, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
)
}
func newErrProcessorFactory() processor.Factory {
return processorprofiles.NewFactory(component.MustNewType("err"),
func() component.Config { return &struct{}{} },
processorprofiles.WithTraces(func(context.Context, processor.Settings, component.Config, consumer.Traces) (processor.Traces, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
processorprofiles.WithLogs(func(context.Context, processor.Settings, component.Config, consumer.Logs) (processor.Logs, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
processorprofiles.WithMetrics(func(context.Context, processor.Settings, component.Config, consumer.Metrics) (processor.Metrics, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
processorprofiles.WithProfiles(func(context.Context, processor.Settings, component.Config, consumerprofiles.Profiles) (processorprofiles.Profiles, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
)
}
func newErrExporterFactory() exporter.Factory {
return exporterprofiles.NewFactory(component.MustNewType("err"),
func() component.Config { return &struct{}{} },
exporterprofiles.WithTraces(func(context.Context, exporter.Settings, component.Config) (exporter.Traces, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
exporterprofiles.WithLogs(func(context.Context, exporter.Settings, component.Config) (exporter.Logs, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
exporterprofiles.WithMetrics(func(context.Context, exporter.Settings, component.Config) (exporter.Metrics, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
exporterprofiles.WithProfiles(func(context.Context, exporter.Settings, component.Config) (exporterprofiles.Profiles, error) {
return &errComponent{}, nil
}, component.StabilityLevelUndefined),
)
}
func newErrConnectorFactory() connector.Factory {
return connectorprofiles.NewFactory(component.MustNewType("err"), func() component.Config {
return &struct{}{}
},
connectorprofiles.WithTracesToTraces(func(context.Context, connector.Settings, component.Config, consumer.Traces) (connector.Traces, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithTracesToMetrics(func(context.Context, connector.Settings, component.Config, consumer.Metrics) (connector.Traces, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithTracesToLogs(func(context.Context, connector.Settings, component.Config, consumer.Logs) (connector.Traces, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithTracesToProfiles(func(context.Context, connector.Settings, component.Config, consumerprofiles.Profiles) (connector.Traces, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithMetricsToTraces(func(context.Context, connector.Settings, component.Config, consumer.Traces) (connector.Metrics, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithMetricsToMetrics(func(context.Context, connector.Settings, component.Config, consumer.Metrics) (connector.Metrics, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithMetricsToLogs(func(context.Context, connector.Settings, component.Config, consumer.Logs) (connector.Metrics, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithMetricsToProfiles(func(context.Context, connector.Settings, component.Config, consumerprofiles.Profiles) (connector.Metrics, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithLogsToTraces(func(context.Context, connector.Settings, component.Config, consumer.Traces) (connector.Logs, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithLogsToMetrics(func(context.Context, connector.Settings, component.Config, consumer.Metrics) (connector.Logs, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithLogsToLogs(func(context.Context, connector.Settings, component.Config, consumer.Logs) (connector.Logs, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithLogsToProfiles(func(context.Context, connector.Settings, component.Config, consumerprofiles.Profiles) (connector.Logs, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithProfilesToTraces(func(context.Context, connector.Settings, component.Config, consumer.Traces) (connectorprofiles.Profiles, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithProfilesToMetrics(func(context.Context, connector.Settings, component.Config, consumer.Metrics) (connectorprofiles.Profiles, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithProfilesToLogs(func(context.Context, connector.Settings, component.Config, consumer.Logs) (connectorprofiles.Profiles, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
connectorprofiles.WithProfilesToProfiles(func(context.Context, connector.Settings, component.Config, consumerprofiles.Profiles) (connectorprofiles.Profiles, error) {
return &errComponent{}, nil
}, component.StabilityLevelUnmaintained),
)
}
type errComponent struct {
consumertest.Consumer
}
func (e errComponent) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: false}
}
func (e errComponent) Start(context.Context, component.Host) error {
return errors.New("my error")
}
func (e errComponent) Shutdown(context.Context) error {
return errors.New("my error")
}