@@ -11,30 +11,34 @@ import (
11
11
logger "github.com/observeinc/observe-agent/internal/commands/util"
12
12
"github.com/observeinc/observe-agent/internal/config"
13
13
"github.com/spf13/viper"
14
+ "gopkg.in/yaml.v3"
14
15
)
15
16
16
- func GetAllOtelConfigFilePaths (ctx context.Context , tmpDir string ) ([]string , string , error ) {
17
+ const (
18
+ OTEL_OVERRIDE_YAML_KEY = "otel_config_overrides"
19
+ )
20
+
21
+ func GetAllOtelConfigFilePaths (ctx context.Context , tmpDir string ) ([]string , error ) {
17
22
configFilePaths := []string {}
18
23
// If the default otel-collector.yaml exists, add it to the list of config files
19
24
defaultOtelConfigPath := filepath .Join (GetDefaultConfigFolder (), "otel-collector.yaml" )
20
25
if _ , err := os .Stat (defaultOtelConfigPath ); err == nil {
21
26
agentConf , err := config .AgentConfigFromViper (viper .GetViper ())
22
27
if err != nil {
23
- return nil , "" , err
28
+ return nil , err
24
29
}
25
30
otelConfigRendered , err := RenderConfigTemplate (ctx , tmpDir , defaultOtelConfigPath , agentConf )
26
31
if err != nil {
27
- return nil , "" , err
32
+ return nil , err
28
33
}
29
34
configFilePaths = append (configFilePaths , otelConfigRendered )
30
35
}
31
- var err error
32
36
// Get additional config paths based on connection configs
33
37
for _ , conn := range AllConnectionTypes {
34
38
if viper .IsSet (conn .Name ) {
35
39
connectionPaths , err := conn .GetConfigFilePaths (ctx , tmpDir )
36
40
if err != nil {
37
- return nil , "" , err
41
+ return nil , err
38
42
}
39
43
configFilePaths = append (configFilePaths , connectionPaths ... )
40
44
}
@@ -44,16 +48,32 @@ func GetAllOtelConfigFilePaths(ctx context.Context, tmpDir string) ([]string, st
44
48
configFilePaths = append (configFilePaths , viper .GetString ("otelConfigFile" ))
45
49
}
46
50
// Generate override file and include path if overrides provided
47
- var overridePath string
48
- if viper .IsSet ("otel_config_overrides" ) {
49
- overridePath , err = GetOverrideConfigFile (viper .Sub ("otel_config_overrides" ))
50
- if err != nil {
51
- return configFilePaths , overridePath , err
51
+ if viper .IsSet (OTEL_OVERRIDE_YAML_KEY ) {
52
+ // GetStringMap is more lenient with respect to conversions than Sub, which only handles maps.
53
+ overrides := viper .GetStringMap (OTEL_OVERRIDE_YAML_KEY )
54
+ if len (overrides ) == 0 {
55
+ stringData := viper .GetString (OTEL_OVERRIDE_YAML_KEY )
56
+ // If this was truly set to empty, then ignore it.
57
+ if stringData != "" {
58
+ // Viper can handle overrides set in the agent config, or passed in as an env var as a JSON string.
59
+ // For consistency, we also want to accept an env var as a YAML string.
60
+ err := yaml .Unmarshal ([]byte (stringData ), & overrides )
61
+ if err != nil {
62
+ return nil , fmt .Errorf ("%s was provided but could not be parsed" , OTEL_OVERRIDE_YAML_KEY )
63
+ }
64
+ }
65
+ }
66
+ // Only create the config file if there are overrides present (ie ignore empty maps)
67
+ if len (overrides ) != 0 {
68
+ overridePath , err := GetOverrideConfigFile (tmpDir , overrides )
69
+ if err != nil {
70
+ return nil , err
71
+ }
72
+ configFilePaths = append (configFilePaths , overridePath )
52
73
}
53
- configFilePaths = append (configFilePaths , overridePath )
54
74
}
55
75
logger .FromCtx (ctx ).Debug (fmt .Sprint ("Config file paths:" , configFilePaths ))
56
- return configFilePaths , overridePath , nil
76
+ return configFilePaths , nil
57
77
}
58
78
59
79
func SetEnvVars () error {
@@ -75,14 +95,18 @@ func SetEnvVars() error {
75
95
return nil
76
96
}
77
97
78
- func GetOverrideConfigFile (sub * viper. Viper ) (string , error ) {
79
- f , err := os .CreateTemp ("" , "otel-config-overrides-*.yaml" )
98
+ func GetOverrideConfigFile (tmpDir string , data map [ string ] any ) (string , error ) {
99
+ f , err := os .CreateTemp (tmpDir , "otel-config-overrides-*.yaml" )
80
100
if err != nil {
81
101
return "" , fmt .Errorf ("failed to create config file to write to: %w" , err )
82
102
}
83
- err = sub .WriteConfigAs (f .Name ())
103
+ contents , err := yaml .Marshal (data )
104
+ if err != nil {
105
+ return "" , fmt .Errorf ("failed to marshal otel config overrides: %w" , err )
106
+ }
107
+ _ , err = f .Write ([]byte (contents ))
84
108
if err != nil {
85
- return f . Name () , fmt .Errorf ("failed to write otel config overrides to file: %w" , err )
109
+ return "" , fmt .Errorf ("failed to write otel config overrides to file: %w" , err )
86
110
}
87
111
return f .Name (), nil
88
112
}
0 commit comments