Skip to content

Commit a471b51

Browse files
committed
Add env var SPLUNK_CONFIG_YAML for storing configuration YAML
1 parent 173e799 commit a471b51

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
lines changed

cmd/otelcol/main.go

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package main
1919

2020
import (
21+
"bytes"
2122
"fmt"
2223
"log"
2324
"os"
@@ -26,6 +27,7 @@ import (
2627

2728
"go.opentelemetry.io/collector/component"
2829
"go.opentelemetry.io/collector/service"
30+
"go.opentelemetry.io/collector/service/parserprovider"
2931
"go.uber.org/zap"
3032

3133
"github.com/signalfx/splunk-otel-collector/internal/components"
@@ -37,6 +39,7 @@ import (
3739
const (
3840
ballastEnvVarName = "SPLUNK_BALLAST_SIZE_MIB"
3941
configEnvVarName = "SPLUNK_CONFIG"
42+
configYamlEnvVarName = "SPLUNK_CONFIG_YAML"
4043
memLimitMiBEnvVarName = "SPLUNK_MEMORY_LIMIT_MIB"
4144
memTotalEnvVarName = "SPLUNK_MEMORY_TOTAL_MIB"
4245
realmEnvVarName = "SPLUNK_REALM"
@@ -71,11 +74,18 @@ func main() {
7174
Version: version.Version,
7275
}
7376

77+
baseParserProvider := parserprovider.Default()
78+
if configYAML := os.Getenv(configYamlEnvVarName); configYAML != "" {
79+
baseParserProvider = parserprovider.NewInMemory(bytes.NewBufferString(configYAML))
80+
}
81+
7482
parserProvider := configprovider.NewConfigSourceParserProvider(
83+
baseParserProvider,
7584
zap.NewNop(), // The service logger is not available yet, setting it to NoP.
7685
info,
7786
configsources.Get()...,
7887
)
88+
7989
serviceParams := service.Parameters{
8090
BuildInfo: info,
8191
Factories: factories,
@@ -124,21 +134,7 @@ func getKeyValue(args []string, argName string) string {
124134
// Config and ballast flags are checked
125135
// Config and all memory env vars are checked
126136
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()
142138

143139
// Set default total memory
144140
memTotalSizeMiB := defaultMemoryTotalMiB
@@ -161,10 +157,9 @@ func checkRuntimeParams() {
161157
// Check if memory ballast flag was passed
162158
// If so, ensure memory ballast env var is not set
163159
// 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")
165161
if ballastSize != "" {
166-
config = os.Getenv(ballastEnvVarName)
167-
if config != "" {
162+
if os.Getenv(ballastEnvVarName) != "" {
168163
log.Fatalf("Both %v and '--config' were specified, but only one is allowed", ballastEnvVarName)
169164
}
170165
os.Setenv(ballastEnvVarName, ballastSize)
@@ -174,31 +169,52 @@ func checkRuntimeParams() {
174169
}
175170

176171
// 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 == "" {
182198
_, err := os.Stat(defaultDockerSAPMConfig)
183199
if err == nil {
184-
config = defaultDockerSAPMConfig
200+
pathEnv = defaultDockerSAPMConfig
185201
}
186202
_, err = os.Stat(defaultLocalSAPMConfig)
187203
if err == nil {
188-
config = defaultLocalSAPMConfig
204+
pathEnv = defaultLocalSAPMConfig
189205
}
190-
if config == "" {
206+
if pathEnv == "" {
191207
log.Fatalf("Unable to find the default configuration file, ensure %s environment variable is set properly", configEnvVarName)
192208
}
193209
} else {
194210
// Check if file exists.
195-
_, err := os.Stat(config)
211+
_, err := os.Stat(pathEnv)
196212
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)
198214
}
199215
}
200216

201-
switch config {
217+
switch pathEnv {
202218
case
203219
defaultDockerSAPMConfig,
204220
defaultDockerOTLPConfig,
@@ -215,12 +231,11 @@ func setConfig() {
215231
}
216232
}
217233

218-
args := os.Args[1:]
219-
if !contains(args, "--config") {
234+
if !contains(os.Args[1:], "--config") {
220235
// 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)
222237
}
223-
log.Printf("Set config to %v", config)
238+
log.Printf("Set config to %v", pathEnv)
224239
}
225240

226241
// Validate and set the memory ballast

cmd/otelcol/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestGetKeyValue(t *testing.T) {
6060
func TestCheckRuntimeParams(*testing.T) {
6161
oldArgs := os.Args
6262
os.Setenv(configEnvVarName, "../../"+defaultLocalSAPMConfig)
63-
setConfig()
63+
setConfigSource()
6464
os.Unsetenv(configEnvVarName)
6565
checkRuntimeParams()
6666

@@ -101,7 +101,7 @@ func TestUseConfigFromEnvVar(t *testing.T) {
101101
os.Setenv(tokenEnvVarName, "12345")
102102
os.Setenv(realmEnvVarName, "us0")
103103
os.Setenv(configEnvVarName, "../../"+defaultLocalSAPMConfig)
104-
setConfig()
104+
setConfigSource()
105105

106106
args := os.Args[1:]
107107
c := getKeyValue(args, "--config")

internal/configprovider/config_source_provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ type configSourceParserProvider struct {
3737
}
3838

3939
// NewConfigSourceParserProvider creates a ParserProvider that uses config sources.
40-
func NewConfigSourceParserProvider(logger *zap.Logger, buildInfo component.BuildInfo, factories ...Factory) parserprovider.ParserProvider {
40+
func NewConfigSourceParserProvider(pp parserprovider.ParserProvider, logger *zap.Logger, buildInfo component.BuildInfo, factories ...Factory) parserprovider.ParserProvider {
4141
return &configSourceParserProvider{
42-
pp: parserprovider.Default(),
42+
pp: pp,
4343
logger: logger,
4444
factories: factories,
4545
buildInfo: buildInfo,

internal/configprovider/config_source_provider_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func TestConfigSourceParserProvider(t *testing.T) {
8888
}
8989

9090
pp := NewConfigSourceParserProvider(
91+
parserprovider.Default(),
9192
zap.NewNop(),
9293
component.DefaultBuildInfo(),
9394
factories...,

0 commit comments

Comments
 (0)