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 13, 2024
1 parent 23e10db commit 55f5221
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
36 changes: 24 additions & 12 deletions confmap/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 @@ -35,10 +39,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 @@ -69,6 +77,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 @@ -83,22 +101,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
26 changes: 8 additions & 18 deletions otelcol/configprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,27 +132,17 @@ func (cm *configProvider) GetConfmap(ctx context.Context) (*confmap.Conf, error)
}

func newDefaultConfigProviderSettings(uris []string) ConfigProviderSettings {
converterSet := confmap.ConverterSettings{}
providerSet := confmap.ProviderSettings{}
return ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: uris,
Providers: makeMapProvidersMap(
fileprovider.NewWithSettings(providerSet),
envprovider.NewWithSettings(providerSet),
yamlprovider.NewWithSettings(providerSet),
httpprovider.NewWithSettings(providerSet),
httpsprovider.NewWithSettings(providerSet),
),
Converters: []confmap.Converter{expandconverter.New(converterSet)},
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 55f5221

Please sign in to comment.