Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[confmap] Add ability to set default provider #10182

Merged
merged 22 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/confmap-default-provider.yaml
Original file line number Diff line number Diff line change
@@ -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 setting a default Provider on a Resolver to use when `${}` syntax is used without a schema
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved

# One or more tracking issues or pull requests related to the change
issues: [10182]

# (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: []
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 15 additions & 4 deletions confmap/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@

// findAndExpandURI attempts to find and expand the first occurrence of an expandable URI in input. If an expandable URI is found it
// returns the input with the URI expanded, true and nil. Otherwise, it returns the unchanged input, false and the expanding error.
// This method expects input to start with ${ and end with }
func (mr *Resolver) findAndExpandURI(ctx context.Context, input string) (any, bool, error) {
uri := findURI(input)
if uri == "" {
// No URI found, return.
if mr.defaultProvider != nil {
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
ret, err := mr.defaultProvider.Retrieve(ctx, input[2:len(input)-1], mr.onChange)
if err != nil {
return nil, false, err

Check warning on line 108 in confmap/expand.go

View check run for this annotation

Codecov / codecov/patch

confmap/expand.go#L108

Added line #L108 was not covered by tests
}
return mr.handleRetrieved(ret)
}
return input, false, nil
}
if uri == input {
Expand Down Expand Up @@ -138,6 +145,12 @@
}
}

func (mr *Resolver) handleRetrieved(ret *Retrieved) (any, bool, error) {
mr.closers = append(mr.closers, ret.Close)
val, err := ret.AsRaw()
return val, true, err
}

func (mr *Resolver) expandURI(ctx context.Context, uri string) (any, bool, error) {
lURI, err := newLocation(uri[2 : len(uri)-1])
if err != nil {
Expand All @@ -150,9 +163,7 @@
if err != nil {
return nil, false, err
}
mr.closers = append(mr.closers, ret.Close)
val, err := ret.AsRaw()
return val, true, err
return mr.handleRetrieved(ret)
}

type location struct {
Expand Down
16 changes: 16 additions & 0 deletions confmap/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ func TestResolverExpandStringValues(t *testing.T) {

func newEnvProvider() ProviderFactory {
return newFakeProvider("env", func(_ context.Context, uri string, _ WatcherFunc) (*Retrieved, error) {
if uri[0:4] != "env:" {
uri = "env:" + uri
}
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
switch uri {
case "env:COMPLEX_VALUE":
return NewRetrieved([]any{"localhost:3042"})
Expand Down Expand Up @@ -459,3 +462,16 @@ func TestResolverExpandStringValueInvalidReturnValue(t *testing.T) {
_, err = resolver.Resolve(context.Background())
assert.EqualError(t, err, `expanding ${test:PORT}, expected convertable to string value type, got ['ӛ']([]interface {})`)
}

func TestResolverDefaultProviderExpand(t *testing.T) {
provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) {
return NewRetrieved(map[string]any{"foo": "${HOST}"})
})

resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider}, DefaultProvider: newEnvProvider(), ConverterFactories: nil})
require.NoError(t, err)

cfgMap, err := resolver.Resolve(context.Background())
require.NoError(t, err)
assert.Equal(t, map[string]any{"foo": "localhost"}, cfgMap.ToStringMap())
}
32 changes: 22 additions & 10 deletions confmap/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ var driverLetterRegexp = regexp.MustCompile("^[A-z]:")

// Resolver resolves a configuration as a Conf.
type Resolver struct {
uris []location
providers map[string]Provider
converters []Converter
uris []location
providers map[string]Provider
defaultProvider Provider
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
converters []Converter

closers []CloseFunc
watcher chan error
Expand All @@ -34,11 +35,16 @@ type ResolverSettings struct {
// It is required to have at least one location.
URIs []string

// ProviderFactories is a list of Provider creation functions.
// It is required to have at least one ProviderFactory
// if a Provider is not given.
// ProviderFactories is a slice of Provider creation functions.
// It is required to have at least one provider.
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
ProviderFactories []ProviderFactory

// DefaultProvider is the provider that is used if ${} syntax is used but no schema is provided.
// If no DefaultProvider is set, ${} with no schema will not be expanded.
// It is strongly recommended to set an EnvProvider as the default to align with the
// OpenTelemetry Configuration Specification
DefaultProvider ProviderFactory
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved

// ProviderSettings contains settings that will be passed to Provider
// factories when instantiating Providers.
ProviderSettings ProviderSettings
Expand Down Expand Up @@ -93,6 +99,11 @@ func NewResolver(set ResolverSettings) (*Resolver, error) {
providers[provider.Scheme()] = provider
}

var defaultProvider Provider
if set.DefaultProvider != nil {
defaultProvider = set.DefaultProvider.Create(set.ProviderSettings)
}

converters := make([]Converter, len(set.ConverterFactories))
for i, factory := range set.ConverterFactories {
converters[i] = factory.Create(set.ConverterSettings)
Expand All @@ -119,10 +130,11 @@ func NewResolver(set ResolverSettings) (*Resolver, error) {
}

return &Resolver{
uris: uris,
providers: providers,
converters: converters,
watcher: make(chan error, 1),
uris: uris,
providers: providers,
defaultProvider: defaultProvider,
converters: converters,
watcher: make(chan error, 1),
}, nil
}

Expand Down
11 changes: 11 additions & 0 deletions confmap/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,14 @@ func TestProvidesDefaultLogger(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, provider.logger)
}

func TestResolverDefaultProviderSet(t *testing.T) {
provider := newEnvProvider()
r, err := NewResolver(ResolverSettings{
URIs: []string{"env:"},
ProviderFactories: []ProviderFactory{provider},
DefaultProvider: provider,
})
require.NoError(t, err)
require.NotNil(t, r.defaultProvider)
}
Loading