Skip to content

[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 56 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
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 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
25 changes: 25 additions & 0 deletions .chloggen/examine-subcommand.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: 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:

# 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: []
1 change: 1 addition & 0 deletions otelcol/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewCommand(set CollectorSettings) *cobra.Command {
}
rootCmd.AddCommand(newComponentsCommand(set))
rootCmd.AddCommand(newValidateSubCommand(set, flagSet))
rootCmd.AddCommand(newExamineSubCommand(set, flagSet))
rootCmd.Flags().AddGoFlagSet(flagSet)
return rootCmd
}
Expand Down
46 changes: 46 additions & 0 deletions otelcol/command_examine.go
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",
Short: "Prints the final yaml configuration after all --config sources are resolved and merged",
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)
}

Check warning on line 39 in otelcol/command_examine.go

View check run for this annotation

Codecov / codecov/patch

otelcol/command_examine.go#L38-L39

Added lines #L38 - L39 were not covered by tests
log.Printf("\n%s", b)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
153 changes: 153 additions & 0 deletions otelcol/command_examine_test.go
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",
},
{
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]
}
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.115.0
go.opentelemetry.io/collector/config/configtelemetry v0.115.0
go.opentelemetry.io/collector/confmap v1.21.0
go.opentelemetry.io/collector/confmap/provider/fileprovider v1.21.0
go.opentelemetry.io/collector/confmap/provider/yamlprovider v1.21.0
go.opentelemetry.io/collector/connector v0.115.0
go.opentelemetry.io/collector/connector/connectortest v0.115.0
go.opentelemetry.io/collector/exporter v0.115.0
Expand Down Expand Up @@ -206,3 +208,7 @@ replace go.opentelemetry.io/collector/extension/extensiontest => ../extension/ex
replace go.opentelemetry.io/collector/extension/auth/authtest => ../extension/auth/authtest

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

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

replace go.opentelemetry.io/collector/confmap/provider/yamlprovider => ../confmap/provider/yamlprovider
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
receviers:
- 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 examine --config=file:file.yaml --config=http:http://remote:8080/config --config=file:file2.yaml
```
Loading