diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a27a5cc14b..208fe51e96f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Remove reference to `defaultcomponents` in core and deprecate `include_core` flag (#4087) + ## 🛑 Breaking changes 🛑 - Remove `config.NewConfigMapFrom[File|Buffer]`, add testonly version (#4502) diff --git a/cmd/builder/README.md b/cmd/builder/README.md index 75a6605180d..a6819f5177a 100644 --- a/cmd/builder/README.md +++ b/cmd/builder/README.md @@ -73,7 +73,6 @@ dist: module: github.com/open-telemetry/opentelemetry-collector # the module name for the new distribution, following Go mod conventions. Optional, but recommended. name: otelcol-custom # the binary name. Optional. description: "Custom OpenTelemetry Collector distribution" # a long name for the application. Optional. - include_core: true # whether the core components should be included in the distribution. Optional. otelcol_version: "0.40.0" # the OpenTelemetry Collector version to use as base for the distribution. Optional. output_path: /tmp/otelcol-distributionNNN # the path to write the output (sources and binary). Optional. version: "1.0.0" # the version for your custom OpenTelemetry Collector. Optional. diff --git a/cmd/builder/internal/builder/config.go b/cmd/builder/internal/builder/config.go index 3d884c50220..d0a2c2db58f 100644 --- a/cmd/builder/internal/builder/config.go +++ b/cmd/builder/internal/builder/config.go @@ -54,9 +54,10 @@ type Distribution struct { Go string `mapstructure:"go"` Description string `mapstructure:"description"` OtelColVersion string `mapstructure:"otelcol_version"` - IncludeCore bool `mapstructure:"include_core"` - OutputPath string `mapstructure:"output_path"` - Version string `mapstructure:"version"` + // IncludeCore is deprecated and note that if this is being used, it will be removed in a subsequent release + IncludeCore bool `mapstructure:"include_core"` + OutputPath string `mapstructure:"output_path"` + Version string `mapstructure:"version"` } // Module represents a receiver, exporter, processor or extension for the distribution @@ -100,6 +101,14 @@ func (c *Config) Validate() error { } c.Distribution.Go = path } + + // create a warning message that include_core is deprecated and will be removed in a subsequent release + if c.Distribution.IncludeCore { + c.Logger.Warn("IncludeCore is deprecated. Starting from v0.41.0, you need to include all components explicitly.") + } else { + c.Logger.Info("IncludeCore is deprecated, starting from v0.41.0. The new behavior won't affect your distribution, just remove the option from the manifest.") + } + c.Logger.Info("Using go", zap.String("go-executable", c.Distribution.Go)) return nil diff --git a/cmd/builder/internal/builder/config_test.go b/cmd/builder/internal/builder/config_test.go index 00dafa2da9b..e5c072494a5 100644 --- a/cmd/builder/internal/builder/config_test.go +++ b/cmd/builder/internal/builder/config_test.go @@ -161,3 +161,9 @@ func TestDefaultConfig(t *testing.T) { require.NoError(t, cfg.ParseModules()) require.NoError(t, cfg.Validate()) } + +func TestValidateDeprecatedIncludeCoreWarnLog(t *testing.T) { + cfg := DefaultConfig() + cfg.Distribution.IncludeCore = true + require.NoError(t, cfg.Validate()) +} diff --git a/cmd/builder/internal/command.go b/cmd/builder/internal/command.go index 25b98129082..b499f8bda1a 100644 --- a/cmd/builder/internal/command.go +++ b/cmd/builder/internal/command.go @@ -61,7 +61,12 @@ func Command() (*cobra.Command, error) { cmd.Flags().StringVar(&cfg.Distribution.Name, "name", "otelcol-custom", "The executable name for the OpenTelemetry Collector distribution") cmd.Flags().StringVar(&cfg.Distribution.Description, "description", "Custom OpenTelemetry Collector distribution", "A descriptive name for the OpenTelemetry Collector distribution") cmd.Flags().StringVar(&cfg.Distribution.Version, "version", "1.0.0", "The version for the OpenTelemetry Collector distribution") - cmd.Flags().BoolVar(&cfg.Distribution.IncludeCore, "include-core", true, "Whether the core components should be included in the distribution") + // IncludeCore is deprecated and will be removed in a subsequent release + cmd.Flags().BoolVar(&cfg.Distribution.IncludeCore, "include-core", true, "Deprecated: starting from v0.41.0, core components are not going to be implicitly included.") + if err := cmd.Flags().MarkDeprecated("include-core", "IncludeCore is deprecated, please explicitly list all the components required"); err != nil { + cfg.Logger.Error("failed to mark the IncludeCore flag is deprecated", zap.Error(err)) + return nil, err + } cmd.Flags().StringVar(&cfg.Distribution.OtelColVersion, "otelcol-version", cfg.Distribution.OtelColVersion, "Which version of OpenTelemetry Collector to use as base") cmd.Flags().StringVar(&cfg.Distribution.OutputPath, "output-path", cfg.Distribution.OutputPath, "Where to write the resulting files") cmd.Flags().StringVar(&cfg.Distribution.Go, "go", "", "The Go binary to use during the compilation phase. Default: go from the PATH") diff --git a/cmd/builder/test/nocore.builder.yaml b/cmd/builder/test/nocore.builder.yaml index 2d089832c7d..f4a5d56ed50 100644 --- a/cmd/builder/test/nocore.builder.yaml +++ b/cmd/builder/test/nocore.builder.yaml @@ -1,14 +1,14 @@ dist: module: go.opentelemetry.io/collector/builder/test/nocore - otelcol_version: 0.38.0 + otelcol_version: 0.40.0 include_core: false receivers: - import: go.opentelemetry.io/collector/receiver/otlpreceiver - gomod: go.opentelemetry.io/collector v0.38.0 + gomod: go.opentelemetry.io/collector v0.40.0 exporters: - import: go.opentelemetry.io/collector/exporter/loggingexporter - gomod: go.opentelemetry.io/collector v0.38.0 + gomod: go.opentelemetry.io/collector v0.40.0 extensions: - import: go.opentelemetry.io/collector/extension/zpagesextension - gomod: go.opentelemetry.io/collector v0.38.0 + gomod: go.opentelemetry.io/collector v0.40.0 diff --git a/cmd/builder/test/replaces.builder.yaml b/cmd/builder/test/replaces.builder.yaml index a20e1a63977..bcdfb28c7f8 100644 --- a/cmd/builder/test/replaces.builder.yaml +++ b/cmd/builder/test/replaces.builder.yaml @@ -1,10 +1,10 @@ dist: module: go.opentelemetry.io/collector/builder/test/replaces - otelcol_version: 0.38.0 + otelcol_version: 0.40.0 processors: - - gomod: github.com/open-telemetry/opentelemetry-collector-contrib/processor/routingprocessor v0.38.0 - - gomod: github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor v0.38.0 + - gomod: github.com/open-telemetry/opentelemetry-collector-contrib/processor/routingprocessor v0.40.0 + - gomod: github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor v0.40.0 replaces: - - github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.38.0 + - github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.40.0 diff --git a/internal/testcomponents/example_factories.go b/internal/testcomponents/example_factories.go index e10105a41ea..44935a0d66b 100644 --- a/internal/testcomponents/example_factories.go +++ b/internal/testcomponents/example_factories.go @@ -15,7 +15,13 @@ package testcomponents // import "go.opentelemetry.io/collector/internal/testcomponents" import ( + "go.uber.org/multierr" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/exporter/otlpexporter" + "go.opentelemetry.io/collector/extension/zpagesextension" + "go.opentelemetry.io/collector/processor/batchprocessor" + "go.opentelemetry.io/collector/receiver/otlpreceiver" ) // ExampleComponents registers example factories. This is only used by tests. @@ -39,3 +45,40 @@ func ExampleComponents() ( return } + +// DefaultFactories returns the set of components in "testdata/otelcol-config.yaml". This is only used by tests. +func DefaultFactories() ( + component.Factories, + error, +) { + var errs error + + extensions, err := component.MakeExtensionFactoryMap( + zpagesextension.NewFactory(), + ) + errs = multierr.Append(errs, err) + + receivers, err := component.MakeReceiverFactoryMap( + otlpreceiver.NewFactory(), + ) + errs = multierr.Append(errs, err) + + processors, err := component.MakeProcessorFactoryMap( + batchprocessor.NewFactory(), + ) + errs = multierr.Append(errs, err) + + exporters, err := component.MakeExporterFactoryMap( + otlpexporter.NewFactory(), + ) + errs = multierr.Append(errs, err) + + factories := component.Factories{ + Extensions: extensions, + Receivers: receivers, + Processors: processors, + Exporters: exporters, + } + + return factories, errs +} diff --git a/service/collector_test.go b/service/collector_test.go index 35e519b3ba9..d1e194e873b 100644 --- a/service/collector_test.go +++ b/service/collector_test.go @@ -36,8 +36,8 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configmapprovider" + "go.opentelemetry.io/collector/internal/testcomponents" "go.opentelemetry.io/collector/internal/testutil" - "go.opentelemetry.io/collector/service/defaultcomponents" ) // TestCollector_StartAsGoRoutine must be the first unit test on the file, @@ -48,7 +48,7 @@ func TestCollector_StartAsGoRoutine(t *testing.T) { collectorTelemetry = &colTelemetry{} defer func() { collectorTelemetry = preservedAppTelemetry }() - factories, err := defaultcomponents.Components() + factories, err := testcomponents.DefaultFactories() require.NoError(t, err) set := CollectorSettings{ @@ -81,7 +81,7 @@ func TestCollector_StartAsGoRoutine(t *testing.T) { } func TestCollector_Start(t *testing.T) { - factories, err := defaultcomponents.Components() + factories, err := testcomponents.DefaultFactories() require.NoError(t, err) loggingHookCalled := false @@ -153,7 +153,7 @@ func TestCollector_ReportError(t *testing.T) { collectorTelemetry = &mockColTelemetry{} defer func() { collectorTelemetry = preservedAppTelemetry }() - factories, err := defaultcomponents.Components() + factories, err := testcomponents.DefaultFactories() require.NoError(t, err) col, err := New(CollectorSettings{ diff --git a/service/collector_windows_test.go b/service/collector_windows_test.go index 784a7169d91..56850dd941e 100644 --- a/service/collector_windows_test.go +++ b/service/collector_windows_test.go @@ -27,13 +27,13 @@ import ( "golang.org/x/sys/windows/svc" "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/service/defaultcomponents" + "go.opentelemetry.io/collector/internal/testcomponents" ) func TestWindowsService_Execute(t *testing.T) { os.Args = []string{"otelcol", "--config", path.Join("testdata", "otelcol-config.yaml")} - factories, err := defaultcomponents.Components() + factories, err := testcomponents.DefaultFactories() require.NoError(t, err) s := NewWindowsService(CollectorSettings{BuildInfo: component.NewDefaultBuildInfo(), Factories: factories}) diff --git a/service/command_test.go b/service/command_test.go index cd06e05d834..2ff6b634b52 100644 --- a/service/command_test.go +++ b/service/command_test.go @@ -20,7 +20,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.opentelemetry.io/collector/service/defaultcomponents" + "go.opentelemetry.io/collector/internal/testcomponents" ) func TestNewCommand(t *testing.T) { @@ -40,7 +40,7 @@ func TestNewCommandMapProviderIsNil(t *testing.T) { } func TestNewCommandInvalidFactories(t *testing.T) { - factories, err := defaultcomponents.Components() + factories, err := testcomponents.ExampleComponents() require.NoError(t, err) f := &badConfigExtensionFactory{} factories.Extensions[f.Type()] = f diff --git a/service/configcheck_test.go b/service/configcheck_test.go index e4e5c59ece1..1a4f6c5229f 100644 --- a/service/configcheck_test.go +++ b/service/configcheck_test.go @@ -23,11 +23,11 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/internal/internalinterface" - "go.opentelemetry.io/collector/service/defaultcomponents" + "go.opentelemetry.io/collector/internal/testcomponents" ) func TestValidateConfigFromFactories_Success(t *testing.T) { - factories, err := defaultcomponents.Components() + factories, err := testcomponents.ExampleComponents() require.NoError(t, err) err = validateConfigFromFactories(factories) @@ -35,7 +35,7 @@ func TestValidateConfigFromFactories_Success(t *testing.T) { } func TestValidateConfigFromFactories_Failure(t *testing.T) { - factories, err := defaultcomponents.Components() + factories, err := testcomponents.ExampleComponents() require.NoError(t, err) // Add a factory returning config not following pattern to force error. diff --git a/service/defaultcomponents/default_exporters_test.go b/service/defaultcomponents/default_exporters_test.go deleted file mode 100644 index 623d8b2d5c0..00000000000 --- a/service/defaultcomponents/default_exporters_test.go +++ /dev/null @@ -1,153 +0,0 @@ -// 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 defaultcomponents - -import ( - "context" - "errors" - "runtime" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/component/componenterror" - "go.opentelemetry.io/collector/component/componenttest" - "go.opentelemetry.io/collector/config" - "go.opentelemetry.io/collector/config/configgrpc" - "go.opentelemetry.io/collector/exporter/otlpexporter" - "go.opentelemetry.io/collector/exporter/otlphttpexporter" - "go.opentelemetry.io/collector/internal/testutil" -) - -func TestDefaultExporters(t *testing.T) { - factories, err := Components() - assert.NoError(t, err) - - expFactories := factories.Exporters - endpoint := testutil.GetAvailableLocalAddress(t) - - tests := []struct { - exporter config.Type - getConfigFn getExporterConfigFn - skipLifecycle bool - }{ - { - exporter: "logging", - skipLifecycle: runtime.GOOS == "darwin", // TODO: investigate why this fails on darwin. - }, - { - exporter: "otlp", - getConfigFn: func() config.Exporter { - cfg := expFactories["otlp"].CreateDefaultConfig().(*otlpexporter.Config) - cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{ - Endpoint: endpoint, - } - return cfg - }, - }, - { - exporter: "otlphttp", - getConfigFn: func() config.Exporter { - cfg := expFactories["otlphttp"].CreateDefaultConfig().(*otlphttpexporter.Config) - cfg.Endpoint = "http://" + endpoint - return cfg - }, - }, - } - - assert.Equal(t, len(tests), len(expFactories)) - for _, tt := range tests { - t.Run(string(tt.exporter), func(t *testing.T) { - factory, ok := expFactories[tt.exporter] - require.True(t, ok) - assert.Equal(t, tt.exporter, factory.Type()) - assert.Equal(t, config.NewComponentID(tt.exporter), factory.CreateDefaultConfig().ID()) - - if tt.skipLifecycle { - t.Log("Skipping lifecycle test", tt.exporter) - return - } - - verifyExporterLifecycle(t, factory, tt.getConfigFn) - }) - } -} - -// GetExporterConfigFn is used customize the configuration passed to the verification. -// This is used to change ports or provide values required but not provided by the -// default configuration. -type getExporterConfigFn func() config.Exporter - -// verifyExporterLifecycle is used to test if an exporter type can handle the typical -// lifecycle of a component. The getConfigFn parameter only need to be specified if -// the test can't be done with the default configuration for the component. -func verifyExporterLifecycle(t *testing.T, factory component.ExporterFactory, getConfigFn getExporterConfigFn) { - ctx := context.Background() - host := newAssertNoErrorHost(t) - expCreateSettings := componenttest.NewNopExporterCreateSettings() - - cfg := factory.CreateDefaultConfig() - if getConfigFn != nil { - cfg = getConfigFn() - } - - createFns := []createExporterFn{ - wrapCreateLogsExp(factory), - wrapCreateTracesExp(factory), - wrapCreateMetricsExp(factory), - } - - for i := 0; i < 2; i++ { - var exps []component.Exporter - for _, createFn := range createFns { - exp, err := createFn(ctx, expCreateSettings, cfg) - if errors.Is(err, componenterror.ErrDataTypeIsNotSupported) { - continue - } - require.NoError(t, err) - require.NoError(t, exp.Start(ctx, host)) - exps = append(exps, exp) - } - for _, exp := range exps { - assert.NoError(t, exp.Shutdown(ctx)) - } - } -} - -type createExporterFn func( - ctx context.Context, - set component.ExporterCreateSettings, - cfg config.Exporter, -) (component.Exporter, error) - -func wrapCreateLogsExp(factory component.ExporterFactory) createExporterFn { - return func(ctx context.Context, set component.ExporterCreateSettings, cfg config.Exporter) (component.Exporter, error) { - return factory.CreateLogsExporter(ctx, set, cfg) - } -} - -func wrapCreateTracesExp(factory component.ExporterFactory) createExporterFn { - return func(ctx context.Context, set component.ExporterCreateSettings, cfg config.Exporter) (component.Exporter, error) { - return factory.CreateTracesExporter(ctx, set, cfg) - } -} - -func wrapCreateMetricsExp(factory component.ExporterFactory) createExporterFn { - return func(ctx context.Context, set component.ExporterCreateSettings, cfg config.Exporter) (component.Exporter, error) { - return factory.CreateMetricsExporter(ctx, set, cfg) - } -} diff --git a/service/defaultcomponents/default_extensions_test.go b/service/defaultcomponents/default_extensions_test.go deleted file mode 100644 index b4f8b75c70b..00000000000 --- a/service/defaultcomponents/default_extensions_test.go +++ /dev/null @@ -1,120 +0,0 @@ -// 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 defaultcomponents - -import ( - "context" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/component/componenttest" - "go.opentelemetry.io/collector/config" - "go.opentelemetry.io/collector/extension/ballastextension" - "go.opentelemetry.io/collector/extension/zpagesextension" - "go.opentelemetry.io/collector/internal/testutil" -) - -func TestDefaultExtensions(t *testing.T) { - allFactories, err := Components() - require.NoError(t, err) - - extFactories := allFactories.Extensions - endpoint := testutil.GetAvailableLocalAddress(t) - - tests := []struct { - extension config.Type - getConfigFn getExtensionConfigFn - }{ - { - extension: "zpages", - getConfigFn: func() config.Extension { - cfg := extFactories["zpages"].CreateDefaultConfig().(*zpagesextension.Config) - cfg.TCPAddr.Endpoint = endpoint - return cfg - }, - }, - { - extension: "memory_ballast", - getConfigFn: func() config.Extension { - cfg := extFactories["memory_ballast"].CreateDefaultConfig().(*ballastextension.Config) - return cfg - }, - }, - } - - assert.Equal(t, len(tests), len(extFactories)) - - for _, tt := range tests { - t.Run(string(tt.extension), func(t *testing.T) { - factory, ok := extFactories[tt.extension] - require.True(t, ok) - assert.Equal(t, tt.extension, factory.Type()) - assert.Equal(t, config.NewComponentID(tt.extension), factory.CreateDefaultConfig().ID()) - - verifyExtensionLifecycle(t, factory, tt.getConfigFn) - }) - } -} - -// getExtensionConfigFn is used customize the configuration passed to the verification. -// This is used to change ports or provide values required but not provided by the -// default configuration. -type getExtensionConfigFn func() config.Extension - -// verifyExtensionLifecycle is used to test if an extension type can handle the typical -// lifecycle of a component. The getConfigFn parameter only need to be specified if -// the test can't be done with the default configuration for the component. -func verifyExtensionLifecycle(t *testing.T, factory component.ExtensionFactory, getConfigFn getExtensionConfigFn) { - ctx := context.Background() - host := newAssertNoErrorHost(t) - extCreateSet := componenttest.NewNopExtensionCreateSettings() - - if getConfigFn == nil { - getConfigFn = factory.CreateDefaultConfig - } - - firstExt, err := factory.CreateExtension(ctx, extCreateSet, getConfigFn()) - require.NoError(t, err) - require.NoError(t, firstExt.Start(ctx, host)) - require.NoError(t, firstExt.Shutdown(ctx)) - - secondExt, err := factory.CreateExtension(ctx, extCreateSet, getConfigFn()) - require.NoError(t, err) - require.NoError(t, secondExt.Start(ctx, host)) - require.NoError(t, secondExt.Shutdown(ctx)) -} - -// assertNoErrorHost implements a component.Host that asserts that there were no errors. -type assertNoErrorHost struct { - component.Host - *testing.T -} - -var _ component.Host = (*assertNoErrorHost)(nil) - -// newAssertNoErrorHost returns a new instance of assertNoErrorHost. -func newAssertNoErrorHost(t *testing.T) component.Host { - return &assertNoErrorHost{ - componenttest.NewNopHost(), - t, - } -} - -func (aneh *assertNoErrorHost) ReportFatalError(err error) { - assert.NoError(aneh, err) -} diff --git a/service/defaultcomponents/default_processors_test.go b/service/defaultcomponents/default_processors_test.go deleted file mode 100644 index 5b2a17c0ad8..00000000000 --- a/service/defaultcomponents/default_processors_test.go +++ /dev/null @@ -1,132 +0,0 @@ -// 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 defaultcomponents - -import ( - "context" - "errors" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/component/componenterror" - "go.opentelemetry.io/collector/component/componenttest" - "go.opentelemetry.io/collector/config" - "go.opentelemetry.io/collector/consumer/consumertest" - "go.opentelemetry.io/collector/processor/memorylimiterprocessor" -) - -func TestDefaultProcessors(t *testing.T) { - allFactories, err := Components() - require.NoError(t, err) - - procFactories := allFactories.Processors - - tests := []struct { - processor config.Type - getConfigFn getProcessorConfigFn - }{ - { - processor: "batch", - }, - { - processor: "memory_limiter", - getConfigFn: func() config.Processor { - cfg := procFactories["memory_limiter"].CreateDefaultConfig().(*memorylimiterprocessor.Config) - cfg.CheckInterval = 100 * time.Millisecond - cfg.MemoryLimitMiB = 1024 * 1024 - return cfg - }, - }, - } - - assert.Equal(t, len(tests), len(procFactories)) - for _, tt := range tests { - t.Run(string(tt.processor), func(t *testing.T) { - factory, ok := procFactories[tt.processor] - require.True(t, ok) - assert.Equal(t, tt.processor, factory.Type()) - assert.EqualValues(t, config.NewComponentID(tt.processor), factory.CreateDefaultConfig().ID()) - - verifyProcessorLifecycle(t, factory, tt.getConfigFn) - }) - } -} - -// getProcessorConfigFn is used customize the configuration passed to the verification. -// This is used to change ports or provide values required but not provided by the -// default configuration. -type getProcessorConfigFn func() config.Processor - -// verifyProcessorLifecycle is used to test if an processor type can handle the typical -// lifecycle of a component. The getConfigFn parameter only need to be specified if -// the test can't be done with the default configuration for the component. -func verifyProcessorLifecycle(t *testing.T, factory component.ProcessorFactory, getConfigFn getProcessorConfigFn) { - ctx := context.Background() - host := newAssertNoErrorHost(t) - processorCreationSet := componenttest.NewNopProcessorCreateSettings() - - if getConfigFn == nil { - getConfigFn = factory.CreateDefaultConfig - } - - createFns := []createProcessorFn{ - wrapCreateLogsProc(factory), - wrapCreateTracesProc(factory), - wrapCreateMetricsProc(factory), - } - - for _, createFn := range createFns { - firstExp, err := createFn(ctx, processorCreationSet, getConfigFn()) - if errors.Is(err, componenterror.ErrDataTypeIsNotSupported) { - continue - } - require.NoError(t, err) - require.NoError(t, firstExp.Start(ctx, host)) - require.NoError(t, firstExp.Shutdown(ctx)) - - secondExp, err := createFn(ctx, processorCreationSet, getConfigFn()) - require.NoError(t, err) - require.NoError(t, secondExp.Start(ctx, host)) - require.NoError(t, secondExp.Shutdown(ctx)) - } -} - -type createProcessorFn func( - ctx context.Context, - set component.ProcessorCreateSettings, - cfg config.Processor, -) (component.Processor, error) - -func wrapCreateLogsProc(factory component.ProcessorFactory) createProcessorFn { - return func(ctx context.Context, set component.ProcessorCreateSettings, cfg config.Processor) (component.Processor, error) { - return factory.CreateLogsProcessor(ctx, set, cfg, consumertest.NewNop()) - } -} - -func wrapCreateMetricsProc(factory component.ProcessorFactory) createProcessorFn { - return func(ctx context.Context, set component.ProcessorCreateSettings, cfg config.Processor) (component.Processor, error) { - return factory.CreateMetricsProcessor(ctx, set, cfg, consumertest.NewNop()) - } -} - -func wrapCreateTracesProc(factory component.ProcessorFactory) createProcessorFn { - return func(ctx context.Context, set component.ProcessorCreateSettings, cfg config.Processor) (component.Processor, error) { - return factory.CreateTracesProcessor(ctx, set, cfg, consumertest.NewNop()) - } -} diff --git a/service/defaultcomponents/default_receivers_test.go b/service/defaultcomponents/default_receivers_test.go deleted file mode 100644 index abfe7fa8a4e..00000000000 --- a/service/defaultcomponents/default_receivers_test.go +++ /dev/null @@ -1,130 +0,0 @@ -// 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 defaultcomponents - -import ( - "context" - "errors" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/component/componenterror" - "go.opentelemetry.io/collector/component/componenttest" - "go.opentelemetry.io/collector/config" - "go.opentelemetry.io/collector/consumer/consumertest" - "go.opentelemetry.io/collector/internal/testutil" - "go.opentelemetry.io/collector/receiver/otlpreceiver" -) - -func TestDefaultReceivers(t *testing.T) { - allFactories, err := Components() - assert.NoError(t, err) - - rcvrFactories := allFactories.Receivers - - tests := []struct { - receiver config.Type - getConfigFn getReceiverConfigFn - }{ - { - receiver: "otlp", - getConfigFn: func() config.Receiver { - cfg := otlpreceiver.NewFactory().CreateDefaultConfig() - cfg.(*otlpreceiver.Config).GRPC.NetAddr.Endpoint = testutil.GetAvailableLocalAddress(t) - cfg.(*otlpreceiver.Config).HTTP.Endpoint = testutil.GetAvailableLocalAddress(t) - return cfg - }, - }, - } - - assert.Equal(t, len(tests), len(rcvrFactories)) - for _, tt := range tests { - t.Run(string(tt.receiver), func(t *testing.T) { - factory, ok := rcvrFactories[tt.receiver] - require.True(t, ok) - assert.Equal(t, tt.receiver, factory.Type()) - assert.Equal(t, config.NewComponentID(tt.receiver), factory.CreateDefaultConfig().ID()) - - verifyReceiverLifecycle(t, factory, tt.getConfigFn) - }) - } -} - -// getReceiverConfigFn is used customize the configuration passed to the verification. -// This is used to change ports or provide values required but not provided by the -// default configuration. -type getReceiverConfigFn func() config.Receiver - -// verifyReceiverLifecycle is used to test if a receiver type can handle the typical -// lifecycle of a component. The getConfigFn parameter only need to be specified if -// the test can't be done with the default configuration for the component. -func verifyReceiverLifecycle(t *testing.T, factory component.ReceiverFactory, getConfigFn getReceiverConfigFn) { - ctx := context.Background() - host := newAssertNoErrorHost(t) - receiverCreateSet := componenttest.NewNopReceiverCreateSettings() - - if getConfigFn == nil { - getConfigFn = factory.CreateDefaultConfig - } - - createFns := []createReceiverFn{ - wrapCreateLogsRcvr(factory), - wrapCreateTracesRcvr(factory), - wrapCreateMetricsRcvr(factory), - } - - for _, createFn := range createFns { - firstRcvr, err := createFn(ctx, receiverCreateSet, getConfigFn()) - if errors.Is(err, componenterror.ErrDataTypeIsNotSupported) { - continue - } - require.NoError(t, err) - require.NoError(t, firstRcvr.Start(ctx, host)) - require.NoError(t, firstRcvr.Shutdown(ctx)) - - secondRcvr, err := createFn(ctx, receiverCreateSet, getConfigFn()) - require.NoError(t, err) - require.NoError(t, secondRcvr.Start(ctx, host)) - require.NoError(t, secondRcvr.Shutdown(ctx)) - } -} - -// assertNoErrorHost implements a component.Host that asserts that there were no errors. -type createReceiverFn func( - ctx context.Context, - set component.ReceiverCreateSettings, - cfg config.Receiver, -) (component.Receiver, error) - -func wrapCreateLogsRcvr(factory component.ReceiverFactory) createReceiverFn { - return func(ctx context.Context, set component.ReceiverCreateSettings, cfg config.Receiver) (component.Receiver, error) { - return factory.CreateLogsReceiver(ctx, set, cfg, consumertest.NewNop()) - } -} - -func wrapCreateMetricsRcvr(factory component.ReceiverFactory) createReceiverFn { - return func(ctx context.Context, set component.ReceiverCreateSettings, cfg config.Receiver) (component.Receiver, error) { - return factory.CreateMetricsReceiver(ctx, set, cfg, consumertest.NewNop()) - } -} - -func wrapCreateTracesRcvr(factory component.ReceiverFactory) createReceiverFn { - return func(ctx context.Context, set component.ReceiverCreateSettings, cfg config.Receiver) (component.Receiver, error) { - return factory.CreateTracesReceiver(ctx, set, cfg, consumertest.NewNop()) - } -}