diff --git a/internal/testcomponents/example_exporter.go b/internal/testcomponents/example_exporter.go index 0d6c3647b71..52be1633a35 100644 --- a/internal/testcomponents/example_exporter.go +++ b/internal/testcomponents/example_exporter.go @@ -25,14 +25,13 @@ import ( "go.opentelemetry.io/collector/pdata/ptrace" ) -// ExampleExporter is for testing purposes. We are defining an example config and factory -// for "exampleexporter" exporter type. -type ExampleExporter struct { +const expType = "exampleexporter" + +// ExampleExporterConfig config for ExampleExporter. +type ExampleExporterConfig struct { config.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct } -const expType = "exampleexporter" - // ExampleExporterFactory is factory for ExampleExporter. var ExampleExporterFactory = component.NewExporterFactory( expType, @@ -41,65 +40,64 @@ var ExampleExporterFactory = component.NewExporterFactory( component.WithMetricsExporter(createMetricsExporter), component.WithLogsExporter(createLogsExporter)) -// CreateDefaultConfig creates the default configuration for the Exporter. func createExporterDefaultConfig() config.Exporter { - return &ExampleExporter{ + return &ExampleExporterConfig{ ExporterSettings: config.NewExporterSettings(config.NewComponentID(expType)), } } func createTracesExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.TracesExporter, error) { - return &ExampleExporterConsumer{}, nil + return &ExampleExporter{}, nil } func createMetricsExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.MetricsExporter, error) { - return &ExampleExporterConsumer{}, nil + return &ExampleExporter{}, nil } func createLogsExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.LogsExporter, error) { - return &ExampleExporterConsumer{}, nil + return &ExampleExporter{}, nil } -// ExampleExporterConsumer stores consumed traces and metrics for testing purposes. -type ExampleExporterConsumer struct { - Traces []ptrace.Traces - Metrics []pmetric.Metrics - Logs []plog.Logs - ExporterStarted bool - ExporterShutdown bool +// ExampleExporter stores consumed traces and metrics for testing purposes. +type ExampleExporter struct { + Traces []ptrace.Traces + Metrics []pmetric.Metrics + Logs []plog.Logs + Started bool + Stopped 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 +func (exp *ExampleExporter) Start(_ context.Context, _ component.Host) error { + exp.Started = true return nil } // ConsumeTraces receives ptrace.Traces for processing by the consumer.Traces. -func (exp *ExampleExporterConsumer) ConsumeTraces(_ context.Context, td ptrace.Traces) error { +func (exp *ExampleExporter) ConsumeTraces(_ context.Context, td ptrace.Traces) error { exp.Traces = append(exp.Traces, td) return nil } -func (exp *ExampleExporterConsumer) Capabilities() consumer.Capabilities { +func (exp *ExampleExporter) Capabilities() consumer.Capabilities { return consumer.Capabilities{MutatesData: false} } // ConsumeMetrics receives pmetric.Metrics for processing by the Metrics. -func (exp *ExampleExporterConsumer) ConsumeMetrics(_ context.Context, md pmetric.Metrics) error { +func (exp *ExampleExporter) ConsumeMetrics(_ context.Context, md pmetric.Metrics) error { exp.Metrics = append(exp.Metrics, md) return nil } -func (exp *ExampleExporterConsumer) ConsumeLogs(_ context.Context, ld plog.Logs) error { +func (exp *ExampleExporter) ConsumeLogs(_ context.Context, ld plog.Logs) error { exp.Logs = append(exp.Logs, ld) return nil } // Shutdown is invoked during shutdown. -func (exp *ExampleExporterConsumer) Shutdown(context.Context) error { - exp.ExporterShutdown = true +func (exp *ExampleExporter) Shutdown(context.Context) error { + exp.Stopped = true return nil } diff --git a/internal/testcomponents/example_exporter_test.go b/internal/testcomponents/example_exporter_test.go index 3a772d15f7f..ddc94f6d7a5 100644 --- a/internal/testcomponents/example_exporter_test.go +++ b/internal/testcomponents/example_exporter_test.go @@ -25,26 +25,22 @@ import ( "go.opentelemetry.io/collector/pdata/ptrace" ) -func TestExampleExporterConsumer(t *testing.T) { - exp := &ExampleExporterConsumer{} +func TestExampleExporter(t *testing.T) { + exp := &ExampleExporter{} host := componenttest.NewNopHost() - assert.False(t, exp.ExporterStarted) - err := exp.Start(context.Background(), host) - assert.NoError(t, err) - assert.True(t, exp.ExporterStarted) + assert.False(t, exp.Started) + assert.NoError(t, exp.Start(context.Background(), host)) + assert.True(t, exp.Started) assert.Equal(t, 0, len(exp.Traces)) - err = exp.ConsumeTraces(context.Background(), ptrace.Traces{}) - assert.NoError(t, err) + assert.NoError(t, exp.ConsumeTraces(context.Background(), ptrace.Traces{})) assert.Equal(t, 1, len(exp.Traces)) assert.Equal(t, 0, len(exp.Metrics)) - err = exp.ConsumeMetrics(context.Background(), pmetric.Metrics{}) - assert.NoError(t, err) + assert.NoError(t, exp.ConsumeMetrics(context.Background(), pmetric.Metrics{})) assert.Equal(t, 1, len(exp.Metrics)) - assert.False(t, exp.ExporterShutdown) - err = exp.Shutdown(context.Background()) - assert.NoError(t, err) - assert.True(t, exp.ExporterShutdown) + assert.False(t, exp.Stopped) + assert.NoError(t, exp.Shutdown(context.Background())) + assert.True(t, exp.Stopped) } diff --git a/internal/testcomponents/example_factories.go b/internal/testcomponents/example_factories.go index 88dc80758e9..38f654bb901 100644 --- a/internal/testcomponents/example_factories.go +++ b/internal/testcomponents/example_factories.go @@ -16,22 +16,20 @@ package testcomponents // import "go.opentelemetry.io/collector/internal/testcom import ( "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/config" ) // ExampleComponents registers example factories. This is only used by tests. -func ExampleComponents() ( - factories component.Factories, - err error, -) { - if factories.Receivers, err = component.MakeReceiverFactoryMap(ExampleReceiverFactory); err != nil { - return - } - - if factories.Exporters, err = component.MakeExporterFactoryMap(ExampleExporterFactory); err != nil { - return - } - - factories.Processors, err = component.MakeProcessorFactoryMap(ExampleProcessorFactory) - - return +func ExampleComponents() (component.Factories, error) { + return component.Factories{ + Receivers: map[config.Type]component.ReceiverFactory{ + ExampleReceiverFactory.Type(): ExampleReceiverFactory, + }, + Processors: map[config.Type]component.ProcessorFactory{ + ExampleProcessorFactory.Type(): ExampleProcessorFactory, + }, + Exporters: map[config.Type]component.ExporterFactory{ + ExampleExporterFactory.Type(): ExampleExporterFactory, + }, + }, nil } diff --git a/internal/testcomponents/example_processor.go b/internal/testcomponents/example_processor.go index f8f4a55e483..601454ed894 100644 --- a/internal/testcomponents/example_processor.go +++ b/internal/testcomponents/example_processor.go @@ -22,15 +22,14 @@ import ( "go.opentelemetry.io/collector/consumer" ) -// ExampleProcessorCfg is for testing purposes. We are defining an example config and factory -// for "exampleprocessor" processor type. -type ExampleProcessorCfg struct { +const procType = "exampleprocessor" + +// ExampleProcessorConfig config for ExampleProcessor. +type ExampleProcessorConfig struct { config.ProcessorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct } -const procType = "exampleprocessor" - -// ExampleProcessorFactory is factory for exampleProcessor. +// ExampleProcessorFactory is factory for ExampleProcessor. var ExampleProcessorFactory = component.NewProcessorFactory( procType, createDefaultConfig, @@ -40,37 +39,41 @@ var ExampleProcessorFactory = component.NewProcessorFactory( // CreateDefaultConfig creates the default configuration for the Processor. func createDefaultConfig() config.Processor { - return &ExampleProcessorCfg{ + return &ExampleProcessorConfig{ ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(procType)), } } func createTracesProcessor(_ context.Context, _ component.ProcessorCreateSettings, _ config.Processor, nextConsumer consumer.Traces) (component.TracesProcessor, error) { - return &exampleProcessor{Traces: nextConsumer}, nil + return &ExampleProcessor{Traces: nextConsumer}, nil } func createMetricsProcessor(_ context.Context, _ component.ProcessorCreateSettings, _ config.Processor, nextConsumer consumer.Metrics) (component.MetricsProcessor, error) { - return &exampleProcessor{Metrics: nextConsumer}, nil + return &ExampleProcessor{Metrics: nextConsumer}, nil } func createLogsProcessor(_ context.Context, _ component.ProcessorCreateSettings, _ config.Processor, nextConsumer consumer.Logs) (component.LogsProcessor, error) { - return &exampleProcessor{Logs: nextConsumer}, nil + return &ExampleProcessor{Logs: nextConsumer}, nil } -type exampleProcessor struct { +type ExampleProcessor struct { consumer.Traces consumer.Metrics consumer.Logs + Started bool + Stopped bool } -func (ep *exampleProcessor) Start(_ context.Context, _ component.Host) error { +func (ep *ExampleProcessor) Start(_ context.Context, _ component.Host) error { + ep.Started = true return nil } -func (ep *exampleProcessor) Shutdown(_ context.Context) error { +func (ep *ExampleProcessor) Shutdown(_ context.Context) error { + ep.Stopped = true return nil } -func (ep *exampleProcessor) Capabilities() consumer.Capabilities { +func (ep *ExampleProcessor) Capabilities() consumer.Capabilities { return consumer.Capabilities{MutatesData: false} } diff --git a/internal/testcomponents/example_processor_test.go b/internal/testcomponents/example_processor_test.go new file mode 100644 index 00000000000..763f5b24825 --- /dev/null +++ b/internal/testcomponents/example_processor_test.go @@ -0,0 +1,36 @@ +// 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 testcomponents + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/component/componenttest" +) + +func TestExampleProcessor(t *testing.T) { + prc := &ExampleProcessor{} + host := componenttest.NewNopHost() + assert.False(t, prc.Started) + assert.NoError(t, prc.Start(context.Background(), host)) + assert.True(t, prc.Started) + + assert.False(t, prc.Stopped) + assert.NoError(t, prc.Shutdown(context.Background())) + assert.True(t, prc.Stopped) +} diff --git a/internal/testcomponents/example_receiver.go b/internal/testcomponents/example_receiver.go index a3c2a742f66..22e0ca2c266 100644 --- a/internal/testcomponents/example_receiver.go +++ b/internal/testcomponents/example_receiver.go @@ -22,14 +22,13 @@ import ( "go.opentelemetry.io/collector/consumer" ) -// ExampleReceiver is for testing purposes. We are defining an example config and factory -// for "examplereceiver" receiver type. -type ExampleReceiver struct { +const receiverType = config.Type("examplereceiver") + +// ExampleReceiverConfig config for ExampleReceiver. +type ExampleReceiverConfig struct { config.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct } -const receiverType = config.Type("examplereceiver") - // ExampleReceiverFactory is factory for ExampleReceiver. var ExampleReceiverFactory = component.NewReceiverFactory( receiverType, @@ -39,7 +38,7 @@ var ExampleReceiverFactory = component.NewReceiverFactory( component.WithLogsReceiver(createLogsReceiver)) func createReceiverDefaultConfig() config.Receiver { - return &ExampleReceiver{ + return &ExampleReceiverConfig{ ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(receiverType)), } } @@ -76,18 +75,17 @@ func createLogsReceiver( ) (component.LogsReceiver, error) { receiver := createReceiver(cfg) receiver.Logs = nextConsumer - return receiver, nil } -func createReceiver(cfg config.Receiver) *ExampleReceiverProducer { +func createReceiver(cfg config.Receiver) *ExampleReceiver { // 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{} + receiver = &ExampleReceiver{} // Remember the receiver in the map exampleReceivers[cfg] = receiver } @@ -95,23 +93,23 @@ func createReceiver(cfg config.Receiver) *ExampleReceiverProducer { return receiver } -// ExampleReceiverProducer allows producing traces and metrics for testing purposes. -type ExampleReceiverProducer struct { - Started bool - Stopped bool +// ExampleReceiver allows producing traces and metrics for testing purposes. +type ExampleReceiver struct { consumer.Traces consumer.Metrics consumer.Logs + Started bool + Stopped bool } // Start tells the receiver to start its processing. -func (erp *ExampleReceiverProducer) Start(_ context.Context, _ component.Host) error { +func (erp *ExampleReceiver) 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 { +func (erp *ExampleReceiver) Shutdown(context.Context) error { erp.Stopped = true return nil } @@ -120,4 +118,4 @@ func (erp *ExampleReceiverProducer) Shutdown(context.Context) error { // We maintain this map because the ReceiverFactory is asked trace and metric receivers separately // when it gets CreateTracesReceiver() and CreateMetricsReceiver() but they must not // create separate objects, they must use one Receiver object per configuration. -var exampleReceivers = map[config.Receiver]*ExampleReceiverProducer{} +var exampleReceivers = map[config.Receiver]*ExampleReceiver{} diff --git a/internal/testcomponents/example_receiver_test.go b/internal/testcomponents/example_receiver_test.go index b0415d05c2c..32f415cc058 100644 --- a/internal/testcomponents/example_receiver_test.go +++ b/internal/testcomponents/example_receiver_test.go @@ -23,15 +23,14 @@ import ( "go.opentelemetry.io/collector/component/componenttest" ) -func TestExampleReceiverProducer(t *testing.T) { - rcv := &ExampleReceiverProducer{} +func TestExampleReceiver(t *testing.T) { + rcv := &ExampleReceiver{} host := componenttest.NewNopHost() assert.False(t, rcv.Started) - err := rcv.Start(context.Background(), host) - assert.NoError(t, err) + assert.NoError(t, rcv.Start(context.Background(), host)) assert.True(t, rcv.Started) - err = rcv.Shutdown(context.Background()) - assert.NoError(t, err) - assert.True(t, rcv.Started) + assert.False(t, rcv.Stopped) + assert.NoError(t, rcv.Shutdown(context.Background())) + assert.True(t, rcv.Stopped) } diff --git a/service/internal/builder/exporters_builder_test.go b/service/internal/builder/exporters_builder_test.go index 967dd2e70f3..ad9d036970c 100644 --- a/service/internal/builder/exporters_builder_test.go +++ b/service/internal/builder/exporters_builder_test.go @@ -83,9 +83,9 @@ func TestBuildExporters(t *testing.T) { } func TestBuildExportersStartStopAll(t *testing.T) { - traceExporter := &testcomponents.ExampleExporterConsumer{} - metricExporter := &testcomponents.ExampleExporterConsumer{} - logsExporter := &testcomponents.ExampleExporterConsumer{} + traceExporter := &testcomponents.ExampleExporter{} + metricExporter := &testcomponents.ExampleExporter{} + logsExporter := &testcomponents.ExampleExporter{} exps := &BuiltExporters{ settings: componenttest.NewNopTelemetrySettings(), exporters: map[config.DataType]map[config.ComponentID]component.Exporter{ @@ -100,19 +100,19 @@ func TestBuildExportersStartStopAll(t *testing.T) { }, }, } - assert.False(t, traceExporter.ExporterStarted) - assert.False(t, metricExporter.ExporterStarted) - assert.False(t, logsExporter.ExporterStarted) + assert.False(t, traceExporter.Started) + assert.False(t, metricExporter.Started) + assert.False(t, logsExporter.Started) assert.NoError(t, exps.StartAll(context.Background(), componenttest.NewNopHost())) - assert.True(t, traceExporter.ExporterStarted) - assert.True(t, metricExporter.ExporterStarted) - assert.True(t, logsExporter.ExporterStarted) + assert.True(t, traceExporter.Started) + assert.True(t, metricExporter.Started) + assert.True(t, logsExporter.Started) assert.NoError(t, exps.ShutdownAll(context.Background())) - assert.True(t, traceExporter.ExporterShutdown) - assert.True(t, metricExporter.ExporterShutdown) - assert.True(t, logsExporter.ExporterShutdown) + assert.True(t, traceExporter.Stopped) + assert.True(t, metricExporter.Stopped) + assert.True(t, logsExporter.Stopped) } func TestBuildExportersNotSupportedDataType(t *testing.T) { diff --git a/service/internal/builder/pipelines_builder_test.go b/service/internal/builder/pipelines_builder_test.go index 5150f65fe9f..f270f04dc5d 100644 --- a/service/internal/builder/pipelines_builder_test.go +++ b/service/internal/builder/pipelines_builder_test.go @@ -144,9 +144,9 @@ func TestBuildPipelines_BuildVarious(t *testing.T) { // Send Logs via processor and verify that all exporters of the pipeline receive it. // First check that there are no logs in the exporters yet. - var exporterConsumers []*testcomponents.ExampleExporterConsumer + var exporterConsumers []*testcomponents.ExampleExporter for _, exporter := range exporters { - expConsumer := exporter.(*testcomponents.ExampleExporterConsumer) + expConsumer := exporter.(*testcomponents.ExampleExporter) exporterConsumers = append(exporterConsumers, expConsumer) require.Equal(t, len(expConsumer.Logs), 0) } @@ -206,9 +206,9 @@ func testPipeline(t *testing.T, pipelineID config.ComponentID, exporterIDs []con // Send TraceData via processor and verify that all exporters of the pipeline receive it. // First check that there are no traces in the exporters yet. - var exporterConsumers []*testcomponents.ExampleExporterConsumer + var exporterConsumers []*testcomponents.ExampleExporter for _, exporter := range exporters { - expConsumer := exporter.(*testcomponents.ExampleExporterConsumer) + expConsumer := exporter.(*testcomponents.ExampleExporter) exporterConsumers = append(exporterConsumers, expConsumer) require.Equal(t, len(expConsumer.Traces), 0) } diff --git a/service/internal/builder/receivers_builder_test.go b/service/internal/builder/receivers_builder_test.go index 3dd0601b0a3..157d56c0405 100644 --- a/service/internal/builder/receivers_builder_test.go +++ b/service/internal/builder/receivers_builder_test.go @@ -123,20 +123,20 @@ func testReceivers(t *testing.T, test testCase) { // First check that there are no traces in the exporters yet. for _, exporter := range exporters { - consumer := exporter.(*testcomponents.ExampleExporterConsumer) + consumer := exporter.(*testcomponents.ExampleExporter) require.Equal(t, len(consumer.Traces), 0) require.Equal(t, len(consumer.Metrics), 0) } td := testdata.GenerateTracesOneSpan() if test.hasTraces { - traceProducer := receiver.receiver.(*testcomponents.ExampleReceiverProducer) + traceProducer := receiver.receiver.(*testcomponents.ExampleReceiver) assert.NoError(t, traceProducer.ConsumeTraces(context.Background(), td)) } md := testdata.GenerateMetricsOneMetric() if test.hasMetrics { - metricsProducer := receiver.receiver.(*testcomponents.ExampleReceiverProducer) + metricsProducer := receiver.receiver.(*testcomponents.ExampleReceiver) assert.NoError(t, metricsProducer.ConsumeMetrics(context.Background(), md)) } @@ -151,7 +151,7 @@ func testReceivers(t *testing.T, test testCase) { spanDuplicationCount = 1 } - traceConsumer := allExporters.exporters[config.TracesDataType][expID].(*testcomponents.ExampleExporterConsumer) + traceConsumer := allExporters.exporters[config.TracesDataType][expID].(*testcomponents.ExampleExporter) require.Equal(t, spanDuplicationCount, len(traceConsumer.Traces)) for i := 0; i < spanDuplicationCount; i++ { @@ -161,7 +161,7 @@ func testReceivers(t *testing.T, test testCase) { // Validate metrics. if test.hasMetrics { - metricsConsumer := allExporters.exporters[config.MetricsDataType][expID].(*testcomponents.ExampleExporterConsumer) + metricsConsumer := allExporters.exporters[config.MetricsDataType][expID].(*testcomponents.ExampleExporter) require.Equal(t, 1, len(metricsConsumer.Metrics)) assert.EqualValues(t, md, metricsConsumer.Metrics[0]) } @@ -225,19 +225,19 @@ func TestBuildReceiversBuildCustom(t *testing.T) { // First check that there are no traces in the exporters yet. for _, exporter := range exporters { - consumer := exporter.(*testcomponents.ExampleExporterConsumer) + consumer := exporter.(*testcomponents.ExampleExporter) require.Equal(t, len(consumer.Logs), 0) } // Send one data. log := plog.Logs{} - producer := receiver.receiver.(*testcomponents.ExampleReceiverProducer) + producer := receiver.receiver.(*testcomponents.ExampleReceiver) require.NoError(t, producer.ConsumeLogs(context.Background(), log)) // Now verify received data. for _, exporter := range exporters { // Validate exported data. - consumer := exporter.(*testcomponents.ExampleExporterConsumer) + consumer := exporter.(*testcomponents.ExampleExporter) require.Equal(t, 1, len(consumer.Logs)) assert.EqualValues(t, log, consumer.Logs[0]) } @@ -247,7 +247,7 @@ func TestBuildReceiversBuildCustom(t *testing.T) { func TestBuildReceivers_StartAll(t *testing.T) { receivers := make(Receivers) - receiver := &testcomponents.ExampleReceiverProducer{} + receiver := &testcomponents.ExampleReceiver{} receivers[config.NewComponentID("example")] = &builtReceiver{ logger: zap.NewNop(),