Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance config validation by Instantiating service during dryrun. #12488

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/enhance_config-validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: otelcol

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Enhance config validation using <validate> command to capture all validation errors that prevents the collector from starting."

# One or more tracking issues or pull requests related to the change
issues: [8721]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
55 changes: 32 additions & 23 deletions otelcol/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,33 +168,16 @@ func buildModuleInfo(m map[component.Type]string) map[component.Type]service.Mod
return moduleInfo
}

// setupConfigurationComponents loads the config, creates the graph, and starts the components. If all the steps succeeds it
// sets the col.service with the service currently running.
func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
col.setCollectorState(StateStarting)

factories, err := col.set.Factories()
if err != nil {
return fmt.Errorf("failed to initialize factories: %w", err)
}
cfg, err := col.configProvider.Get(ctx, factories)
if err != nil {
return fmt.Errorf("failed to get config: %w", err)
}

if err = xconfmap.Validate(cfg); err != nil {
return fmt.Errorf("invalid configuration: %w", err)
}

// createService initializes a new service instance using the provided configuration and factories.
// Returns the service or an error if marshaling or service creation fails.
func (col *Collector) createService(ctx context.Context, cfg *Config, factories Factories) (*service.Service, error) {
col.serviceConfig = &cfg.Service

conf := confmap.New()

if err = conf.Marshal(cfg); err != nil {
return fmt.Errorf("could not marshal configuration: %w", err)
}
_ = conf.Marshal(cfg)

col.service, err = service.New(ctx, service.Settings{
return service.New(ctx, service.Settings{
BuildInfo: col.set.BuildInfo,
CollectorConf: conf,

Expand All @@ -219,6 +202,27 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
AsyncErrorChannel: col.asyncErrorChannel,
LoggingOptions: col.set.LoggingOptions,
}, cfg.Service)
}

// setupConfigurationComponents loads the config, creates the graph, and starts the components. If all the steps succeeds it
// sets the col.service with the service currently running.
func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
col.setCollectorState(StateStarting)

factories, err := col.set.Factories()
if err != nil {
return fmt.Errorf("failed to initialize factories: %w", err)
}
cfg, err := col.configProvider.Get(ctx, factories)
if err != nil {
return fmt.Errorf("failed to get config: %w", err)
}

if err = xconfmap.Validate(cfg); err != nil {
return fmt.Errorf("invalid configuration: %w", err)
}

col.service, err = col.createService(ctx, cfg, factories)
if err != nil {
return err
}
Expand Down Expand Up @@ -272,7 +276,12 @@ func (col *Collector) DryRun(ctx context.Context) error {
return fmt.Errorf("failed to get config: %w", err)
}

return xconfmap.Validate(cfg)
if err = xconfmap.Validate(cfg); err != nil {
return err
}

_, err = col.createService(ctx, cfg, factories)
return err
Comment on lines +283 to +284
Copy link
Member

@bogdandrutu bogdandrutu Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this? Why do we need to start things to make sure we are ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does not start a service. As mentioned in this #8721 (comment) we can surface configuration errors by instantiating the service during a dry run. This will initialize the graph, and detect cycles and other errors which might prevent the service to start.

}

func newFallbackLogger(options []zap.Option) (*zap.Logger, error) {
Expand Down
109 changes: 109 additions & 0 deletions otelcol/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,47 @@ func TestCollectorStartInvalidConfig(t *testing.T) {
assert.EqualError(t, col.Run(context.Background()), "invalid configuration: service::pipelines::traces: references processor \"invalid\" which is not configured")
}

func TestSetupConfigurationComponentsInvalidConfig(t *testing.T) {
tests := map[string]struct {
settings CollectorSettings
expectedErr string
}{
"invalid_factories": {
settings: CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: func() (Factories, error) { return Factories{}, nil },
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid.yaml")}),
},
expectedErr: "failed to get config",
},
"error_factories": {
settings: CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: func() (Factories, error) { return Factories{}, errors.New("error") },
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid.yaml")}),
},
expectedErr: "failed to initialize factories",
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
col, err := NewCollector(test.settings)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err = col.setupConfigurationComponents(ctx)
if test.expectedErr == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, test.expectedErr)
}
})
}
}

func TestNewCollectorInvalidConfigProviderSettings(t *testing.T) {
_, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Expand Down Expand Up @@ -462,6 +503,22 @@ func TestCollectorDryRun(t *testing.T) {
},
expectedErr: `service::pipelines::traces: references processor "invalid" which is not configured`,
},
"invalid_connector_use": {
settings: CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: nopFactories,
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid-connector-use.yaml")}),
},
expectedErr: `failed to build pipelines: connector "nop/connector1" used as exporter in [logs/in2] pipeline but not used in any supported receiver pipeline`,
},
"cyclic_connector": {
settings: CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: nopFactories,
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-cyclic-connector.yaml")}),
},
expectedErr: `failed to build pipelines: cycle detected: connector "nop/forward" (traces to traces) -> connector "nop/forward" (traces to traces)`,
},
}

for name, test := range tests {
Expand All @@ -479,6 +536,58 @@ func TestCollectorDryRun(t *testing.T) {
}
}

func TestCreateService(t *testing.T) {
tests := map[string]struct {
settings CollectorSettings
expectedErr string
}{
"invalid_connector_use": {
settings: CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: nopFactories,
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid-connector-use.yaml")}),
},
expectedErr: `failed to build pipelines: connector "nop/connector1" used as exporter in [logs/in2] pipeline but not used in any supported receiver pipeline`,
},
"valid_connector_use": {
settings: CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: nopFactories,
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-valid-connector-use.yaml")}),
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
col, err := NewCollector(test.settings)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

factories, err := nopFactories()
require.NoError(t, err)

cfg, err := col.configProvider.Get(ctx, factories)
require.NoError(t, err)

svc, err := col.createService(ctx, cfg, factories)

if test.expectedErr == "" {
require.NoError(t, err)
require.NotNil(t, svc)
defer func() {
require.NoError(t, svc.Shutdown(ctx))
}()
} else {
require.EqualError(t, err, test.expectedErr)
require.Nil(t, svc)
}
})
}
}

func startCollector(ctx context.Context, t *testing.T, col *Collector) *sync.WaitGroup {
wg := &sync.WaitGroup{}
wg.Add(1)
Expand Down
19 changes: 19 additions & 0 deletions otelcol/testdata/otelcol-cyclic-connector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
receivers:
nop:

exporters:
nop:

connectors:
nop/forward:

service:
pipelines:
traces/in:
receivers: [nop/forward]
processors: [ ]
exporters: [nop/forward]
traces/out:
receivers: [nop/forward]
processors: [ ]
exporters: [nop/forward]
23 changes: 23 additions & 0 deletions otelcol/testdata/otelcol-invalid-connector-use.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
receivers:
nop:

exporters:
nop:

connectors:
nop/connector1:

service:
pipelines:
logs/in1:
receivers: [nop]
processors: [ ]
exporters: [nop]
logs/in2:
receivers: [nop]
processors: [ ]
exporters: [nop/connector1]
logs/out:
receivers: [nop]
processors: [ ]
exporters: [nop]
23 changes: 23 additions & 0 deletions otelcol/testdata/otelcol-valid-connector-use.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
receivers:
nop:

exporters:
nop:

connectors:
nop/connector1:

service:
pipelines:
logs/in1:
receivers: [nop]
processors: [ ]
exporters: [nop]
logs/in2:
receivers: [nop]
processors: [ ]
exporters: [nop/connector1]
logs/out:
receivers: [nop/connector1]
processors: [ ]
exporters: [nop]
1 change: 1 addition & 0 deletions otelcol/unmarshal_dry_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestDryRunWithExpandedValues(t *testing.T) {
mockMap: map[string]string{
"number": "123",
},
expectErr: true,
},
{
name: "string that looks like a bool",
Expand Down
2 changes: 0 additions & 2 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) {
return nil, fmt.Errorf("failed to create tracer provider: %w", err)
}

logger.Info("Setting up own telemetry...")

mp, err := telFactory.CreateMeterProvider(ctx, telset, &cfg.Telemetry)
if err != nil {
err = multierr.Append(err, sdk.Shutdown(ctx))
Expand Down
Loading