| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package confmap // import "go.opentelemetry.io/collector/confmap" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"regexp" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"go.uber.org/multierr" |
| 14 |
"go.uber.org/zap" |
| 15 |
|
| 16 |
"go.opentelemetry.io/collector/confmap/internal" |
| 17 |
) |
| 18 |
|
| 19 |
// follows drive-letter specification: |
| 20 |
// https://datatracker.ietf.org/doc/html/draft-kerwin-file-scheme-07.html#section-2.2 |
| 21 |
var driverLetterRegexp = regexp.MustCompile("^[A-z]:") |
| 22 |
|
| 23 |
// Resolver resolves a configuration as a Conf. |
| 24 |
type Resolver struct { |
| 25 |
uris []location |
| 26 |
providers map[string]Provider |
| 27 |
defaultScheme string |
| 28 |
converters []Converter |
| 29 |
|
| 30 |
closers []CloseFunc |
| 31 |
watcher chan error |
| 32 |
} |
| 33 |
|
| 34 |
// ResolverSettings are the settings to configure the behavior of the Resolver. |
| 35 |
type ResolverSettings struct { |
| 36 |
// URIs locations from where the Conf is retrieved, and merged in the given order. |
| 37 |
// It is required to have at least one location. |
| 38 |
URIs []string |
| 39 |
|
| 40 |
// ProviderFactories is a slice of Provider factories. |
| 41 |
// It is required to have at least one factory. |
| 42 |
ProviderFactories []ProviderFactory |
| 43 |
|
| 44 |
// DefaultScheme is the scheme that is used if ${} syntax is used but no schema is provided. |
| 45 |
// If no DefaultScheme is set, ${} with no schema will not be expanded. |
| 46 |
// It is strongly recommended to set "env" as the default scheme to align with the |
| 47 |
// OpenTelemetry Configuration Specification |
| 48 |
DefaultScheme string |
| 49 |
|
| 50 |
// ProviderSettings contains settings that will be passed to Provider |
| 51 |
// factories when instantiating Providers. |
| 52 |
ProviderSettings ProviderSettings |
| 53 |
|
| 54 |
// ConverterFactories is a slice of Converter creation functions. |
| 55 |
ConverterFactories []ConverterFactory |
| 56 |
|
| 57 |
// ConverterSettings contains settings that will be passed to Converter |
| 58 |
// factories when instantiating Converters. |
| 59 |
ConverterSettings ConverterSettings |
| 60 |
|
| 61 |
// prevent unkeyed literal initialization |
| 62 |
_ struct{} |
| 63 |
} |
| 64 |
|
| 65 |
// NewResolver returns a new Resolver that resolves configuration from multiple URIs. |
| 66 |
// |
| 67 |
// To resolve a configuration the following steps will happen: |
| 68 |
// 1. Retrieves individual configurations from all given "URIs", and merge them in the retrieve order. |
| 69 |
// 2. Once the Conf is merged, apply the converters in the given order. |
| 70 |
// |
| 71 |
// After the configuration was resolved the `Resolver` can be used as a single point to watch for updates in |
| 72 |
// the configuration data retrieved via the config providers used to process the "initial" configuration and to generate |
| 73 |
// the "effective" one. The typical usage is the following: |
| 74 |
// |
| 75 |
// Resolver.Resolve(ctx) |
| 76 |
// Resolver.Watch() // wait for an event. |
| 77 |
// Resolver.Resolve(ctx) |
| 78 |
// Resolver.Watch() // wait for an event. |
| 79 |
// // repeat Resolve/Watch cycle until it is time to shut down the Collector process. |
| 80 |
// Resolver.Shutdown(ctx) |
| 81 |
// |
| 82 |
// `uri` must follow the "<scheme>:<opaque_data>" format. This format is compatible with the URI definition |
| 83 |
// (see https://datatracker.ietf.org/doc/html/rfc3986). An empty "<scheme>" defaults to "file" schema. |
| 84 |
func NewResolver(set ResolverSettings) (*Resolver, error) { |
| 85 |
if len(set.URIs) == 0 { |
| 86 |
return nil, errors.New("invalid 'confmap.ResolverSettings' configuration: no URIs") |
| 87 |
} |
| 88 |
|
| 89 |
if len(set.ProviderFactories) == 0 { |
| 90 |
return nil, errors.New("invalid 'confmap.ResolverSettings' configuration: no Providers") |
| 91 |
} |
| 92 |
|
| 93 |
if set.ProviderSettings.Logger == nil { |
| 94 |
set.ProviderSettings.Logger = zap.NewNop() |
| 95 |
} |
| 96 |
|
| 97 |
if set.ConverterSettings.Logger == nil { |
| 98 |
set.ConverterSettings.Logger = zap.NewNop() |
| 99 |
} |
| 100 |
|
| 101 |
providers := make(map[string]Provider, len(set.ProviderFactories)) |
| 102 |
for _, factory := range set.ProviderFactories { |
| 103 |
provider := factory.Create(set.ProviderSettings) |
| 104 |
scheme := provider.Scheme() |
| 105 |
// Check that the scheme follows the pattern. |
| 106 |
if !regexp.MustCompile(schemePattern).MatchString(scheme) { |
| 107 |
return nil, fmt.Errorf("invalid 'confmap.Provider' scheme %q", scheme) |
| 108 |
} |
| 109 |
// Check that the scheme is unique. |
| 110 |
if _, ok := providers[scheme]; ok { |
| 111 |
return nil, fmt.Errorf("duplicate 'confmap.Provider' scheme %q", scheme) |
| 112 |
} |
| 113 |
|
| 114 |
providers[scheme] = provider |
| 115 |
} |
| 116 |
|
| 117 |
if set.DefaultScheme != "" { |
| 118 |
_, ok := providers[set.DefaultScheme] |
| 119 |
if !ok { |
| 120 |
return nil, errors.New("invalid 'confmap.ResolverSettings' configuration: DefaultScheme not found in providers list") |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
converters := make([]Converter, len(set.ConverterFactories)) |
| 125 |
for i, factory := range set.ConverterFactories { |
| 126 |
converters[i] = factory.Create(set.ConverterSettings) |
| 127 |
} |
| 128 |
|
| 129 |
// Safe copy, ensures the slices and maps cannot be changed from the caller. |
| 130 |
uris := make([]location, len(set.URIs)) |
| 131 |
for i, uri := range set.URIs { |
| 132 |
// For backwards compatibility: |
| 133 |
// - empty url scheme means "file". |
| 134 |
// - "^[A-z]:" also means "file" |
| 135 |
if driverLetterRegexp.MatchString(uri) || !strings.Contains(uri, ":") { |
| 136 |
uris[i] = location{scheme: "file", opaqueValue: uri} |
| 137 |
continue |
| 138 |
} |
| 139 |
lURI, err := newLocation(uri) |
| 140 |
if err != nil { |
| 141 |
return nil, err |
| 142 |
} |
| 143 |
if _, ok := providers[lURI.scheme]; !ok { |
| 144 |
return nil, fmt.Errorf("unsupported scheme on URI %q", uri) |
| 145 |
} |
| 146 |
uris[i] = lURI |
| 147 |
} |
| 148 |
|
| 149 |
return &Resolver{ |
| 150 |
uris: uris, |
| 151 |
providers: providers, |
| 152 |
defaultScheme: set.DefaultScheme, |
| 153 |
converters: converters, |
| 154 |
watcher: make(chan error, 1), |
| 155 |
}, nil |
| 156 |
} |
| 157 |
|
| 158 |
// Resolve returns the configuration as a Conf, or error otherwise. |
| 159 |
// Should never be called concurrently with itself, Watch or Shutdown. |
| 160 |
func (mr *Resolver) Resolve(ctx context.Context) (*Conf, error) { |
| 161 |
// First check if already an active watching, close that if any. |
| 162 |
if err := mr.closeIfNeeded(ctx); err != nil { |
| 163 |
return nil, fmt.Errorf("cannot close previous watch: %w", err) |
| 164 |
} |
| 165 |
|
| 166 |
// Retrieves individual configurations from all URIs in the given order, and merge them in retMap. |
| 167 |
retMap := New() |
| 168 |
for _, uri := range mr.uris { |
| 169 |
ret, err := mr.retrieveValue(ctx, uri) |
| 170 |
if err != nil { |
| 171 |
return nil, fmt.Errorf("cannot retrieve the configuration: %w", err) |
| 172 |
} |
| 173 |
mr.closers = append(mr.closers, ret.Close) |
| 174 |
retCfgMap, err := ret.AsConf() |
| 175 |
if err != nil { |
| 176 |
return nil, err |
| 177 |
} |
| 178 |
|
| 179 |
if err := retMap.Merge(retCfgMap); err != nil { |
| 180 |
return nil, err |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
cfgMap := make(map[string]any) |
| 185 |
for _, k := range retMap.AllKeys() { |
| 186 |
ug := internal.UnsanitizedGetter{Conf: retMap} |
| 187 |
val, err := mr.expandValueRecursively(ctx, ug.UnsanitizedGet(k)) |
| 188 |
if err != nil { |
| 189 |
return nil, err |
| 190 |
} |
| 191 |
cfgMap[k] = escapeDollarSigns(val) |
| 192 |
} |
| 193 |
retMap = NewFromStringMap(cfgMap) |
| 194 |
|
| 195 |
// Apply the converters in the given order. |
| 196 |
for _, confConv := range mr.converters { |
| 197 |
if err := confConv.Convert(ctx, retMap); err != nil { |
| 198 |
return nil, fmt.Errorf("cannot convert the confmap.Conf: %w", err) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return retMap, nil |
| 203 |
} |
| 204 |
|
| 205 |
func escapeDollarSigns(val any) any { |
| 206 |
switch v := val.(type) { |
| 207 |
case string: |
| 208 |
return strings.ReplaceAll(v, "$$", "$") |
| 209 |
case internal.ExpandedValue: |
| 210 |
v.Original = strings.ReplaceAll(v.Original, "$$", "$") |
| 211 |
v.Value = escapeDollarSigns(v.Value) |
| 212 |
return v |
| 213 |
case []any: |
| 214 |
nslice := make([]any, len(v)) |
| 215 |
for i, x := range v { |
| 216 |
nslice[i] = escapeDollarSigns(x) |
| 217 |
} |
| 218 |
return nslice |
| 219 |
case map[string]any: |
| 220 |
nmap := make(map[string]any, len(v)) |
| 221 |
for k, x := range v { |
| 222 |
nmap[k] = escapeDollarSigns(x) |
| 223 |
} |
| 224 |
return nmap |
| 225 |
default: |
| 226 |
return val |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
// Watch blocks until any configuration change was detected or an unrecoverable error |
| 231 |
// happened during monitoring the configuration changes. |
| 232 |
// |
| 233 |
// Error is nil if the configuration is changed and needs to be re-fetched. Any non-nil |
| 234 |
// error indicates that there was a problem with watching the configuration changes. |
| 235 |
// |
| 236 |
// Should never be called concurrently with itself or Get. |
| 237 |
func (mr *Resolver) Watch() <-chan error { |
| 238 |
return mr.watcher |
| 239 |
} |
| 240 |
|
| 241 |
// Shutdown signals that the provider is no longer in use and the that should close |
| 242 |
// and release any resources that it may have created. It terminates the Watch channel. |
| 243 |
// |
| 244 |
// Should never be called concurrently with itself or Get. |
| 245 |
func (mr *Resolver) Shutdown(ctx context.Context) error { |
| 246 |
var errs error |
| 247 |
errs = multierr.Append(errs, mr.closeIfNeeded(ctx)) |
| 248 |
for _, p := range mr.providers { |
| 249 |
errs = multierr.Append(errs, p.Shutdown(ctx)) |
| 250 |
} |
| 251 |
|
| 252 |
close(mr.watcher) |
| 253 |
|
| 254 |
return errs |
| 255 |
} |
| 256 |
|
| 257 |
func (mr *Resolver) onChange(event *ChangeEvent) { |
| 258 |
mr.watcher <- event.Error |
| 259 |
} |
| 260 |
|
| 261 |
func (mr *Resolver) closeIfNeeded(ctx context.Context) error { |
| 262 |
var err error |
| 263 |
for _, ret := range mr.closers { |
| 264 |
err = multierr.Append(err, ret(ctx)) |
| 265 |
} |
| 266 |
mr.closers = nil |
| 267 |
return err |
| 268 |
} |
| 269 |
|
| 270 |
func (mr *Resolver) retrieveValue(ctx context.Context, uri location) (*Retrieved, error) { |
| 271 |
p, ok := mr.providers[uri.scheme] |
| 272 |
if !ok { |
| 273 |
return nil, fmt.Errorf("scheme %q is not supported for uri %q", uri.scheme, uri.asString()) |
| 274 |
} |
| 275 |
return p.Retrieve(ctx, uri.asString(), mr.onChange) |
| 276 |
} |