-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathconfig.go
544 lines (469 loc) · 16.4 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Copyright Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package discovery
import (
"fmt"
"io"
"io/fs"
"os"
"regexp"
"sort"
"github.com/knadh/koanf/maps"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
"github.com/signalfx/splunk-otel-collector/internal/common/discovery"
)
const (
typeService = "service"
typeReceiver = "receiver"
typeExporter = "exporter"
typeExtension = "extension"
typeProcessor = "processor"
typeDiscoveryObserver = "discovery.extension"
typeReceiverToDiscover = "discovery.receiver"
)
var (
defaultType = component.NewID("default")
discoveryDirRegex = fmt.Sprintf("[^%s]*", compilablePathSeparator)
serviceEntryRegex = regexp.MustCompile(fmt.Sprintf("%s%s*service\\.(yaml|yml)$", discoveryDirRegex, compilablePathSeparator))
_, exporterEntryRegex = dirAndEntryRegex("exporters")
extensionsDirRegex, extensionEntryRegex = dirAndEntryRegex("extensions")
discoveryObserverEntryRegex = regexp.MustCompile(fmt.Sprintf("%s%s[^%s]*\\.discovery\\.(yaml|yml)$", extensionsDirRegex, compilablePathSeparator, compilablePathSeparator))
_, processorEntryRegex = dirAndEntryRegex("processors")
receiversDirRegex, receiverEntryRegex = dirAndEntryRegex("receivers")
receiverToDiscoverEntryRegex = regexp.MustCompile(fmt.Sprintf("%s%s[^%s]*\\.discovery\\.(yaml|yml)$", receiversDirRegex, compilablePathSeparator, compilablePathSeparator))
)
// Config is a model for stitching together the final Collector configuration with additional discovery component
// fields for use w/ discovery mode (not yet implemented). It allows individual yaml files to be added to a config.d
// directory and be sourced in the final config such that small changes don't require a central configuration file,
// and possible eliminates the need for one overall (still in design).
type Config struct {
logger *zap.Logger
// Service is for pipelines and final settings
Service ServiceEntry
// Exporters is a map of exporters to use in final config.
// They must be in `config.d/exporters` directory.
Exporters map[component.ID]ExporterEntry
// Extensions is a map of extensions to use in final config.
// They must be in `config.d/extensions` directory.
Extensions map[component.ID]ExtensionEntry
// DiscoveryObservers is a map of observer extensions to use in discovery,
// overriding the default settings. They must be in `config.d/extensions` directory
// and end with ".discovery.yaml".
DiscoveryObservers map[component.ID]ExtensionEntry
// Processors is a map of extensions to use in final config.
// They must be in `config.d/processors` directory.
Processors map[component.ID]ProcessorEntry
// Receivers is a map of receiver entries to use in final config
// They must be in `config.d/receivers` directory.
Receivers map[component.ID]ReceiverEntry
// ReceiversToDiscover is a map of receiver entries to use in discovery mode's
// underlying discovery receiver. They must be in `config.d/receivers` directory and
// end with ".discovery.yaml".
ReceiversToDiscover map[component.ID]ReceiverToDiscoverEntry
}
func NewConfig(logger *zap.Logger) *Config {
return &Config{
logger: logger,
Service: ServiceEntry{Entry{}},
Exporters: map[component.ID]ExporterEntry{},
Extensions: map[component.ID]ExtensionEntry{},
DiscoveryObservers: map[component.ID]ExtensionEntry{},
Processors: map[component.ID]ProcessorEntry{},
Receivers: map[component.ID]ReceiverEntry{},
ReceiversToDiscover: map[component.ID]ReceiverToDiscoverEntry{},
}
}
func dirAndEntryRegex(dirName string) (*regexp.Regexp, *regexp.Regexp) {
dirRegex := regexp.MustCompile(fmt.Sprintf("%s%s*%s", discoveryDirRegex, compilablePathSeparator, dirName))
entryRegex := regexp.MustCompile(fmt.Sprintf("%s%s[^%s]*\\.(yaml|yml)$", dirRegex, compilablePathSeparator, compilablePathSeparator))
return dirRegex, entryRegex
}
type keyType interface {
string | component.ID
}
type entryType interface {
ErrorF(path string, err error) error
Self() Entry
ToStringMap() map[string]any
}
type Entry map[string]any
func (e Entry) Self() Entry {
return e
}
func (e Entry) ToStringMap() map[string]any {
cp := map[string]any{}
for k, v := range e {
cp[k] = v
}
maps.IntfaceKeysToStrings(cp)
return cp
}
var _ entryType = (*ServiceEntry)(nil)
type ServiceEntry struct {
Entry `yaml:",inline"`
}
func (ServiceEntry) ErrorF(path string, err error) error {
return errorF(typeService, path, err)
}
var _ entryType = (*ExtensionEntry)(nil)
type ExtensionEntry struct {
Entry `yaml:",inline"`
}
func (ExtensionEntry) ErrorF(path string, err error) error {
return errorF(typeExtension, path, err)
}
var _ entryType = (*ExporterEntry)(nil)
type ExporterEntry struct {
Entry `yaml:",inline"`
}
func (ExporterEntry) ErrorF(path string, err error) error {
return errorF(typeExporter, path, err)
}
var _ entryType = (*ObserverEntry)(nil)
type ObserverEntry struct {
Entry `yaml:",inline"`
}
func (ObserverEntry) ErrorF(path string, err error) error {
return errorF(typeDiscoveryObserver, path, err)
}
var _ entryType = (*ProcessorEntry)(nil)
type ProcessorEntry struct {
Entry `yaml:",inline"`
}
func (ProcessorEntry) ErrorF(path string, err error) error {
return errorF(typeProcessor, path, err)
}
var _ entryType = (*ReceiverEntry)(nil)
type ReceiverEntry struct {
Entry `yaml:",inline"`
}
func (ReceiverEntry) ErrorF(path string, err error) error {
return errorF(typeReceiver, path, err)
}
type ReceiverToDiscoverEntry struct {
// Receiver creator rules by observer extension ID
Rule map[component.ID]string
// Platform/observer specific config by observer extension ID.
// These are merged w/ "default" component.ID in a "config" map
Config map[component.ID]map[string]any
// The remaining items used to merge applicable rule and config
Entry `yaml:",inline"`
}
var _ entryType = (*ReceiverToDiscoverEntry)(nil)
func (r ReceiverToDiscoverEntry) ToStringMap() map[string]any {
return r.Entry.ToStringMap()
}
func (ReceiverToDiscoverEntry) ErrorF(path string, err error) error {
return errorF(typeReceiverToDiscover, path, err)
}
// Load will walk the file tree from the configDPath root, loading the component
// files as they are discovered, determined by their parent directory and filename.
func (c *Config) Load(configDPath string) error {
if c == nil {
return fmt.Errorf("config must not be nil to be loaded (use NewConfig())")
}
return c.LoadFS(os.DirFS(configDPath))
}
// LoadFS will walk the provided filesystem, loading the component files as they are discovered,
// determined by their parent directory and filename.
func (c *Config) LoadFS(dirfs fs.FS) error {
if c == nil {
return fmt.Errorf("config must not be nil to be loaded (use NewConfig())")
}
err := fs.WalkDir(dirfs, ".", func(path string, d fs.DirEntry, err error) error {
c.logger.Debug("loading component", zap.String("path", path), zap.String("DirEntry", fmt.Sprintf("%#v", d)), zap.Error(err))
if err != nil {
return err
}
switch {
case isServiceEntryPath(path):
// c.Service is not a map[string]ServiceEntry, so we form a tmp
// and unmarshal to the underlying ServiceEntry
tmpSEMap := map[string]ServiceEntry{typeService: c.Service}
return loadEntry(typeService, dirfs, path, tmpSEMap)
case isExporterEntryPath(path):
return loadEntry(typeExporter, dirfs, path, c.Exporters)
case isExtensionEntryPath(path):
if isDiscoveryObserverEntryPath(path) {
return loadEntry(typeDiscoveryObserver, dirfs, path, c.DiscoveryObservers)
}
return loadEntry(typeExtension, dirfs, path, c.Extensions)
case isProcessorEntryPath(path):
return loadEntry(typeProcessor, dirfs, path, c.Processors)
case isReceiverEntryPath(path):
if isReceiverToDiscoverEntryPath(path) {
return loadEntry(typeReceiverToDiscover, dirfs, path, c.ReceiversToDiscover)
}
return loadEntry(typeReceiver, dirfs, path, c.Receivers)
default:
c.logger.Debug("Disregarding path", zap.String("path", path))
}
return nil
})
if err != nil {
// clean up to prevent using partial config
c.DiscoveryObservers = nil
c.ReceiversToDiscover = nil
c.Receivers = nil
c.Service = ServiceEntry{nil}
c.Exporters = nil
c.Processors = nil
c.Extensions = nil
}
return err
}
// toServiceConfig renders the loaded Config content
// suitable for use as a Collector configuration
func (c *Config) toServiceConfig() map[string]any {
sc := confmap.New()
service := c.Service.ToStringMap()
sc.Merge(confmap.NewFromStringMap(map[string]any{typeService: service}))
receivers := map[string]any{}
for k, v := range c.Receivers {
receivers[k.String()] = v.ToStringMap()
}
sc.Merge(confmap.NewFromStringMap(map[string]any{"receivers": receivers}))
processors := map[string]any{}
for k, v := range c.Processors {
processors[k.String()] = v.ToStringMap()
}
sc.Merge(confmap.NewFromStringMap(map[string]any{"processors": processors}))
exporters := map[string]any{}
for k, v := range c.Exporters {
exporters[k.String()] = v.ToStringMap()
}
sc.Merge(confmap.NewFromStringMap(map[string]any{"exporters": exporters}))
extensions := map[string]any{}
for k, v := range c.Extensions {
extensions[k.String()] = v.ToStringMap()
}
sc.Merge(confmap.NewFromStringMap(map[string]any{"extensions": extensions}))
return sc.ToStringMap()
}
func errorF(entryType, path string, err error) error {
return fmt.Errorf("failed loading %s from %s: %w", entryType, path, err)
}
func isServiceEntryPath(path string) bool {
return serviceEntryRegex.MatchString(path)
}
func isExporterEntryPath(path string) bool {
return exporterEntryRegex.MatchString(path)
}
func isExtensionEntryPath(path string) bool {
return extensionEntryRegex.MatchString(path)
}
func isDiscoveryObserverEntryPath(path string) bool {
return discoveryObserverEntryRegex.MatchString(path)
}
func isProcessorEntryPath(path string) bool {
return processorEntryRegex.MatchString(path)
}
func isReceiverEntryPath(path string) bool {
return receiverEntryRegex.MatchString(path)
}
func isReceiverToDiscoverEntryPath(path string) bool {
return receiverToDiscoverEntryRegex.MatchString(path)
}
func loadEntry[K keyType, V entryType](componentType string, fs fs.FS, path string, target map[K]V) error {
tmpDest := map[K]V{}
componentID, err := unmarshalEntry(componentType, fs, path, &tmpDest)
noTypeK, err2 := stringToKeyType(discovery.NoType.String(), componentID)
if err2 != nil {
return err2
}
if err != nil {
return tmpDest[noTypeK].ErrorF(path, err)
}
if componentID == noTypeK {
return nil
}
if componentType == typeService {
// set directly on target and exit
typeServiceK, err := stringToKeyType(componentType, componentID)
if err != nil {
return err
}
serviceEntry := target[typeServiceK].Self()
tmpDstSM := tmpDest[typeServiceK].ToStringMap()
for k, v := range tmpDstSM {
serviceEntry[keyTypeToString(k)] = v
}
return nil
}
if v, ok := target[componentID]; ok {
return v.ErrorF(path, fmt.Errorf("duplicate %q", keyTypeToString(componentID)))
}
entry := tmpDest[componentID]
target[componentID] = entry
return nil
}
func unmarshalEntry[K keyType, V entryType](componentType string, fs fs.FS, path string, dst *map[K]V) (componentID K, err error) {
if dst == nil {
err = fmt.Errorf("cannot load %s into nil entry", componentType)
return
}
var unmarshalDst any = dst
// service is map[string]ServiceEntry{typeService: ServiceEntry} and we want dst to be &ServiceEntry
if componentType == typeService {
var s any = typeService
// service key is always string so this type assertion is safe
se := (*dst)[s.(K)]
unmarshalDst = &se
}
if err = unmarshalYaml(fs, path, unmarshalDst); err != nil {
err = fmt.Errorf("failed unmarshalling component %s: %w", componentType, err)
return
}
if componentType == typeService {
// reset map[string]ServiceEntry dst w/ unmarshalled ServiceEntry and return
var tService any = typeService
var serviceEntry any = *(unmarshalDst.(*ServiceEntry))
(*dst)[tService.(K)] = serviceEntry.(V)
return tService.(K), nil
}
entry := *(unmarshalDst.(*map[K]V))
if len(entry) == 0 {
// empty or all-comment files are supported but ignored
var noTypeK any = discovery.NoType
// non-service key is always componentID so this type assertion is safe
return noTypeK.(K), nil
}
var componentIDs []K
var comp V
for k, v := range entry {
componentIDs = append(componentIDs, k)
comp = v
}
if len(componentIDs) != 1 {
// deterministic for testability
var cids []string
for _, i := range componentIDs {
cids = append(cids, keyTypeToString(i))
}
sort.Strings(cids)
err = comp.ErrorF(
path, fmt.Errorf("must contain a single mapping of ComponentID to component but contained %v", cids),
)
return
}
return componentIDs[0], nil
}
func unmarshalYaml(fs fs.FS, path string, out any) error {
f, err := fs.Open(path)
if err != nil {
return err
}
defer f.Close()
contents, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("failed reading file %q: %w", path, err)
}
if err = yaml.Unmarshal(contents, out); err != nil {
return fmt.Errorf("failed parsing %q as yaml: %w", path, err)
}
return nil
}
func stringToKeyType[K keyType](s string, key K) (K, error) {
var componentIDK any
for _, kid := range []K{key} {
var cidK any = kid
switch cidK.(type) {
case string:
var anyS any = s
return anyS.(K), nil
case component.ID:
if s == discovery.NoType.String() {
componentIDK = discovery.NoType
} else {
var err error
componentIDK = component.ID{}
cIDK := componentIDK.(component.ID)
if err = (&cIDK).UnmarshalText([]byte(s)); err != nil {
// nolint:gocritic
return *new(K), err // (gocritic suggestion not valid with type parameter)
}
}
}
break
}
return componentIDK.(K), nil
}
func keyTypeToString[K keyType](key K) string {
var ret string
for _, k := range []K{key} {
var anyK any = k
switch i := anyK.(type) {
case string:
ret = i
case component.ID:
ret = i.String()
}
break
}
return ret
}
var compilablePathSeparator = func() string {
if os.PathSeparator == '\\' {
// fs.Stat doesn't use os.PathSeparator so accept '/' as well.
// TODO: determine if we even need anything but "/"
return "(\\\\|/)"
}
return string(os.PathSeparator)
}()
func mergeConfigWithBundle(userCfg *Config, bundleCfg *Config) error {
for obs, bundledObs := range bundleCfg.DiscoveryObservers {
userObs, ok := userCfg.DiscoveryObservers[obs]
if !ok {
userCfg.DiscoveryObservers[obs] = bundledObs
continue
}
bundledConfMap := confmap.NewFromStringMap(bundledObs.ToStringMap())
userConfMap := confmap.NewFromStringMap(userObs.ToStringMap())
if err := bundledConfMap.Merge(userConfMap); err != nil {
return fmt.Errorf("failed merged user and bundled observer %q discovery configs: %w", obs, err)
}
userCfg.DiscoveryObservers[obs] = ExtensionEntry{Entry: bundledConfMap.ToStringMap()}
}
for rec, bundledRec := range bundleCfg.ReceiversToDiscover {
userRec, ok := userCfg.ReceiversToDiscover[rec]
if !ok {
userCfg.ReceiversToDiscover[rec] = bundledRec
continue
}
bundledConfMap := confmap.NewFromStringMap(bundledRec.ToStringMap())
userConfMap := confmap.NewFromStringMap(userRec.ToStringMap())
if err := bundledConfMap.Merge(userConfMap); err != nil {
return fmt.Errorf("failed merged user and bundled receiver %q discovery configs: %w", rec, err)
}
receiver := ReceiverToDiscoverEntry{
Rule: bundledRec.Rule, Config: bundledRec.Config, Entry: bundledConfMap.ToStringMap(),
}
for cid, rule := range userRec.Rule {
receiver.Rule[cid] = rule
}
for obs, config := range userRec.Config {
if bundledConfig, ok := bundledRec.Config[obs]; ok {
bundledConf := confmap.NewFromStringMap(bundledConfig)
bundledConf.Merge(confmap.NewFromStringMap(config))
config = bundledConf.ToStringMap()
}
receiver.Config[obs] = config
}
userCfg.ReceiversToDiscover[rec] = receiver
}
return nil
}