18
18
package main
19
19
20
20
import (
21
+ "bytes"
21
22
"fmt"
22
23
"log"
23
24
"os"
@@ -26,6 +27,7 @@ import (
26
27
27
28
"go.opentelemetry.io/collector/component"
28
29
"go.opentelemetry.io/collector/service"
30
+ "go.opentelemetry.io/collector/service/parserprovider"
29
31
"go.uber.org/zap"
30
32
31
33
"github.com/signalfx/splunk-otel-collector/internal/components"
@@ -37,6 +39,7 @@ import (
37
39
const (
38
40
ballastEnvVarName = "SPLUNK_BALLAST_SIZE_MIB"
39
41
configEnvVarName = "SPLUNK_CONFIG"
42
+ configYamlEnvVarName = "SPLUNK_CONFIG_YAML"
40
43
memLimitMiBEnvVarName = "SPLUNK_MEMORY_LIMIT_MIB"
41
44
memTotalEnvVarName = "SPLUNK_MEMORY_TOTAL_MIB"
42
45
realmEnvVarName = "SPLUNK_REALM"
@@ -71,11 +74,18 @@ func main() {
71
74
Version : version .Version ,
72
75
}
73
76
77
+ baseParserProvider := parserprovider .Default ()
78
+ if configYAML := os .Getenv (configYamlEnvVarName ); configYAML != "" {
79
+ baseParserProvider = parserprovider .NewInMemory (bytes .NewBufferString (configYAML ))
80
+ }
81
+
74
82
parserProvider := configprovider .NewConfigSourceParserProvider (
83
+ baseParserProvider ,
75
84
zap .NewNop (), // The service logger is not available yet, setting it to NoP.
76
85
info ,
77
86
configsources .Get ()... ,
78
87
)
88
+
79
89
serviceParams := service.Parameters {
80
90
BuildInfo : info ,
81
91
Factories : factories ,
@@ -124,21 +134,7 @@ func getKeyValue(args []string, argName string) string {
124
134
// Config and ballast flags are checked
125
135
// Config and all memory env vars are checked
126
136
func checkRuntimeParams () {
127
- args := os .Args [1 :]
128
- config := ""
129
-
130
- // Check if config flag was passed
131
- // If so, ensure config env var is not set
132
- // Then set config properly
133
- cliConfig := getKeyValue (args , "--config" )
134
- if cliConfig != "" {
135
- config = os .Getenv (configEnvVarName )
136
- if config != "" {
137
- log .Fatalf ("Both %v and '--config' were specified, but only one is allowed" , configEnvVarName )
138
- }
139
- os .Setenv (configEnvVarName , cliConfig )
140
- }
141
- setConfig ()
137
+ setConfigSource ()
142
138
143
139
// Set default total memory
144
140
memTotalSizeMiB := defaultMemoryTotalMiB
@@ -161,10 +157,9 @@ func checkRuntimeParams() {
161
157
// Check if memory ballast flag was passed
162
158
// If so, ensure memory ballast env var is not set
163
159
// Then set memory ballast and limit properly
164
- ballastSize := getKeyValue (args , "--mem-ballast-size-mib" )
160
+ ballastSize := getKeyValue (os . Args [ 1 :] , "--mem-ballast-size-mib" )
165
161
if ballastSize != "" {
166
- config = os .Getenv (ballastEnvVarName )
167
- if config != "" {
162
+ if os .Getenv (ballastEnvVarName ) != "" {
168
163
log .Fatalf ("Both %v and '--config' were specified, but only one is allowed" , ballastEnvVarName )
169
164
}
170
165
os .Setenv (ballastEnvVarName , ballastSize )
@@ -174,31 +169,52 @@ func checkRuntimeParams() {
174
169
}
175
170
176
171
// Validate and set the configuration
177
- func setConfig () {
178
- // Check if the config is specified via the env var.
179
- config := os .Getenv (configEnvVarName )
180
- // If not attempt to use a default config; supports Docker and local
181
- if config == "" {
172
+ func setConfigSource () {
173
+ // Config file path from cmd flag --config.
174
+ pathFlag := getKeyValue (os .Args [1 :], "--config" )
175
+ // Config file path from env var SPLUNK_CONFIG.
176
+ pathEnv := os .Getenv (configEnvVarName )
177
+ // Config YAML from env var SPLUNK_CONFIG_YAML.
178
+ cfgYAML := os .Getenv (configYamlEnvVarName )
179
+
180
+ // Halting if multiple config sources specified.
181
+ if (pathFlag != "" && pathEnv != "" ) || (pathFlag != "" && cfgYAML != "" ) || (pathEnv != "" && cfgYAML != "" ) {
182
+ log .Fatalf ("'--config', %s and %s must be specified exclusively" , configEnvVarName , configYamlEnvVarName )
183
+ }
184
+
185
+ if cfgYAML != "" {
186
+ log .Printf ("Configuring collector using env var %s YAML" , configYamlEnvVarName )
187
+ return
188
+ }
189
+
190
+ // Setting env var SPLUNK_CONFIG if flag config was passed.
191
+ if pathFlag != "" {
192
+ pathEnv = pathFlag
193
+ os .Setenv (configEnvVarName , pathEnv )
194
+ }
195
+
196
+ // Use a default config if no config given; supports Docker and local
197
+ if pathEnv == "" {
182
198
_ , err := os .Stat (defaultDockerSAPMConfig )
183
199
if err == nil {
184
- config = defaultDockerSAPMConfig
200
+ pathEnv = defaultDockerSAPMConfig
185
201
}
186
202
_ , err = os .Stat (defaultLocalSAPMConfig )
187
203
if err == nil {
188
- config = defaultLocalSAPMConfig
204
+ pathEnv = defaultLocalSAPMConfig
189
205
}
190
- if config == "" {
206
+ if pathEnv == "" {
191
207
log .Fatalf ("Unable to find the default configuration file, ensure %s environment variable is set properly" , configEnvVarName )
192
208
}
193
209
} else {
194
210
// Check if file exists.
195
- _ , err := os .Stat (config )
211
+ _ , err := os .Stat (pathEnv )
196
212
if err != nil {
197
- log .Fatalf ("Unable to find the configuration file (%s) ensure %s environment variable is set properly" , config , configEnvVarName )
213
+ log .Fatalf ("Unable to find the configuration file (%s) ensure %s environment variable is set properly" , pathEnv , configEnvVarName )
198
214
}
199
215
}
200
216
201
- switch config {
217
+ switch pathEnv {
202
218
case
203
219
defaultDockerSAPMConfig ,
204
220
defaultDockerOTLPConfig ,
@@ -215,12 +231,11 @@ func setConfig() {
215
231
}
216
232
}
217
233
218
- args := os .Args [1 :]
219
- if ! contains (args , "--config" ) {
234
+ if ! contains (os .Args [1 :], "--config" ) {
220
235
// Inject the command line flag that controls the configuration.
221
- os .Args = append (os .Args , "--config=" + config )
236
+ os .Args = append (os .Args , "--config=" + pathEnv )
222
237
}
223
- log .Printf ("Set config to %v" , config )
238
+ log .Printf ("Set config to %v" , pathEnv )
224
239
}
225
240
226
241
// Validate and set the memory ballast
0 commit comments