Skip to content

Commit

Permalink
Add converter and provider settings to confmap.ResolverSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-bradley committed Feb 7, 2024
1 parent c63397c commit 15c96dc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
36 changes: 24 additions & 12 deletions confmap/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Resolver struct {
watcher chan error
}

type NewConverter = func(ConverterSettings) Converter

type NewProvider = func(ProviderSettings) Provider

// ResolverSettings are the settings to configure the behavior of the Resolver.
type ResolverSettings struct {
// URIs locations from where the Conf is retrieved, and merged in the given order.
Expand All @@ -43,10 +47,14 @@ type ResolverSettings struct {

// Providers is a map of pairs <scheme, Provider>.
// It is required to have at least one Provider.
Providers map[string]Provider
Providers []NewProvider

ProviderSettings ProviderSettings

// MapConverters is a slice of Converter creation functions.
Converters []NewConverter

// MapConverters is a slice of Converter.
Converters []Converter
ConverterSettings ConverterSettings
}

// NewResolver returns a new Resolver that resolves configuration from multiple URIs.
Expand Down Expand Up @@ -77,6 +85,16 @@ func NewResolver(set ResolverSettings) (*Resolver, error) {
return nil, errors.New("invalid map resolver config: no Providers")
}

providers := make(map[string]Provider, len(set.Providers))
for _, newProvider := range set.Providers {
provider := newProvider(set.ProviderSettings)
providers[provider.Scheme()] = provider
}
converters := make([]Converter, len(set.Converters))
for i, newConverter := range set.Converters {
converters[i] = newConverter(set.ConverterSettings)
}

// Safe copy, ensures the slices and maps cannot be changed from the caller.
uris := make([]location, len(set.URIs))
for i, uri := range set.URIs {
Expand All @@ -91,22 +109,16 @@ func NewResolver(set ResolverSettings) (*Resolver, error) {
if err != nil {
return nil, err
}
if _, ok := set.Providers[lURI.scheme]; !ok {
if _, ok := providers[lURI.scheme]; !ok {
return nil, fmt.Errorf("unsupported scheme on URI %q", uri)
}
uris[i] = lURI
}
providersCopy := make(map[string]Provider, len(set.Providers))
for k, v := range set.Providers {
providersCopy[k] = v
}
convertersCopy := make([]Converter, len(set.Converters))
copy(convertersCopy, set.Converters)

return &Resolver{
uris: uris,
providers: providersCopy,
converters: convertersCopy,
providers: providers,
converters: converters,
watcher: make(chan error, 1),
}, nil
}
Expand Down
3 changes: 3 additions & 0 deletions otelcol/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func NewCollector(set CollectorSettings) (*Collector, error) {
var err error
configProvider := set.ConfigProvider

set.ConfigProviderSettings.ResolverSettings.ProviderSettings = confmap.ProviderSettings{}
set.ConfigProviderSettings.ResolverSettings.ConverterSettings = confmap.ConverterSettings{}

if configProvider == nil {
configProvider, err = NewConfigProvider(set.ConfigProviderSettings)
if err != nil {
Expand Down
20 changes: 9 additions & 11 deletions otelcol/configprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,15 @@ func (cm *configProvider) GetConfmap(ctx context.Context) (*confmap.Conf, error)
func newDefaultConfigProviderSettings(uris []string) ConfigProviderSettings {
return ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: uris,
Providers: makeMapProvidersMap(fileprovider.New(), envprovider.New(), yamlprovider.New(), httpprovider.New(), httpsprovider.New()),
Converters: []confmap.Converter{expandconverter.New()},
URIs: uris,
Providers: []confmap.NewProvider{
fileprovider.NewWithSettings,
envprovider.NewWithSettings,
yamlprovider.NewWithSettings,
httpprovider.NewWithSettings,
httpsprovider.NewWithSettings,
},
Converters: []confmap.NewConverter{expandconverter.New},
},
}
}

func makeMapProvidersMap(providers ...confmap.Provider) map[string]confmap.Provider {
ret := make(map[string]confmap.Provider, len(providers))
for _, provider := range providers {
ret[provider.Scheme()] = provider
}
return ret
}

0 comments on commit 15c96dc

Please sign in to comment.