forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support set flag for component configs (open-telemetry#1640)
Signed-off-by: Pavol Loffay <ploffay@redhat.com> This patch adds support for configuring components (receivers, processors, exporters...) via `--set` flag. For example `--set=processors.batch.timeout=12s` or `--receivers.otlp.protocols.grpc.endpoint=123456`. ``` --set stringArray Set arbitrary component config property. The flag has a higher precedence over config file. The arrays are overridden. Example --set=processors.batch.timeout=2s ``` - Flags have higher precedence than config file - The component has to be defined in the config to apply the configuration otherwise the flag is noop e.g. `--set=processor.queued_retry.queue_size=15` is noop if the `queued_retry` is not defined. - The named components work e.g. `--set=processors.batch/bar.timeout=3s` - The maps are joined with the existing (file) config - The arrays are overridden - The object arrays are more complicated - with flag it's possible to set only the first entry e.g. `--set=processors.resource.attributes.key=key2` The implementation creates a new viper instance that is initialized with a temporary properties file that contains values passed via set flag. Then the new viper is used to apply changes to an existing configuration (loaded via the main config file). Resolves open-telemetry#873 Tested locally `RUN_CONFIG=config.yaml make run` ```yaml receivers: otlp: protocols: grpc: exporters: logging: logLevel: debug processors: batch: service: pipelines: traces: receivers: [otlp] processors: [batch] exporters: [logging] ```
- Loading branch information
1 parent
09c4e3d
commit 450fd93
Showing
6 changed files
with
270 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,68 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package service | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
|
||
"go.opentelemetry.io/collector/config" | ||
) | ||
|
||
const ( | ||
setFlagName = "set" | ||
setFlagFileType = "properties" | ||
) | ||
|
||
func addSetFlag(flagSet *pflag.FlagSet) { | ||
flagSet.StringArray(setFlagName, []string{}, "Set arbitrary component config property. The component has to be defined in the config file and the flag has a higher precedence. Array config properties are overridden and maps are joined, note that only a single (first) array property can be set e.g. -set=processors.attributes.actions.key=some_key. Example --set=processors.batch.timeout=2s") | ||
} | ||
|
||
// AddSetFlagProperties overrides properties from set flag(s) in supplied viper instance. | ||
// The implementation reads set flag(s) from the cmd and passes the content to a new viper instance as .properties file. | ||
// Then the properties from new viper instance are read and set to the supplied viper. | ||
func AddSetFlagProperties(v *viper.Viper, cmd *cobra.Command) error { | ||
flagProperties, err := cmd.Flags().GetStringArray(setFlagName) | ||
if err != nil { | ||
return err | ||
} | ||
if len(flagProperties) == 0 { | ||
return nil | ||
} | ||
b := &bytes.Buffer{} | ||
for _, property := range flagProperties { | ||
property = strings.TrimSpace(property) | ||
if _, err := fmt.Fprintf(b, "%s\n", property); err != nil { | ||
return err | ||
} | ||
} | ||
viperFlags := config.NewViper() | ||
viperFlags.SetConfigType(setFlagFileType) | ||
if err := viperFlags.ReadConfig(b); err != nil { | ||
return fmt.Errorf("failed to read set flag config: %v", err) | ||
} | ||
|
||
// flagProperties cannot be applied to v directly because | ||
// v.MergeConfig(io.Reader) or v.MergeConfigMap(map[string]interface) does not work properly. | ||
for _, k := range viperFlags.AllKeys() { | ||
v.Set(k, viperFlags.Get(k)) | ||
} | ||
return nil | ||
} |
This file contains 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,64 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package service | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSetFlags(t *testing.T) { | ||
cmd := &cobra.Command{} | ||
addSetFlag(cmd.Flags()) | ||
|
||
err := cmd.ParseFlags([]string{ | ||
"--set=processors.batch.timeout=2s", | ||
"--set=processors.batch/foo.timeout=3s", | ||
"--set=receivers.otlp.protocols.grpc.endpoint=localhost:1818", | ||
"--set=exporters.kafka.brokers=foo:9200,foo2:9200", | ||
}) | ||
require.NoError(t, err) | ||
|
||
v := viper.New() | ||
err = AddSetFlagProperties(v, cmd) | ||
require.NoError(t, err) | ||
|
||
settings := v.AllSettings() | ||
assert.Equal(t, 4, len(settings)) | ||
assert.Equal(t, "2s", v.Get("processors::batch::timeout")) | ||
assert.Equal(t, "3s", v.Get("processors::batch/foo::timeout")) | ||
assert.Equal(t, "foo:9200,foo2:9200", v.Get("exporters::kafka::brokers")) | ||
assert.Equal(t, "localhost:1818", v.Get("receivers::otlp::protocols::grpc::endpoint")) | ||
} | ||
|
||
func TestSetFlags_err_set_flag(t *testing.T) { | ||
cmd := &cobra.Command{} | ||
v := viper.New() | ||
err := AddSetFlagProperties(v, cmd) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestSetFlags_empty(t *testing.T) { | ||
cmd := &cobra.Command{} | ||
addSetFlag(cmd.Flags()) | ||
v := viper.New() | ||
err := AddSetFlagProperties(v, cmd) | ||
require.NoError(t, err) | ||
assert.Equal(t, 0, len(v.AllSettings())) | ||
} |
This file contains 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