-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[service]: add new subcommand to examine the initial configuration #11775
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
b8b934f
chore: add new subcommand, tests, changelog
VihasMakwana cffca74
chore: readme
VihasMakwana c0a2032
chore: add newline
VihasMakwana 5ce60ba
adjust imports
VihasMakwana 774c56d
chore: lint
VihasMakwana e0985ef
Merge branch 'main' into examine-subcommand
VihasMakwana 255b05f
Update otelcol/command_examine.go
VihasMakwana 133a591
Merge branch 'main' into examine-subcommand
VihasMakwana e2bbdf8
Merge branch 'main' into examine-subcommand
VihasMakwana 9b87eac
Merge branch 'main' into examine-subcommand
VihasMakwana a9f80eb
chore: add test cases
VihasMakwana fae0381
expand tests
VihasMakwana e6df555
improve test cases
VihasMakwana 066bf60
lint
VihasMakwana 16e1a70
improve error
VihasMakwana 21b48de
chore: tests
VihasMakwana c83a4b8
chore: lint
VihasMakwana ab3de36
close file
VihasMakwana a226e45
Merge branch 'main' into examine-subcommand
VihasMakwana a5e0491
Update otelcol/testdata/configs/2-config-output.yaml
VihasMakwana 06c9f23
Update otelcol/command_examine.go
VihasMakwana 2104566
Update otelcol/command_examine.go
VihasMakwana 73dd46b
rename command
VihasMakwana 93803a1
update changelog
VihasMakwana 19d5f20
Merge branch 'main' into examine-subcommand
VihasMakwana dcebb60
go.mod
VihasMakwana c3ec2f6
Merge branch 'main' into examine-subcommand
mx-psi 22dbd83
Update otelcol/command_print.go
VihasMakwana 7b7bb60
Update otelcol/command_print.go
VihasMakwana 720e8da
Merge branch 'main' into examine-subcommand
VihasMakwana 3e1b8bc
add warning
VihasMakwana 954fe99
chore: make gotidy
VihasMakwana 76c3ee2
Merge branch 'main' into examine-subcommand
VihasMakwana f811ef5
fix tests
VihasMakwana a86936d
lint
VihasMakwana 27c7c16
Merge branch 'main' into examine-subcommand
VihasMakwana 2b9991d
Merge branch 'main' into examine-subcommand
VihasMakwana 52e1abf
more test coverage
VihasMakwana b4881d1
Merge branch 'main' into examine-subcommand
VihasMakwana 8f8cb8a
Update otelcol/testdata/configs/1-config-first.yaml
VihasMakwana 2682bb4
Update otelcol/testdata/configs/1-config-output.yaml
VihasMakwana 874eff4
Update otelcol/testdata/configs/1-config-second.yaml
VihasMakwana 4946aef
Update service/README.md
VihasMakwana e3d939d
Update otelcol/command_print.go
VihasMakwana 0a12882
Update otelcol/command_print.go
VihasMakwana f2e6a45
Update .chloggen/examine-subcommand.yaml
VihasMakwana 6254c59
Update otelcol/command_print.go
VihasMakwana 15806bd
Update .chloggen/examine-subcommand.yaml
VihasMakwana 366240d
Merge branch 'main' into examine-subcommand
VihasMakwana 3ccba25
go tidy
VihasMakwana 0e492b1
fix test
VihasMakwana e51cae5
crosslink
VihasMakwana 31b14fa
go tidy
VihasMakwana df3d2b4
Merge branch 'main' into examine-subcommand
VihasMakwana c0fd7df
Merge branch 'main' into examine-subcommand
VihasMakwana 9116f55
gotidy
VihasMakwana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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: service | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: A new subcommand to dump the initial configuration after resolving/merging. | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [11479] | ||
|
||
# (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: | ||
VihasMakwana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# 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: [] |
This file contains hidden or 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 hidden or 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 "go.opentelemetry.io/collector/otelcol" | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
|
||
"go.opentelemetry.io/collector/confmap" | ||
) | ||
|
||
// newExamineSubCommand constructs a new examine sub command using the given CollectorSettings. | ||
func newExamineSubCommand(set CollectorSettings, flagSet *flag.FlagSet) *cobra.Command { | ||
examineCmd := &cobra.Command{ | ||
Use: "examine", | ||
VihasMakwana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Short: "Prints the final yaml configuration after all --config sources are resolved and merged", | ||
VihasMakwana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
err := updateSettingsUsingFlags(&set, flagSet) | ||
if err != nil { | ||
return err | ||
} | ||
resolver, err := confmap.NewResolver(set.ConfigProviderSettings.ResolverSettings) | ||
if err != nil { | ||
return fmt.Errorf("failed to create new resolver: %w", err) | ||
} | ||
conf, err := resolver.Resolve(cmd.Context()) | ||
if err != nil { | ||
return fmt.Errorf("error while resolving config: %w", err) | ||
} | ||
b, err := yaml.Marshal(conf.ToStringMap()) | ||
if err != nil { | ||
return fmt.Errorf("error while marshaling to yaml: %w", err) | ||
VihasMakwana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
log.Printf("\n%s", b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change to fmt.Printf here? This output is likely to be piped/redirected and I would rather not have a random timestamp at the start of the output. |
||
return nil | ||
}, | ||
} | ||
examineCmd.Flags().AddGoFlagSet(flagSet) | ||
return examineCmd | ||
} |
This file contains hidden or 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,153 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otelcol // import "go.opentelemetry.io/collector/otelcol" | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
|
||
"go.opentelemetry.io/collector/confmap" | ||
"go.opentelemetry.io/collector/confmap/provider/fileprovider" | ||
"go.opentelemetry.io/collector/confmap/provider/yamlprovider" | ||
"go.opentelemetry.io/collector/featuregate" | ||
) | ||
|
||
func TestExamineCommand(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
set confmap.ResolverSettings | ||
errString string | ||
}{ | ||
{ | ||
name: "no URIs", | ||
set: confmap.ResolverSettings{}, | ||
errString: "at least one config flag must be provided", | ||
}, | ||
{ | ||
name: "valid URI - file not found", | ||
set: confmap.ResolverSettings{ | ||
URIs: []string{"file:blabla.yaml"}, | ||
ProviderFactories: []confmap.ProviderFactory{ | ||
fileprovider.NewFactory(), | ||
}, | ||
DefaultScheme: "file", | ||
}, | ||
errString: "cannot retrieve the configuration: unable to read the file", | ||
}, | ||
{ | ||
name: "valid URI", | ||
set: confmap.ResolverSettings{ | ||
URIs: []string{"yaml:processors::batch/foo::timeout: 3s"}, | ||
ProviderFactories: []confmap.ProviderFactory{ | ||
yamlprovider.NewFactory(), | ||
}, | ||
DefaultScheme: "yaml", | ||
}, | ||
}, | ||
{ | ||
name: "valid URI - no provider set", | ||
set: confmap.ResolverSettings{ | ||
URIs: []string{"yaml:processors::batch/foo::timeout: 3s"}, | ||
DefaultScheme: "yaml", | ||
}, | ||
errString: "at least one Provider must be supplied", | ||
}, | ||
VihasMakwana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
name: "valid URI - invalid scheme name", | ||
set: confmap.ResolverSettings{ | ||
URIs: []string{"yaml:processors::batch/foo::timeout: 3s"}, | ||
DefaultScheme: "foo", | ||
ProviderFactories: []confmap.ProviderFactory{ | ||
yamlprovider.NewFactory(), | ||
}, | ||
}, | ||
errString: "configuration: DefaultScheme not found in providers list", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
set := ConfigProviderSettings{ | ||
ResolverSettings: test.set, | ||
} | ||
|
||
cmd := newExamineSubCommand(CollectorSettings{ConfigProviderSettings: set}, flags(featuregate.GlobalRegistry())) | ||
err := cmd.Execute() | ||
if test.errString != "" { | ||
require.ErrorContains(t, err, test.errString) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
configs []string | ||
finalConfig string | ||
}{ | ||
{ | ||
name: "two-configs", | ||
configs: []string{ | ||
"file:testdata/configs/1-config-first.yaml", | ||
"file:testdata/configs/1-config-second.yaml", | ||
}, | ||
finalConfig: "testdata/configs/1-config-output.yaml", | ||
}, | ||
{ | ||
name: "two-configs-yaml", | ||
configs: []string{ | ||
"file:testdata/configs/1-config-first.yaml", | ||
"file:testdata/configs/1-config-second.yaml", | ||
"yaml:service::pipelines::logs::receviers: [foo,bar]", | ||
"yaml:service::pipelines::logs::exporters: [foo,bar]", | ||
}, | ||
finalConfig: "testdata/configs/2-config-output.yaml", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
set := ConfigProviderSettings{ | ||
ResolverSettings: confmap.ResolverSettings{ | ||
URIs: test.configs, | ||
ProviderFactories: []confmap.ProviderFactory{ | ||
fileprovider.NewFactory(), | ||
yamlprovider.NewFactory(), | ||
}, | ||
DefaultScheme: "file", | ||
}, | ||
} | ||
var logOutput bytes.Buffer | ||
log.SetOutput(&logOutput) | ||
|
||
cmd := newExamineSubCommand(CollectorSettings{ConfigProviderSettings: set}, flags(featuregate.GlobalRegistry())) | ||
require.NoError(t, cmd.Execute()) | ||
|
||
expectedOutput, err := os.ReadFile(test.finalConfig) | ||
require.NoError(t, err) | ||
|
||
actualOutput := extractYAML(t, logOutput.Bytes()) | ||
|
||
actualConfig := make(map[string]any, 0) | ||
expectedConfig := make(map[string]any, 0) | ||
|
||
require.NoError(t, yaml.Unmarshal(bytes.TrimSpace(actualOutput), actualConfig)) | ||
require.NoError(t, yaml.Unmarshal(bytes.TrimSpace(expectedOutput), expectedConfig)) | ||
|
||
require.Equal(t, expectedConfig, actualConfig) | ||
}) | ||
} | ||
} | ||
|
||
func extractYAML(t *testing.T, b []byte) []byte { | ||
arr := bytes.SplitN(b, []byte("\n"), 2) | ||
require.Greater(t, len(arr), 1) | ||
return arr[1] | ||
} |
This file contains hidden or 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 hidden or 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,6 @@ | ||
receivers: | ||
bar: | ||
key: val | ||
exporters: | ||
bar: | ||
key: val | ||
VihasMakwana marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,10 @@ | ||
exporters: | ||
bar: | ||
key: val | ||
foo: | ||
key: val | ||
receivers: | ||
bar: | ||
key: val | ||
foo: | ||
key: val | ||
VihasMakwana marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,6 @@ | ||
receivers: | ||
foo: | ||
key: val | ||
exporters: | ||
foo: | ||
key: val | ||
VihasMakwana marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,19 @@ | ||
exporters: | ||
bar: | ||
key: val | ||
foo: | ||
key: val | ||
receivers: | ||
bar: | ||
key: val | ||
foo: | ||
key: val | ||
service: | ||
pipelines: | ||
logs: | ||
exporters: | ||
- foo | ||
- bar | ||
receviers: | ||
VihasMakwana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- foo | ||
- bar |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.