Skip to content

Commit

Permalink
[confmap] log a warning when using $VAR in config
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmota committed Feb 10, 2024
1 parent de6287d commit 04524c2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
38 changes: 29 additions & 9 deletions confmap/converter/expandconverter/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,71 @@ package expandconverter // import "go.opentelemetry.io/collector/confmap/convert

import (
"context"
"fmt"
"os"
"regexp"

"go.opentelemetry.io/collector/confmap"
"go.uber.org/zap"
)

type converter struct{}
type converter struct {
logger *zap.Logger

// Record of which env vars we have logged a warning for
loggedDeprecations map[string]struct{}
}

// New returns a confmap.Converter, that expands all environment variables for a given confmap.Conf.
//
// Notice: This API is experimental.
func New(_ confmap.ConverterSettings) confmap.Converter {
return converter{}
return converter{
loggedDeprecations: make(map[string]struct{}),
logger: zap.NewNop(), // TODO: pass logger in ConverterSettings
}
}

func (converter) Convert(_ context.Context, conf *confmap.Conf) error {
func (c converter) Convert(_ context.Context, conf *confmap.Conf) error {
out := make(map[string]any)
for _, k := range conf.AllKeys() {
out[k] = expandStringValues(conf.Get(k))
out[k] = c.expandStringValues(conf.Get(k))
}
return conf.Merge(confmap.NewFromStringMap(out))
}

func expandStringValues(value any) any {
func (c converter) expandStringValues(value any) any {
switch v := value.(type) {
case string:
return expandEnv(v)
return c.expandEnv(v)
case []any:
nslice := make([]any, 0, len(v))
for _, vint := range v {
nslice = append(nslice, expandStringValues(vint))
nslice = append(nslice, c.expandStringValues(vint))
}
return nslice
case map[string]any:
nmap := map[string]any{}
for mk, mv := range v {
nmap[mk] = expandStringValues(mv)
nmap[mk] = c.expandStringValues(mv)
}
return nmap
default:
return v
}
}

func expandEnv(s string) string {
func (c converter) expandEnv(s string) string {
return os.Expand(s, func(str string) string {
// Matches on $VAR style environment variables
// in order to make sure we don't log a warning for ${VAR}
var regex = regexp.MustCompile(fmt.Sprintf(`\$%s`, str))
if _, exists := c.loggedDeprecations[str]; !exists && regex.MatchString(s) {
msg := fmt.Sprintf("Variable substitution using $VAR will be deprecated in favor of ${VAR} and ${env:VAR}, please update $%s", str)
c.logger.Warn(msg, zap.String("variable", str))
c.loggedDeprecations[str] = struct{}{}
}

// This allows escaping environment variable substitution via $$, e.g.
// - $FOO will be substituted with env var FOO
// - $$FOO will be replaced with $FOO
Expand Down
2 changes: 1 addition & 1 deletion confmap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ require (
github.com/stretchr/testify v1.8.4
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.26.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions confmap/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 04524c2

Please sign in to comment.