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

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
38 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
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.
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved

# 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
45 changes: 45 additions & 0 deletions otelcol/command_examine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"flag"
"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: "Logs the final 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 err
}
conf, err := resolver.Resolve(cmd.Context())
if err != nil {
return err
}
b, err := yaml.Marshal(conf.ToStringMap())
if err != nil {
return err
}
log.Printf("\n%s", b)

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
}
73 changes: 73 additions & 0 deletions otelcol/command_examine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"testing"

"github.com/stretchr/testify/require"

"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
}
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)
}
})
}
}
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.114.0
go.opentelemetry.io/collector/config/configtelemetry v0.114.0
go.opentelemetry.io/collector/confmap v1.20.0
go.opentelemetry.io/collector/confmap/provider/fileprovider v1.20.0
go.opentelemetry.io/collector/confmap/provider/yamlprovider v1.20.0
go.opentelemetry.io/collector/connector v0.114.0
go.opentelemetry.io/collector/connector/connectortest v0.114.0
go.opentelemetry.io/collector/exporter v0.114.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 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