diff --git a/.chloggen/confmap-converter-logger.yaml b/.chloggen/confmap-converter-logger.yaml new file mode 100644 index 00000000000..c93ebb4873d --- /dev/null +++ b/.chloggen/confmap-converter-logger.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: confmap + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Allow Converters to write logs during startup + +# One or more tracking issues or pull requests related to the change +issues: [10135] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/confmap/converter.go b/confmap/converter.go index 2dce87e496e..0e3a5c03f2e 100644 --- a/confmap/converter.go +++ b/confmap/converter.go @@ -5,10 +5,18 @@ package confmap // import "go.opentelemetry.io/collector/confmap" import ( "context" + + "go.uber.org/zap" ) // ConverterSettings are the settings to initialize a Converter. -type ConverterSettings struct{} +type ConverterSettings struct { + // Logger is a zap.Logger that will be passed to Converters. + // Converters should be able to rely on the Logger being non-nil; + // when instantiating a Converter with a ConverterFactory, + // nil Logger references should be replaced with a no-op Logger. + Logger *zap.Logger +} // ConverterFactory defines a factory that can be used to instantiate // new instances of a Converter. diff --git a/confmap/converter/expandconverter/expand.go b/confmap/converter/expandconverter/expand.go index ea6977fda0f..98bc455d907 100644 --- a/confmap/converter/expandconverter/expand.go +++ b/confmap/converter/expandconverter/expand.go @@ -27,10 +27,10 @@ func NewFactory() confmap.ConverterFactory { return confmap.NewConverterFactory(newConverter) } -func newConverter(_ confmap.ConverterSettings) confmap.Converter { +func newConverter(set confmap.ConverterSettings) confmap.Converter { return converter{ loggedDeprecations: make(map[string]struct{}), - logger: zap.NewNop(), // TODO: pass logger in ConverterSettings + logger: set.Logger, } } @@ -80,6 +80,12 @@ func (c converter) expandEnv(s string) string { if str == "$" { return "$" } - return os.Getenv(str) + val, exists := os.LookupEnv(str) + if !exists { + c.logger.Warn("Configuration references unset environment variable", zap.String("name", str)) + } else if len(val) == 0 { + c.logger.Info("Configuration references empty environment variable", zap.String("name", str)) + } + return val }) } diff --git a/confmap/converter/expandconverter/expand_test.go b/confmap/converter/expandconverter/expand_test.go index 424a8be0319..fbc58a2e1f2 100644 --- a/confmap/converter/expandconverter/expand_test.go +++ b/confmap/converter/expandconverter/expand_test.go @@ -254,5 +254,5 @@ func TestDeprecatedWarning(t *testing.T) { } func createConverter() confmap.Converter { - return NewFactory().Create(confmap.ConverterSettings{}) + return NewFactory().Create(confmap.ConverterSettings{Logger: zap.NewNop()}) } diff --git a/confmap/resolver.go b/confmap/resolver.go index 05f7f964d03..c3de276e245 100644 --- a/confmap/resolver.go +++ b/confmap/resolver.go @@ -94,6 +94,10 @@ func NewResolver(set ResolverSettings) (*Resolver, error) { set.ProviderSettings.Logger = zap.NewNop() } + if set.ConverterSettings.Logger == nil { + set.ConverterSettings.Logger = zap.NewNop() + } + var providers map[string]Provider var converters []Converter diff --git a/confmap/resolver_test.go b/confmap/resolver_test.go index 122009d0e0e..d0218d01f3d 100644 --- a/confmap/resolver_test.go +++ b/confmap/resolver_test.go @@ -425,6 +425,10 @@ func TestProvidesDefaultLogger(t *testing.T) { _, err := NewResolver(ResolverSettings{ URIs: []string{filepath.Join("testdata", "config.yaml")}, ProviderFactories: []ProviderFactory{factory}, + ConverterFactories: []ConverterFactory{NewConverterFactory(func(set ConverterSettings) Converter { + assert.NotNil(t, set.Logger) + return &mockConverter{} + })}, }) require.NoError(t, err) require.NotNil(t, provider.logger) diff --git a/otelcol/collector.go b/otelcol/collector.go index fb36217cb11..3376f3a0c35 100644 --- a/otelcol/collector.go +++ b/otelcol/collector.go @@ -123,8 +123,9 @@ func NewCollector(set CollectorSettings) (*Collector, error) { bc := newBufferedCore(zapcore.DebugLevel) cc := &collectorCore{core: bc} options := append([]zap.Option{zap.WithCaller(true)}, set.LoggingOptions...) - set.ConfigProviderSettings.ResolverSettings.ProviderSettings = confmap.ProviderSettings{Logger: zap.New(cc, options...)} - set.ConfigProviderSettings.ResolverSettings.ConverterSettings = confmap.ConverterSettings{} + logger := zap.New(cc, options...) + set.ConfigProviderSettings.ResolverSettings.ProviderSettings = confmap.ProviderSettings{Logger: logger} + set.ConfigProviderSettings.ResolverSettings.ConverterSettings = confmap.ConverterSettings{Logger: logger} if configProvider == nil { configProvider, err = NewConfigProvider(set.ConfigProviderSettings)