Skip to content

Commit

Permalink
Override reporter config only when agent host/port is set in env (#513)
Browse files Browse the repository at this point in the history
* override reporter config only when agent host or port was set in env

Signed-off-by: ilylia <ilylia@126.com>

* fmt fixed

Signed-off-by: ilylia <ilylia@126.com>

* split to new test

Signed-off-by: ilylia <ilylia@126.com>
  • Loading branch information
ilylia committed May 14, 2020
1 parent 4293012 commit c6853b8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
7 changes: 6 additions & 1 deletion config/config_env.go
Expand Up @@ -187,20 +187,25 @@ func (rc *ReporterConfig) reporterConfigFromEnv() (*ReporterConfig, error) {
rc.User = user
rc.Password = pswd
} else {
useEnv := false
host := jaeger.DefaultUDPSpanServerHost
if e := os.Getenv(envAgentHost); e != "" {
host = e
useEnv = true
}

port := jaeger.DefaultUDPSpanServerPort
if e := os.Getenv(envAgentPort); e != "" {
if value, err := strconv.ParseInt(e, 10, 0); err == nil {
port = int(value)
useEnv = true
} else {
return nil, errors.Wrapf(err, "cannot parse env var %s=%s", envAgentPort, e)
}
}
rc.LocalAgentHostPort = fmt.Sprintf("%s:%d", host, port)
if useEnv || rc.LocalAgentHostPort == "" {
rc.LocalAgentHostPort = fmt.Sprintf("%s:%d", host, port)
}
}

return rc, nil
Expand Down
62 changes: 62 additions & 0 deletions config/config_test.go
Expand Up @@ -398,6 +398,68 @@ func TestReporterConfigFromEnv(t *testing.T) {
unsetEnv(t, envPassword)
}

func TestReporterAgentConfigFromEnv(t *testing.T) {
// prepare
unsetEnv(t, envEndpoint)
unsetEnv(t, envAgentHost)
unsetEnv(t, envAgentPort)

// No config and no env check
rc := ReporterConfig{}

// test
cfg, err := rc.reporterConfigFromEnv()
assert.NoError(t, err)

// verify
assert.Equal(t, "localhost:6831", cfg.LocalAgentHostPort)

// No env check
rc = ReporterConfig{
LocalAgentHostPort: "localhost01:7777",
}

// test
cfg, err = rc.reporterConfigFromEnv()
assert.NoError(t, err)

// verify
assert.Equal(t, "localhost01:7777", cfg.LocalAgentHostPort)

// Only host env check
setEnv(t, envAgentHost, "localhost02")
unsetEnv(t, envAgentPort)
rc = ReporterConfig{
LocalAgentHostPort: "localhost01:7777",
}

// test
cfg, err = rc.reporterConfigFromEnv()
assert.NoError(t, err)

// verify
assert.Equal(t, "localhost02:6831", cfg.LocalAgentHostPort)

// Only port env check
unsetEnv(t, envAgentHost)
setEnv(t, envAgentPort, "8888")
rc = ReporterConfig{
LocalAgentHostPort: "localhost01:7777",
}

// test
cfg, err = rc.reporterConfigFromEnv()
assert.NoError(t, err)

// verify
assert.Equal(t, "localhost:8888", cfg.LocalAgentHostPort)

// cleanup
unsetEnv(t, envEndpoint)
unsetEnv(t, envAgentHost)
unsetEnv(t, envAgentPort)
}

func TestParsingErrorsFromEnv(t *testing.T) {
setEnv(t, envAgentHost, "localhost") // we require this in order to test the parsing of the port

Expand Down

0 comments on commit c6853b8

Please sign in to comment.