Skip to content

Commit

Permalink
Preset default value for SPLUNK_TRACE_URL from SPLUNK_INGEST_URL (#5670)
Browse files Browse the repository at this point in the history
If SPLUNK_INGEST_URL set by the user, use it to populate default value for SPLUNK_TRACE_URL.

This is a preliminary step before we can deprecate and remove SPLUNK_TRACE_URL
  • Loading branch information
dmitryax authored Dec 5, 2024
1 parent 3f739af commit 7f963ba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
36 changes: 17 additions & 19 deletions internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,36 +376,34 @@ func checkRuntimeParams(settings *Settings) error {
}

func setDefaultEnvVars(s *Settings) error {
type ev struct {
e, v string
log bool
defaultEnvVars := map[string]string{
ListenInterfaceEnvVar: defaultListenAddr(s),
ConfigServerEnabledEnvVar: "true",
}

envVars := []ev{
{e: ListenInterfaceEnvVar, v: defaultListenAddr(s), log: true},
{e: ConfigServerEnabledEnvVar, v: "true"},
if realm, ok := os.LookupEnv(RealmEnvVar); ok {
defaultEnvVars[APIURLEnvVar] = fmt.Sprintf("https://api.%s.signalfx.com", realm)
defaultEnvVars[IngestURLEnvVar] = fmt.Sprintf("https://ingest.%s.signalfx.com", realm)
defaultEnvVars[TraceIngestURLEnvVar] = fmt.Sprintf("https://ingest.%s.signalfx.com/v2/trace", realm)
defaultEnvVars[HecLogIngestURLEnvVar] = fmt.Sprintf("https://ingest.%s.signalfx.com/v1/log", realm)
}

if realm, ok := os.LookupEnv(RealmEnvVar); ok {
envVars = append(envVars,
ev{e: APIURLEnvVar, v: fmt.Sprintf("https://api.%s.signalfx.com", realm)},
ev{e: IngestURLEnvVar, v: fmt.Sprintf("https://ingest.%s.signalfx.com", realm)},
ev{e: TraceIngestURLEnvVar, v: fmt.Sprintf("https://ingest.%s.signalfx.com/v2/trace", realm)},
ev{e: HecLogIngestURLEnvVar, v: fmt.Sprintf("https://ingest.%s.signalfx.com/v1/log", realm)},
)
if ingestURL, ok := os.LookupEnv(IngestURLEnvVar); ok {
ingestURL = strings.TrimSuffix(ingestURL, "/")
defaultEnvVars[TraceIngestURLEnvVar] = fmt.Sprintf("%s/v2/trace", ingestURL)
}

if token, ok := os.LookupEnv(TokenEnvVar); ok {
envVars = append(envVars, ev{e: HecTokenEnvVar, v: token})
defaultEnvVars[HecTokenEnvVar] = token
}

for _, envVar := range envVars {
if _, ok := os.LookupEnv(envVar.e); !ok {
if err := os.Setenv(envVar.e, envVar.v); err != nil {
for e, v := range defaultEnvVars {
if _, ok := os.LookupEnv(e); !ok {
if err := os.Setenv(e, v); err != nil {
return err
}
if envVar.log {
log.Printf("set %q to %q", envVar.e, envVar.v)
if e == ListenInterfaceEnvVar {
log.Printf("set %q to %q", e, v)
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions internal/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,17 @@ func TestSetDefaultEnvVarsSetsHECTokenFromAccessTokenEnvVar(t *testing.T) {
assert.Equal(t, token, val)
}

func TestSetDefaultEnvVarsSetsTraceURLFromIngestURL(t *testing.T) {
t.Cleanup(clearEnv(t))

os.Setenv("SPLUNK_INGEST_URL", "https://ingest.fake-realm.signalfx.com/")
require.NoError(t, setDefaultEnvVars(nil))

val, ok := os.LookupEnv("SPLUNK_TRACE_URL")
assert.True(t, ok)
assert.Equal(t, "https://ingest.fake-realm.signalfx.com/v2/trace", val)
}

func TestSetDefaultEnvVarsRespectsSetEnvVars(t *testing.T) {
t.Cleanup(clearEnv(t))
envVars := []string{"SPLUNK_API_URL", "SPLUNK_INGEST_URL", "SPLUNK_TRACE_URL", "SPLUNK_HEC_URL", "SPLUNK_HEC_TOKEN", "SPLUNK_LISTEN_INTERFACE"}
Expand Down

0 comments on commit 7f963ba

Please sign in to comment.