Debian Codegraph @

otelcol/configprovider.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelcol // import "go.opentelemetry.io/collector/otelcol"
5
6 import (
7 "context"
8 "fmt"
9
10 "go.opentelemetry.io/collector/confmap"
11 )
12
13 // ConfigProvider provides the service configuration.
14 //
15 // The typical usage is the following:
16 //
17 // cfgProvider.Get(...)
18 // cfgProvider.Watch() // wait for an event.
19 // cfgProvider.Get(...)
20 // cfgProvider.Watch() // wait for an event.
21 // // repeat Get/Watch cycle until it is time to shut down the Collector process.
22 // cfgProvider.Shutdown()
23 type ConfigProvider struct {
24 mapResolver *confmap.Resolver
25 }
26
27 // ConfigProviderSettings are the settings to configure the behavior of the ConfigProvider.
28 type ConfigProviderSettings struct {
29 // ResolverSettings are the settings to configure the behavior of the confmap.Resolver.
30 ResolverSettings confmap.ResolverSettings
31 // prevent unkeyed literal initialization
32 _ struct{}
33 }
34
35 // NewConfigProvider returns a new ConfigProvider that provides the service configuration:
36 // * Initially it resolves the "configuration map":
37 // - Retrieve the confmap.Conf by merging all retrieved maps from the given `locations` in order.
38 // - Then applies all the confmap.Converter in the given order.
39 //
40 // * Then unmarshalls the confmap.Conf into the service Config.
41 func NewConfigProvider(set ConfigProviderSettings) (*ConfigProvider, error) {
42 mr, err := confmap.NewResolver(set.ResolverSettings)
43 if err != nil {
44 return nil, err
45 }
46
47 return &ConfigProvider{
48 mapResolver: mr,
49 }, nil
50 }
51
52 // Get returns the service configuration, or error otherwise.
53 //
54 // Should never be called concurrently with itself, Watch or Shutdown.
55 func (cm *ConfigProvider) Get(ctx context.Context, factories Factories) (*Config, error) {
56 conf, err := cm.mapResolver.Resolve(ctx)
57 if err != nil {
58 return nil, fmt.Errorf("cannot resolve the configuration: %w", err)
59 }
60
61 var cfg *configSettings
62 if cfg, err = unmarshal(conf, factories); err != nil {
63 return nil, fmt.Errorf("cannot unmarshal the configuration: %w", err)
64 }
65
66 return &Config{
67 Receivers: cfg.Receivers.Configs(),
68 Processors: cfg.Processors.Configs(),
69 Exporters: cfg.Exporters.Configs(),
70 Connectors: cfg.Connectors.Configs(),
71 Extensions: cfg.Extensions.Configs(),
72 Service: cfg.Service,
73 }, nil
74 }
75
76 // Watch blocks until any configuration change was detected or an unrecoverable error
77 // happened during monitoring the configuration changes.
78 //
79 // Error is nil if the configuration is changed and needs to be re-fetched. Any non-nil
80 // error indicates that there was a problem with watching the config changes.
81 //
82 // Should never be called concurrently with itself or Get.
83 func (cm *ConfigProvider) Watch() <-chan error {
84 return cm.mapResolver.Watch()
85 }
86
87 // Shutdown signals that the provider is no longer in use and the that should close
88 // and release any resources that it may have created.
89 //
90 // This function must terminate the Watch channel.
91 //
92 // Should never be called concurrently with itself or Get.
93 func (cm *ConfigProvider) Shutdown(ctx context.Context) error {
94 return cm.mapResolver.Shutdown(ctx)
95 }