-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validate command to validate collector config (#7835)
This is bringing back the functionality initially submitted in @Chinwendu20's PR: #6445 Adds a validate method which validates the configuration Link to tracking Issue: #4613 --------- Signed-off-by: Alex Boten <[email protected]> Co-authored-by: Maureen <[email protected]>
- Loading branch information
1 parent
5d032e9
commit 463711f
Showing
8 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 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: service | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Added dry run flag to validate config file without running collector. | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [4671] | ||
|
||
# (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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otelcol // import "go.opentelemetry.io/collector/otelcol" | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// newValidateSubCommand constructs a new validate sub command using the given CollectorSettings. | ||
func newValidateSubCommand(set CollectorSettings, flagSet *flag.FlagSet) *cobra.Command { | ||
validateCmd := &cobra.Command{ | ||
Use: "validate", | ||
Short: "Validates the config without running the collector", | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if set.ConfigProvider == nil { | ||
var err error | ||
|
||
configFlags := getConfigFlag(flagSet) | ||
if len(configFlags) == 0 { | ||
return errors.New("at least one config flag must be provided") | ||
} | ||
|
||
set.ConfigProvider, err = NewConfigProvider(newDefaultConfigProviderSettings(configFlags)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
col, err := NewCollector(set) | ||
if err != nil { | ||
return err | ||
} | ||
return col.DryRun(cmd.Context()) | ||
}, | ||
} | ||
validateCmd.Flags().AddGoFlagSet(flagSet) | ||
return validateCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otelcol | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/confmap" | ||
"go.opentelemetry.io/collector/confmap/converter/expandconverter" | ||
"go.opentelemetry.io/collector/confmap/provider/fileprovider" | ||
"go.opentelemetry.io/collector/featuregate" | ||
) | ||
|
||
func TestValidateSubCommandNoConfig(t *testing.T) { | ||
factories, err := nopFactories() | ||
require.NoError(t, err) | ||
|
||
cmd := newValidateSubCommand(CollectorSettings{Factories: factories}, flags(featuregate.GlobalRegistry())) | ||
err = cmd.Execute() | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "at least one config flag must be provided") | ||
} | ||
|
||
func TestValidateSubCommandInvalidComponents(t *testing.T) { | ||
factories, err := nopFactories() | ||
require.NoError(t, err) | ||
|
||
cfgProvider, err := NewConfigProvider( | ||
ConfigProviderSettings{ | ||
ResolverSettings: confmap.ResolverSettings{ | ||
URIs: []string{filepath.Join("testdata", "otelcol-invalid-components.yaml")}, | ||
Providers: map[string]confmap.Provider{"file": fileprovider.New()}, | ||
Converters: []confmap.Converter{expandconverter.New()}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
cmd := newValidateSubCommand(CollectorSettings{Factories: factories, ConfigProvider: cfgProvider}, flags(featuregate.GlobalRegistry())) | ||
err = cmd.Execute() | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "unknown type: \"nosuchprocessor\"") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
receivers: | ||
nop: | ||
exporters: | ||
nop: | ||
processors: | ||
nosuchprocessor: | ||
service: | ||
pipelines: | ||
traces: | ||
receivers: [nop] | ||
exporters: [nop] | ||
processors: [nop] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters