/
example_factories.go
493 lines (414 loc) · 16.4 KB
/
example_factories.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package componenttest
import (
"context"
"fmt"
"go.uber.org/zap"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configerror"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumerdata"
"go.opentelemetry.io/collector/internal/data"
)
// ExampleReceiver is for testing purposes. We are defining an example config and factory
// for "examplereceiver" receiver type.
type ExampleReceiver struct {
configmodels.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
// Configures the receiver server protocol.
confignet.TCPAddr `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
ExtraSetting string `mapstructure:"extra"`
ExtraMapSetting map[string]string `mapstructure:"extra_map"`
ExtraListSetting []string `mapstructure:"extra_list"`
// FailTraceCreation causes CreateTraceReceiver to fail. Useful for testing.
FailTraceCreation bool `mapstructure:"-"`
// FailMetricsCreation causes CreateTraceReceiver to fail. Useful for testing.
FailMetricsCreation bool `mapstructure:"-"`
}
// ExampleReceiverFactory is factory for ExampleReceiver.
type ExampleReceiverFactory struct {
}
// Type gets the type of the Receiver config created by this factory.
func (f *ExampleReceiverFactory) Type() configmodels.Type {
return "examplereceiver"
}
// CustomUnmarshaler returns nil because we don't need custom unmarshaling for this factory.
func (f *ExampleReceiverFactory) CustomUnmarshaler() component.CustomUnmarshaler {
return nil
}
// CreateDefaultConfig creates the default configuration for the Receiver.
func (f *ExampleReceiverFactory) CreateDefaultConfig() configmodels.Receiver {
return &ExampleReceiver{
ReceiverSettings: configmodels.ReceiverSettings{
TypeVal: f.Type(),
NameVal: string(f.Type()),
},
TCPAddr: confignet.TCPAddr{
Endpoint: "localhost:1000",
},
ExtraSetting: "some string",
ExtraMapSetting: nil,
ExtraListSetting: nil,
}
}
// CreateTraceReceiver creates a trace receiver based on this config.
func (f *ExampleReceiverFactory) CreateTraceReceiver(
_ context.Context,
_ *zap.Logger,
cfg configmodels.Receiver,
nextConsumer consumer.TraceConsumerOld,
) (component.TraceReceiver, error) {
if cfg.(*ExampleReceiver).FailTraceCreation {
return nil, configerror.ErrDataTypeIsNotSupported
}
receiver := f.createReceiver(cfg)
receiver.TraceConsumer = nextConsumer
return receiver, nil
}
func (f *ExampleReceiverFactory) createReceiver(cfg configmodels.Receiver) *ExampleReceiverProducer {
// There must be one receiver for all data types. We maintain a map of
// receivers per config.
// Check to see if there is already a receiver for this config.
receiver, ok := exampleReceivers[cfg]
if !ok {
receiver = &ExampleReceiverProducer{}
// Remember the receiver in the map
exampleReceivers[cfg] = receiver
}
return receiver
}
// CreateMetricsReceiver creates a metrics receiver based on this config.
func (f *ExampleReceiverFactory) CreateMetricsReceiver(_ context.Context, _ *zap.Logger, cfg configmodels.Receiver, nextConsumer consumer.MetricsConsumerOld) (component.MetricsReceiver, error) {
if cfg.(*ExampleReceiver).FailMetricsCreation {
return nil, configerror.ErrDataTypeIsNotSupported
}
receiver := f.createReceiver(cfg)
receiver.MetricsConsumer = nextConsumer
return receiver, nil
}
func (f *ExampleReceiverFactory) CreateLogsReceiver(
_ context.Context,
_ component.ReceiverCreateParams,
cfg configmodels.Receiver,
nextConsumer consumer.LogsConsumer,
) (component.LogsReceiver, error) {
receiver := f.createReceiver(cfg)
receiver.LogConsumer = nextConsumer
return receiver, nil
}
// ExampleReceiverProducer allows producing traces and metrics for testing purposes.
type ExampleReceiverProducer struct {
Started bool
Stopped bool
TraceConsumer consumer.TraceConsumerOld
MetricsConsumer consumer.MetricsConsumerOld
LogConsumer consumer.LogsConsumer
}
// Start tells the receiver to start its processing.
func (erp *ExampleReceiverProducer) Start(_ context.Context, _ component.Host) error {
erp.Started = true
return nil
}
// Shutdown tells the receiver that should stop reception,
func (erp *ExampleReceiverProducer) Shutdown(context.Context) error {
erp.Stopped = true
return nil
}
// This is the map of already created example receivers for particular configurations.
// We maintain this map because the ReceiverFactoryBase is asked trace and metric receivers separately
// when it gets CreateTraceReceiver() and CreateMetricsReceiver() but they must not
// create separate objects, they must use one Receiver object per configuration.
var exampleReceivers = map[configmodels.Receiver]*ExampleReceiverProducer{}
// MultiProtoReceiver is for testing purposes. We are defining an example multi protocol
// config and factory for "multireceiver" receiver type.
type MultiProtoReceiver struct {
TypeVal configmodels.Type `mapstructure:"-"`
NameVal string `mapstructure:"-"`
Protocols map[string]MultiProtoReceiverOneCfg `mapstructure:"protocols"`
}
var _ configmodels.Receiver = (*MultiProtoReceiver)(nil)
// Name gets the exporter name.
func (rs *MultiProtoReceiver) Name() string {
return rs.NameVal
}
// SetName sets the receiver name.
func (rs *MultiProtoReceiver) SetName(name string) {
rs.NameVal = name
}
// Type sets the receiver type.
func (rs *MultiProtoReceiver) Type() configmodels.Type {
return rs.TypeVal
}
// SetType sets the receiver type.
func (rs *MultiProtoReceiver) SetType(typeStr configmodels.Type) {
rs.TypeVal = typeStr
}
// MultiProtoReceiverOneCfg is multi proto receiver config.
type MultiProtoReceiverOneCfg struct {
Endpoint string `mapstructure:"endpoint"`
ExtraSetting string `mapstructure:"extra"`
}
// MultiProtoReceiverFactory is factory for MultiProtoReceiver.
type MultiProtoReceiverFactory struct {
}
// Type gets the type of the Receiver config created by this factory.
func (f *MultiProtoReceiverFactory) Type() configmodels.Type {
return "multireceiver"
}
// CustomUnmarshaler returns nil because we don't need custom unmarshaling for this factory.
func (f *MultiProtoReceiverFactory) CustomUnmarshaler() component.CustomUnmarshaler {
return nil
}
// CreateDefaultConfig creates the default configuration for the Receiver.
func (f *MultiProtoReceiverFactory) CreateDefaultConfig() configmodels.Receiver {
return &MultiProtoReceiver{
TypeVal: f.Type(),
NameVal: string(f.Type()),
Protocols: map[string]MultiProtoReceiverOneCfg{
"http": {
Endpoint: "example.com:8888",
ExtraSetting: "extra string 1",
},
"tcp": {
Endpoint: "omnition.com:9999",
ExtraSetting: "extra string 2",
},
},
}
}
// CreateTraceReceiver creates a trace receiver based on this config.
func (f *MultiProtoReceiverFactory) CreateTraceReceiver(
_ context.Context,
_ *zap.Logger,
_ configmodels.Receiver,
_ consumer.TraceConsumerOld,
) (component.TraceReceiver, error) {
// Not used for this test, just return nil
return nil, nil
}
// CreateMetricsReceiver creates a metrics receiver based on this config.
func (f *MultiProtoReceiverFactory) CreateMetricsReceiver(_ context.Context, _ *zap.Logger, _ configmodels.Receiver, _ consumer.MetricsConsumerOld) (component.MetricsReceiver, error) {
// Not used for this test, just return nil
return nil, nil
}
// ExampleExporter is for testing purposes. We are defining an example config and factory
// for "exampleexporter" exporter type.
type ExampleExporter struct {
configmodels.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
ExtraInt int32 `mapstructure:"extra_int"`
ExtraSetting string `mapstructure:"extra"`
ExtraMapSetting map[string]string `mapstructure:"extra_map"`
ExtraListSetting []string `mapstructure:"extra_list"`
}
// ExampleExporterFactory is factory for ExampleExporter.
type ExampleExporterFactory struct {
}
// Type gets the type of the Exporter config created by this factory.
func (f *ExampleExporterFactory) Type() configmodels.Type {
return "exampleexporter"
}
// CreateDefaultConfig creates the default configuration for the Exporter.
func (f *ExampleExporterFactory) CreateDefaultConfig() configmodels.Exporter {
return &ExampleExporter{
ExporterSettings: configmodels.ExporterSettings{
TypeVal: f.Type(),
NameVal: string(f.Type()),
},
ExtraSetting: "some export string",
ExtraMapSetting: nil,
ExtraListSetting: nil,
}
}
// CreateTraceExporter creates a trace exporter based on this config.
func (f *ExampleExporterFactory) CreateTraceExporter(_ *zap.Logger, _ configmodels.Exporter) (component.TraceExporterOld, error) {
return &ExampleExporterConsumer{}, nil
}
// CreateMetricsExporter creates a metrics exporter based on this config.
func (f *ExampleExporterFactory) CreateMetricsExporter(_ *zap.Logger, _ configmodels.Exporter) (component.MetricsExporterOld, error) {
return &ExampleExporterConsumer{}, nil
}
func (f *ExampleExporterFactory) CreateLogsExporter(
_ context.Context,
_ component.ExporterCreateParams,
_ configmodels.Exporter,
) (component.LogsExporter, error) {
return &ExampleExporterConsumer{}, nil
}
// ExampleExporterConsumer stores consumed traces and metrics for testing purposes.
type ExampleExporterConsumer struct {
Traces []consumerdata.TraceData
Metrics []consumerdata.MetricsData
Logs []data.Logs
ExporterStarted bool
ExporterShutdown bool
}
// Start tells the exporter to start. The exporter may prepare for exporting
// by connecting to the endpoint. Host parameter can be used for communicating
// with the host after Start() has already returned.
func (exp *ExampleExporterConsumer) Start(_ context.Context, _ component.Host) error {
exp.ExporterStarted = true
return nil
}
// ConsumeTraceData receives consumerdata.TraceData for processing by the TraceConsumer.
func (exp *ExampleExporterConsumer) ConsumeTraceData(_ context.Context, td consumerdata.TraceData) error {
exp.Traces = append(exp.Traces, td)
return nil
}
// ConsumeMetricsData receives consumerdata.MetricsData for processing by the MetricsConsumer.
func (exp *ExampleExporterConsumer) ConsumeMetricsData(_ context.Context, md consumerdata.MetricsData) error {
exp.Metrics = append(exp.Metrics, md)
return nil
}
func (exp *ExampleExporterConsumer) ConsumeLogs(_ context.Context, ld data.Logs) error {
exp.Logs = append(exp.Logs, ld)
return nil
}
// Name returns the name of the exporter.
func (exp *ExampleExporterConsumer) Name() string {
return "exampleexporter"
}
// Shutdown is invoked during shutdown.
func (exp *ExampleExporterConsumer) Shutdown(context.Context) error {
exp.ExporterShutdown = true
return nil
}
// ExampleProcessorCfg is for testing purposes. We are defining an example config and factory
// for "exampleprocessor" processor type.
type ExampleProcessorCfg struct {
configmodels.ProcessorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
ExtraSetting string `mapstructure:"extra"`
ExtraMapSetting map[string]string `mapstructure:"extra_map"`
ExtraListSetting []string `mapstructure:"extra_list"`
}
// ExampleProcessorFactory is factory for ExampleProcessor.
type ExampleProcessorFactory struct {
}
// Type gets the type of the Processor config created by this factory.
func (f *ExampleProcessorFactory) Type() configmodels.Type {
return "exampleprocessor"
}
// CreateDefaultConfig creates the default configuration for the Processor.
func (f *ExampleProcessorFactory) CreateDefaultConfig() configmodels.Processor {
return &ExampleProcessorCfg{
ProcessorSettings: configmodels.ProcessorSettings{
TypeVal: f.Type(),
NameVal: string(f.Type()),
},
ExtraSetting: "some export string",
ExtraMapSetting: nil,
ExtraListSetting: nil,
}
}
// CreateTraceProcessor creates a trace processor based on this config.
func (f *ExampleProcessorFactory) CreateTraceProcessor(
_ *zap.Logger,
_ consumer.TraceConsumerOld,
_ configmodels.Processor,
) (component.TraceProcessorOld, error) {
return nil, configerror.ErrDataTypeIsNotSupported
}
// CreateMetricsProcessor creates a metrics processor based on this config.
func (f *ExampleProcessorFactory) CreateMetricsProcessor(
_ *zap.Logger,
_ consumer.MetricsConsumerOld,
_ configmodels.Processor,
) (component.MetricsProcessorOld, error) {
return nil, configerror.ErrDataTypeIsNotSupported
}
func (f *ExampleProcessorFactory) CreateLogsProcessor(
_ context.Context,
_ component.ProcessorCreateParams,
_ configmodels.Processor,
nextConsumer consumer.LogsConsumer,
) (component.LogsProcessor, error) {
return &ExampleProcessor{nextConsumer}, nil
}
type ExampleProcessor struct {
nextConsumer consumer.LogsConsumer
}
func (ep *ExampleProcessor) Start(_ context.Context, _ component.Host) error {
return nil
}
func (ep *ExampleProcessor) Shutdown(_ context.Context) error {
return nil
}
func (ep *ExampleProcessor) GetCapabilities() component.ProcessorCapabilities {
return component.ProcessorCapabilities{MutatesConsumedData: false}
}
func (ep *ExampleProcessor) ConsumeLogs(ctx context.Context, ld data.Logs) error {
return ep.nextConsumer.ConsumeLogs(ctx, ld)
}
// ExampleExtensionCfg is for testing purposes. We are defining an example config and factory
// for "exampleextension" extension type.
type ExampleExtensionCfg struct {
configmodels.ExtensionSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
ExtraSetting string `mapstructure:"extra"`
ExtraMapSetting map[string]string `mapstructure:"extra_map"`
ExtraListSetting []string `mapstructure:"extra_list"`
}
type ExampleExtension struct {
}
func (e *ExampleExtension) Start(_ context.Context, _ component.Host) error { return nil }
func (e *ExampleExtension) Shutdown(_ context.Context) error { return nil }
// ExampleExtensionFactory is factory for ExampleExtensionCfg.
type ExampleExtensionFactory struct {
FailCreation bool
}
// Type gets the type of the Extension config created by this factory.
func (f *ExampleExtensionFactory) Type() configmodels.Type {
return "exampleextension"
}
// CreateDefaultConfig creates the default configuration for the Extension.
func (f *ExampleExtensionFactory) CreateDefaultConfig() configmodels.Extension {
return &ExampleExtensionCfg{
ExtensionSettings: configmodels.ExtensionSettings{
TypeVal: f.Type(),
NameVal: string(f.Type()),
},
ExtraSetting: "extra string setting",
ExtraMapSetting: nil,
ExtraListSetting: nil,
}
}
// CreateExtension creates an Extension based on this config.
func (f *ExampleExtensionFactory) CreateExtension(_ context.Context, _ component.ExtensionCreateParams, _ configmodels.Extension) (component.ServiceExtension, error) {
if f.FailCreation {
return nil, fmt.Errorf("cannot create %q extension type", f.Type())
}
return &ExampleExtension{}, nil
}
// ExampleComponents registers example factories. This is only used by tests.
func ExampleComponents() (
factories component.Factories,
err error,
) {
if factories.Extensions, err = component.MakeExtensionFactoryMap(&ExampleExtensionFactory{}); err != nil {
return
}
factories.Receivers, err = component.MakeReceiverFactoryMap(
&ExampleReceiverFactory{},
&MultiProtoReceiverFactory{},
)
if err != nil {
return
}
factories.Exporters, err = component.MakeExporterFactoryMap(&ExampleExporterFactory{})
if err != nil {
return
}
factories.Processors, err = component.MakeProcessorFactoryMap(&ExampleProcessorFactory{})
return
}