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

[service]: add new subcommand to examine the initial configuration #11775

Merged
merged 56 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
b8b934f
chore: add new subcommand, tests, changelog
VihasMakwana Nov 29, 2024
cffca74
chore: readme
VihasMakwana Nov 29, 2024
c0a2032
chore: add newline
VihasMakwana Nov 29, 2024
5ce60ba
adjust imports
VihasMakwana Nov 29, 2024
774c56d
chore: lint
VihasMakwana Nov 29, 2024
e0985ef
Merge branch 'main' into examine-subcommand
VihasMakwana Dec 2, 2024
255b05f
Update otelcol/command_examine.go
VihasMakwana Dec 2, 2024
133a591
Merge branch 'main' into examine-subcommand
VihasMakwana Dec 2, 2024
e2bbdf8
Merge branch 'main' into examine-subcommand
VihasMakwana Dec 5, 2024
9b87eac
Merge branch 'main' into examine-subcommand
VihasMakwana Dec 7, 2024
a9f80eb
chore: add test cases
VihasMakwana Dec 7, 2024
fae0381
expand tests
VihasMakwana Dec 7, 2024
e6df555
improve test cases
VihasMakwana Dec 7, 2024
066bf60
lint
VihasMakwana Dec 8, 2024
16e1a70
improve error
VihasMakwana Dec 8, 2024
21b48de
chore: tests
VihasMakwana Dec 10, 2024
c83a4b8
chore: lint
VihasMakwana Dec 10, 2024
ab3de36
close file
VihasMakwana Dec 10, 2024
a226e45
Merge branch 'main' into examine-subcommand
VihasMakwana Dec 26, 2024
a5e0491
Update otelcol/testdata/configs/2-config-output.yaml
VihasMakwana Jan 16, 2025
06c9f23
Update otelcol/command_examine.go
VihasMakwana Jan 27, 2025
2104566
Update otelcol/command_examine.go
VihasMakwana Jan 27, 2025
73dd46b
rename command
VihasMakwana Jan 27, 2025
93803a1
update changelog
VihasMakwana Jan 27, 2025
19d5f20
Merge branch 'main' into examine-subcommand
VihasMakwana Jan 27, 2025
dcebb60
go.mod
VihasMakwana Jan 27, 2025
c3ec2f6
Merge branch 'main' into examine-subcommand
mx-psi Jan 31, 2025
22dbd83
Update otelcol/command_print.go
VihasMakwana Jan 31, 2025
7b7bb60
Update otelcol/command_print.go
VihasMakwana Jan 31, 2025
720e8da
Merge branch 'main' into examine-subcommand
VihasMakwana Feb 3, 2025
3e1b8bc
add warning
VihasMakwana Feb 3, 2025
954fe99
chore: make gotidy
VihasMakwana Feb 3, 2025
76c3ee2
Merge branch 'main' into examine-subcommand
VihasMakwana Feb 4, 2025
f811ef5
fix tests
VihasMakwana Feb 4, 2025
a86936d
lint
VihasMakwana Feb 4, 2025
27c7c16
Merge branch 'main' into examine-subcommand
VihasMakwana Feb 4, 2025
2b9991d
Merge branch 'main' into examine-subcommand
VihasMakwana Feb 10, 2025
52e1abf
more test coverage
VihasMakwana Feb 10, 2025
b4881d1
Merge branch 'main' into examine-subcommand
VihasMakwana Feb 12, 2025
8f8cb8a
Update otelcol/testdata/configs/1-config-first.yaml
VihasMakwana Feb 13, 2025
2682bb4
Update otelcol/testdata/configs/1-config-output.yaml
VihasMakwana Feb 13, 2025
874eff4
Update otelcol/testdata/configs/1-config-second.yaml
VihasMakwana Feb 13, 2025
4946aef
Update service/README.md
VihasMakwana Feb 13, 2025
e3d939d
Update otelcol/command_print.go
VihasMakwana Feb 13, 2025
0a12882
Update otelcol/command_print.go
VihasMakwana Feb 13, 2025
f2e6a45
Update .chloggen/examine-subcommand.yaml
VihasMakwana Feb 13, 2025
6254c59
Update otelcol/command_print.go
VihasMakwana Feb 13, 2025
15806bd
Update .chloggen/examine-subcommand.yaml
VihasMakwana Feb 13, 2025
366240d
Merge branch 'main' into examine-subcommand
VihasMakwana Feb 13, 2025
3ccba25
go tidy
VihasMakwana Feb 13, 2025
0e492b1
fix test
VihasMakwana Feb 13, 2025
e51cae5
crosslink
VihasMakwana Feb 13, 2025
31b14fa
go tidy
VihasMakwana Feb 13, 2025
df3d2b4
Merge branch 'main' into examine-subcommand
VihasMakwana Feb 19, 2025
c0fd7df
Merge branch 'main' into examine-subcommand
VihasMakwana Feb 20, 2025
9116f55
gotidy
VihasMakwana Feb 20, 2025
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
28 changes: 28 additions & 0 deletions .chloggen/examine-subcommand.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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: Create 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: |
To use the `print-initial-config` subcommand, invoke the Collector with the subcommand and corresponding feature gate: `otelcol print-initial-config --feature-gates=otelcol.printInitialConfig --config=config.yaml`.
Note that the feature gate enabling this flag is currently in alpha stability, and the subcommand may
be changed in the future.

# 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: []
4 changes: 4 additions & 0 deletions internal/e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,7 @@ replace go.opentelemetry.io/collector/extension/auth/authtest => ../../extension
replace go.opentelemetry.io/collector/extension/xextension => ../../extension/xextension

replace go.opentelemetry.io/collector/otelcol => ../../otelcol

replace go.opentelemetry.io/collector/confmap/provider/yamlprovider => ../../confmap/provider/yamlprovider

replace go.opentelemetry.io/collector/confmap/provider/fileprovider => ../../confmap/provider/fileprovider
1 change: 1 addition & 0 deletions otelcol/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewCommand(set CollectorSettings) *cobra.Command {
rootCmd.AddCommand(newFeatureGateCommand())
rootCmd.AddCommand(newComponentsCommand(set))
rootCmd.AddCommand(newValidateSubCommand(set, flagSet))
rootCmd.AddCommand(newConfigPrintSubCommand(set, flagSet))
rootCmd.Flags().AddGoFlagSet(flagSet)
return rootCmd
}
Expand Down
58 changes: 58 additions & 0 deletions otelcol/command_print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otelcol // import "go.opentelemetry.io/collector/otelcol"

import (
"errors"
"flag"
"fmt"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/featuregate"
)

var printCommandFeatureFlag = featuregate.GlobalRegistry().MustRegister(
"otelcol.printInitialConfig",
featuregate.StageAlpha,
featuregate.WithRegisterFromVersion("v0.120.0"),
featuregate.WithRegisterDescription("if set to true, turns on the print-initial-config command"),
)

// newConfigPrintSubCommand constructs a new config print sub command using the given CollectorSettings.
func newConfigPrintSubCommand(set CollectorSettings, flagSet *flag.FlagSet) *cobra.Command {
cmd := &cobra.Command{
Use: "print-initial-config",
Short: "Prints the Collector's configuration in YAML format after all config sources are resolved and merged",
Long: `Note: This command prints the final yaml configuration before it is unmarshaled into config structs, which may contain sensitive values.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, _ []string) error {
if !printCommandFeatureFlag.IsEnabled() {
return errors.New("print-initial-config is currently experimental, use the otelcol.printInitialConfig feature gate to enable this command")
}
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)
}

Check warning on line 51 in otelcol/command_print.go

View check run for this annotation

Codecov / codecov/patch

otelcol/command_print.go#L50-L51

Added lines #L50 - L51 were not covered by tests
fmt.Printf("%s\n", b)
return nil
},
}
cmd.Flags().AddGoFlagSet(flagSet)
return cmd
}
179 changes: 179 additions & 0 deletions otelcol/command_print_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otelcol // import "go.opentelemetry.io/collector/otelcol"

import (
"bytes"
"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 TestPrintCommand(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",
},
{
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) {
err := featuregate.GlobalRegistry().Set(printCommandFeatureFlag.ID(), true)
require.NoError(t, err)
defer func() {
_ = featuregate.GlobalRegistry().Set(printCommandFeatureFlag.ID(), false)
}()

set := ConfigProviderSettings{
ResolverSettings: test.set,
}
cmd := newConfigPrintSubCommand(CollectorSettings{ConfigProviderSettings: set}, flags(featuregate.GlobalRegistry()))
err = cmd.Execute()
if test.errString != "" {
require.ErrorContains(t, err, test.errString)
} else {
require.NoError(t, err)
}
})
}
}

func TestPrintCommandFeaturegateDisabled(t *testing.T) {
cmd := newConfigPrintSubCommand(CollectorSettings{ConfigProviderSettings: ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{"yaml:processors::batch/foo::timeout: 3s"},
DefaultScheme: "foo",
ProviderFactories: []confmap.ProviderFactory{
yamlprovider.NewFactory(),
},
},
}}, flags(featuregate.GlobalRegistry()))
err := cmd.Execute()
require.ErrorContains(t, err, "print-initial-config is currently experimental, use the otelcol.printInitialConfig feature gate to enable this command")
}

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::receivers: [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) {
err := featuregate.GlobalRegistry().Set(printCommandFeatureFlag.ID(), true)
require.NoError(t, err)
defer func() {
_ = featuregate.GlobalRegistry().Set(printCommandFeatureFlag.ID(), false)
}()
set := ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: test.configs,
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
yamlprovider.NewFactory(),
},
DefaultScheme: "file",
},
}
tmpFile, err := os.CreateTemp(t.TempDir(), "*")
require.NoError(t, err)
t.Cleanup(func() { _ = tmpFile.Close() })

// save the os.Stdout and temporarily set it to temp file
oldStdout := os.Stdout
os.Stdout = tmpFile

cmd := newConfigPrintSubCommand(CollectorSettings{ConfigProviderSettings: set}, flags(featuregate.GlobalRegistry()))
require.NoError(t, cmd.Execute())

// restore os.Stdout
os.Stdout = oldStdout

expectedOutput, err := os.ReadFile(test.finalConfig)
require.NoError(t, err)

actualOutput, err := os.ReadFile(tmpFile.Name())
require.NoError(t, err)

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)
})
}
}
6 changes: 6 additions & 0 deletions otelcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require (
go.opentelemetry.io/collector/component/componentstatus v0.120.0
go.opentelemetry.io/collector/config/configtelemetry v0.120.0
go.opentelemetry.io/collector/confmap v1.26.0
go.opentelemetry.io/collector/confmap/provider/fileprovider v1.25.0
go.opentelemetry.io/collector/confmap/provider/yamlprovider v1.25.0
go.opentelemetry.io/collector/confmap/xconfmap v0.120.0
go.opentelemetry.io/collector/connector v0.120.0
go.opentelemetry.io/collector/connector/connectortest v0.120.0
Expand Down Expand Up @@ -209,4 +211,8 @@ replace go.opentelemetry.io/collector/extension/auth/authtest => ../extension/au

replace go.opentelemetry.io/collector/extension/xextension => ../extension/xextension

replace go.opentelemetry.io/collector/confmap/provider/fileprovider => ../confmap/provider/fileprovider

replace go.opentelemetry.io/collector/confmap/provider/yamlprovider => ../confmap/provider/yamlprovider

replace go.opentelemetry.io/collector/internal/telemetry => ../internal/telemetry
6 changes: 6 additions & 0 deletions otelcol/testdata/configs/1-config-first.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
receivers:
bar:
key: val
exporters:
bar:
key: val
10 changes: 10 additions & 0 deletions otelcol/testdata/configs/1-config-output.yaml
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
6 changes: 6 additions & 0 deletions otelcol/testdata/configs/1-config-second.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
receivers:
foo:
key: val
exporters:
foo:
key: val
19 changes: 19 additions & 0 deletions otelcol/testdata/configs/2-config-output.yaml
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
receivers:
- foo
- bar
6 changes: 6 additions & 0 deletions service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,9 @@ extensions:
```bash
./otelcorecol validate --config=file:examples/local/otel-config.yaml
```

## How to examine the final configuration after merging and resolving from various sources?

```bash
./otelcorecol print-initial-config --config=file:file.yaml --config=http:http://remote:8080/config --config=file:file2.yaml
```
4 changes: 4 additions & 0 deletions service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,7 @@ replace go.opentelemetry.io/collector/extension/auth/authtest => ../extension/au
replace go.opentelemetry.io/collector/extension/xextension => ../extension/xextension

replace go.opentelemetry.io/collector/otelcol => ../otelcol

replace go.opentelemetry.io/collector/confmap/provider/yamlprovider => ../confmap/provider/yamlprovider

replace go.opentelemetry.io/collector/confmap/provider/fileprovider => ../confmap/provider/fileprovider
Loading