Skip to content

Commit e66ff9c

Browse files
Enable feature-gates for otel subcommands (#4198)
Added support for feature gates for otel and validate commands How to test - Build/Package agent - run ./elastic-agent otel --feature-gates "-telemetry.useOtelForInternalMetrics" as this is stable feature gate, you should see error returned saying useOtelForInternalMetrics is stable and cannot be disabled - running ./elastic-agent otel --feature-gates telemetry.useOtelForInternalMetrics (difference is - in front of a gate name) should result in a warning message in output saying it's stable and collector running normally - running ./elastic-agent otel -h needs to include flag in help message
1 parent 4fa6bb6 commit e66ff9c

File tree

8 files changed

+257
-228
lines changed

8 files changed

+257
-228
lines changed

NOTICE.txt

+212-212
Large diffs are not rendered by default.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ require (
6565
go.opentelemetry.io/collector/exporter v0.93.0
6666
go.opentelemetry.io/collector/exporter/debugexporter v0.93.0
6767
go.opentelemetry.io/collector/exporter/otlpexporter v0.93.0
68+
go.opentelemetry.io/collector/featuregate v1.0.1
6869
go.opentelemetry.io/collector/otelcol v0.93.0
6970
go.opentelemetry.io/collector/processor v0.93.0
7071
go.opentelemetry.io/collector/processor/batchprocessor v0.93.0
@@ -214,7 +215,6 @@ require (
214215
go.opentelemetry.io/collector/consumer v0.93.0 // indirect
215216
go.opentelemetry.io/collector/extension v0.93.0 // indirect
216217
go.opentelemetry.io/collector/extension/auth v0.93.0 // indirect
217-
go.opentelemetry.io/collector/featuregate v1.0.1 // indirect
218218
go.opentelemetry.io/collector/pdata v1.0.1 // indirect
219219
go.opentelemetry.io/collector/semconv v0.93.0 // indirect
220220
go.opentelemetry.io/collector/service v0.93.0 // indirect

internal/pkg/agent/cmd/common.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command {
4242
}
4343

4444
// Init version information contained in package version file
45-
err := version.InitVersionError()
46-
if err != nil {
47-
cmd.PrintErrf("Error initializing version information: %v\n", err)
45+
if isOtel := len(args) > 1 && args[1] == "otel"; !isOtel {
46+
err := version.InitVersionError()
47+
if err != nil {
48+
cmd.PrintErrf("Error initializing version information: %v\n", err)
49+
}
4850
}
4951

5052
// path flags

internal/pkg/agent/cmd/otel.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ func newOtelCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Comman
4848
c.Root().HelpFunc()(c, s)
4949
})
5050

51-
cmd.Flags().StringArray(configFlagName, []string{}, "Locations to the config file(s), note that only a"+
52-
" single location can be set per flag entry e.g. `--config=file:/path/to/first --config=file:path/to/second`.")
53-
54-
cmd.Flags().StringArray(setFlagName, []string{}, "Set arbitrary component config property. The component has to be defined in the config file and the flag"+
55-
" has a higher precedence. Array config properties are overridden and maps are joined. Example --set=processors.batch.timeout=2s")
56-
51+
setupOtelFlags(cmd.Flags())
5752
cmd.AddCommand(newValidateCommandWithArgs(args, streams))
5853

5954
return cmd

internal/pkg/agent/cmd/otel_flags.go

+16
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,30 @@
55
package cmd
66

77
import (
8+
"flag"
89
"fmt"
910
"strings"
1011

1112
"github.com/spf13/cobra"
13+
"github.com/spf13/pflag"
14+
"go.opentelemetry.io/collector/featuregate"
1215

1316
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
1417
)
1518

19+
func setupOtelFlags(flags *pflag.FlagSet) {
20+
flags.StringArray(configFlagName, []string{}, "Locations to the config file(s), note that only a"+
21+
" single location can be set per flag entry e.g. `--config=file:/path/to/first --config=file:path/to/second`.")
22+
23+
flags.StringArray(setFlagName, []string{}, "Set arbitrary component config property. The component has to be defined in the config file and the flag"+
24+
" has a higher precedence. Array config properties are overridden and maps are joined. Example --set=processors.batch.timeout=2s")
25+
26+
goFlags := new(flag.FlagSet)
27+
featuregate.GlobalRegistry().RegisterFlags(goFlags)
28+
29+
flags.AddGoFlagSet(goFlags)
30+
}
31+
1632
func getConfigFiles(cmd *cobra.Command, useDefault bool) ([]string, error) {
1733
configFiles, err := cmd.Flags().GetStringArray(configFlagName)
1834
if err != nil {

internal/pkg/agent/cmd/otel_flags_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,26 @@ package cmd
77
import (
88
"testing"
99

10+
"github.com/spf13/pflag"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
1213
)
1314

15+
func TestOtelFlagsSetup(t *testing.T) {
16+
fs := new(pflag.FlagSet)
17+
setupOtelFlags(fs)
18+
19+
expectedFlags := []string{
20+
configFlagName,
21+
setFlagName,
22+
"feature-gates",
23+
}
24+
25+
for _, expectedFlag := range expectedFlags {
26+
require.NotNil(t, fs.Lookup(expectedFlag), "Flag %q is not present", expectedFlag)
27+
}
28+
}
29+
1430
func TestGetSets(t *testing.T) {
1531
testCases := []struct {
1632
name string

internal/pkg/agent/cmd/validate.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ func newValidateCommandWithArgs(_ []string, _ *cli.IOStreams) *cobra.Command {
2828
},
2929
}
3030

31-
cmd.Flags().StringArray(configFlagName, []string{}, "Locations to the config file(s), note that only a"+
32-
" single location can be set per flag entry e.g. `--config=file:/path/to/first --config=file:path/to/second`.")
33-
34-
cmd.Flags().StringArray(setFlagName, []string{}, "Set arbitrary component config property. The component has to be defined in the config file and the flag"+
35-
" has a higher precedence. Array config properties are overridden and maps are joined. Example --set=processors.batch.timeout=2s")
36-
31+
setupOtelFlags(cmd.Flags())
3732
cmd.SetHelpFunc(func(c *cobra.Command, s []string) {
3833
hideInheritedFlags(c)
3934
c.Root().HelpFunc()(c, s)

testing/integration/otel_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ func validateCommandIsWorking(t *testing.T, ctx context.Context, fixture *aTesti
203203
require.NoError(t, err)
204204
require.Equal(t, 0, len(out)) // no error printed out
205205

206+
// check feature gate works
207+
out, err = fixture.Exec(ctx, []string{"otel", "validate", "--config", cfgFilePath, "--feature-gates", "foo.bar"})
208+
require.Error(t, err)
209+
require.Contains(t, string(out), `no such feature gate "foo.bar"`)
210+
206211
// check `elastic-agent otel validate` command works for invalid otel config
207212
cfgFilePath = filepath.Join(tempDir, "otel-invalid.yml")
208213
require.NoError(t, os.WriteFile(cfgFilePath, []byte(fileInvalidOtelConfig), 0600))

0 commit comments

Comments
 (0)