| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelcol // import "go.opentelemetry.io/collector/otelcol" |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"flag" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/featuregate" |
| 12 |
) |
| 13 |
|
| 14 |
const ( |
| 15 |
configFlag = "config" |
| 16 |
) |
| 17 |
|
| 18 |
type configFlagValue struct { |
| 19 |
values []string |
| 20 |
sets []string |
| 21 |
} |
| 22 |
|
| 23 |
func (s *configFlagValue) Set(val string) error { |
| 24 |
s.values = append(s.values, val) |
| 25 |
return nil |
| 26 |
} |
| 27 |
|
| 28 |
func (s *configFlagValue) String() string { |
| 29 |
return "[" + strings.Join(s.values, ", ") + "]" |
| 30 |
} |
| 31 |
|
| 32 |
func flags(reg *featuregate.Registry) *flag.FlagSet { |
| 33 |
flagSet := new(flag.FlagSet) |
| 34 |
|
| 35 |
cfgs := new(configFlagValue) |
| 36 |
flagSet.Var(cfgs, configFlag, "Locations to the config file(s), note that only a"+ |
| 37 |
" single location can be set per flag entry e.g. `--config=file:/path/to/first --config=file:path/to/second`.") |
| 38 |
|
| 39 |
flagSet.Func("set", |
| 40 |
"Set arbitrary component config property. The component has to be defined in the config file and the flag"+ |
| 41 |
" has a higher precedence. Array config properties are overridden and maps are joined. Example --set=processors.batch.timeout=2s", |
| 42 |
func(s string) error { |
| 43 |
before, after, ok := strings.Cut(s, "=") |
| 44 |
if !ok { |
| 45 |
// No need for more context, see TestSetFlag/invalid_set. |
| 46 |
return errors.New("missing equal sign") |
| 47 |
} |
| 48 |
cfgs.sets = append(cfgs.sets, "yaml:"+strings.TrimSpace(strings.ReplaceAll(before, ".", "::"))+": "+strings.TrimSpace(after)) |
| 49 |
return nil |
| 50 |
}) |
| 51 |
|
| 52 |
reg.RegisterFlags(flagSet) |
| 53 |
return flagSet |
| 54 |
} |
| 55 |
|
| 56 |
func getConfigFlag(flagSet *flag.FlagSet) []string { |
| 57 |
cfv := flagSet.Lookup(configFlag).Value.(*configFlagValue) |
| 58 |
return append(cfv.values, cfv.sets...) |
| 59 |
} |