@@ -18,9 +18,11 @@ package configprovider
18
18
import (
19
19
"context"
20
20
"fmt"
21
+ "strings"
21
22
22
23
"github.com/spf13/cast"
23
24
"go.opentelemetry.io/collector/config"
25
+ "go.opentelemetry.io/collector/config/experimental/configsource"
24
26
)
25
27
26
28
const (
@@ -37,16 +39,46 @@ type (
37
39
38
40
// Load reads the configuration for ConfigSource objects from the given parser and returns a map
39
41
// from the full name of config sources to the respective ConfigSettings.
40
- func Load (_ context.Context , v * config.Parser , factories Factories ) (map [string ]ConfigSettings , error ) {
42
+ func Load (ctx context.Context , v * config.Parser , factories Factories ) (map [string ]ConfigSettings , error ) {
43
+ processedParser , err := processParser (ctx , v )
44
+ if err != nil {
45
+ return nil , err
46
+ }
41
47
42
- cfgSrcSettings , err := loadSettings (cast .ToStringMap (v .Get (configSourcesKey )), factories )
48
+ cfgSrcSettings , err := loadSettings (cast .ToStringMap (processedParser .Get (configSourcesKey )), factories )
43
49
if err != nil {
44
50
return nil , err
45
51
}
46
52
47
53
return cfgSrcSettings , nil
48
54
}
49
55
56
+ // processParser prepares a config.Parser to be used to load config source settings.
57
+ func processParser (ctx context.Context , v * config.Parser ) (* config.Parser , error ) {
58
+ // Use a manager to resolve environment variables with a syntax consistent with
59
+ // the config source usage.
60
+ manager := newManager (make (map [string ]configsource.ConfigSource ))
61
+ defer func () {
62
+ _ = manager .Close (ctx )
63
+ }()
64
+
65
+ processedParser := config .NewParser ()
66
+ for _ , key := range v .AllKeys () {
67
+ if ! strings .HasPrefix (key , configSourcesKey ) {
68
+ // In Load we only care about config sources, ignore everything else.
69
+ continue
70
+ }
71
+
72
+ value , err := manager .expandStringValues (ctx , v .Get (key ))
73
+ if err != nil {
74
+ return nil , err
75
+ }
76
+ processedParser .Set (key , value )
77
+ }
78
+
79
+ return processedParser , nil
80
+ }
81
+
50
82
func loadSettings (css map [string ]interface {}, factories Factories ) (map [string ]ConfigSettings , error ) {
51
83
// Prepare resulting map.
52
84
cfgSrcToSettings := make (map [string ]ConfigSettings )
@@ -55,8 +87,6 @@ func loadSettings(css map[string]interface{}, factories Factories) (map[string]C
55
87
for key , value := range css {
56
88
settingsParser := config .NewParserFromStringMap (cast .ToStringMap (value ))
57
89
58
- // TODO: expand env vars.
59
-
60
90
// Decode the key into type and fullName components.
61
91
componentID , err := config .IDFromString (key )
62
92
if err != nil {
0 commit comments