Skip to content

Commit

Permalink
Deprecate usage of bare env vars and config sources
Browse files Browse the repository at this point in the history
Deprecate usage of bare environment variables and config sources in configuration files.
  - Use `${env:VAR}` or `${VAR}` instead of `$VAR`.
  - Use `${uri:selector}` instead of `${uri:selector}`, e.g. `${file:/path/to/file}` instead of `$file:/path/to/file`.

Detect such usages and print a deprecation warning. It's ok to not use logger because it's not initiated at this point yet. We use `log.Print` at the stage in other places as well.
  • Loading branch information
dmitryax committed Jul 29, 2024
1 parent 4ab756d commit 7470480
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
in v0.42.0 to support a bug causing double expansion. $$ is treated as an escape sequence representing a literal
$ character ([#5134](https://github.com/signalfx/splunk-otel-collector/pull/5134))

### 🚩Deprecations 🚩

- (Splunk) Deprecate usage of bare environment variables and config sources in configuration files.
- Use `${env:VAR}` or `${VAR}` instead of `$VAR`.
- Use `${uri:selector}` instead of `$uri:selector`, e.g. `${file:/path/to/file}` instead of `$file:/path/to/file`.

### 💡 Enhancements 💡

- (Splunk) `discovery`: Enable discovery mode for SQL Server receiver ([#5109](https://github.com/signalfx/splunk-otel-collector/pull/5109))
Expand Down
26 changes: 24 additions & 2 deletions internal/configsource/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ package configsource
import (
"context"
"fmt"
"log"
"net/url"
"os"
"strings"
"sync"

"github.com/knadh/koanf/maps"
"github.com/spf13/cast"
Expand Down Expand Up @@ -334,22 +336,33 @@ func resolveStringValue(ctx context.Context, configSources map[string]ConfigSour
var expandableContent, cfgSrcName string
w := 0 // number of bytes consumed on this pass

var deprecatedFormUsed bool
switch {
case s[j+1] == '{':
expandableContent, w, cfgSrcName = getBracketedExpandableContent(s, j+1)
default:
// TODO: To be deprecated removed to align with the upstream behavior
deprecatedFormUsed = true
expandableContent, w, cfgSrcName = getBareExpandableContent(s, j+1)
}

// At this point expandableContent contains a string to be expanded, evaluate and expand it.
switch {
case cfgSrcName == "":
// Not a config source, expand as os.ExpandEnv
// TODO: Align with confmap.strictlyTypedInput feature gate
if deprecatedFormUsed {
printDeprecationWarningOnce(fmt.Sprintf(
"[WARNING] Variable substitution using $VAR has been deprecated in favor of ${VAR} and "+
"${env:VAR}. Please update $%s in your configuration", expandableContent))
}
buf = osExpandEnv(buf, expandableContent, w)

default:
if deprecatedFormUsed {
printDeprecationWarningOnce(fmt.Sprintf(
"[WARNING] Config source expansion formatted as $uri:selector has been deprecated, "+
"use ${uri:selector[?params]} instead. Please replace $%s with ${%s} in your configuration",
expandableContent, expandableContent))
}
// A config source, retrieve and apply results.
retrieved, closeFunc, err := retrieveConfigSourceData(ctx, configSources, confmapProviders, cfgSrcName, expandableContent, watcher)
if err != nil {
Expand Down Expand Up @@ -705,3 +718,12 @@ func MergeCloseFuncs(closeFuncs []confmap.CloseFunc) confmap.CloseFunc {
return errs
}
}

var deprecationWarningsPrinted = &sync.Map{}

func printDeprecationWarningOnce(msg string) {
if _, ok := deprecationWarningsPrinted.Load(msg); !ok {
deprecationWarningsPrinted.Store(msg, struct{}{})
log.Println(msg)
}
}

0 comments on commit 7470480

Please sign in to comment.