Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddtrace/tracer: fixed precedence ordering of configuration options #1232

Merged
merged 5 commits into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ddtrace/tracer/option.go
Expand Up @@ -177,7 +177,7 @@ func forEachStringTag(str string, fn func(key string, val string)) {
func newConfig(opts ...StartOption) *config {
c := new(config)
c.sampler = NewAllSampler()
c.agentAddr = resolveAgentAddr(defaultAddress)
c.agentAddr = resolveAgentAddr("")
Copy link
Contributor

@gbbr gbbr Apr 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably best to get rid of this completely and follow the same pattern as the other settings. We can move the env. var. checks together with the rest of the env. var checks for the other options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would have to do it after running all the in-code options, I doubt that this change would contribute to cleaner code. Also, since it's in a separate function order of precedence is clear, no need to worry about someone moving around pieces of code

forEachStringTag(v, func(key, val string) { WithServiceMapping(key, val)(c) })
}
if v := os.Getenv("DD_TAGS"); v != "" {
forEachStringTag(v, func(key, val string) { WithGlobalTag(key, val)(c) })
}
if _, ok := os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME"); ok {
// AWS_LAMBDA_FUNCTION_NAME being set indicates that we're running in an AWS Lambda environment.
// See: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
c.logToStdout = true
}
c.logStartup = internal.BoolEnv("DD_TRACE_STARTUP_LOGS", true)
c.runtimeMetrics = internal.BoolEnv("DD_RUNTIME_METRICS_ENABLED", false)
c.debug = internal.BoolEnv("DD_TRACE_DEBUG", false)
c.enabled = internal.BoolEnv("DD_TRACE_ENABLED", true)
c.profilerEndpoints = internal.BoolEnv(traceprof.EndpointEnvVar, true)
c.profilerHotspots = internal.BoolEnv(traceprof.CodeHotspotsEnvVar, true)
for _, fn := range opts {
fn(c)
}
WithGlobalTag(ext.RuntimeID, globalconfig.RuntimeID())(c)
if c.env == "" {

As for the regression test, we have up to date TestResolveAgentAddr, which verify correct order of in-code options, env variables, and default values.

func TestResolveAgentAddr(t *testing.T) {
for _, tt := range []struct {
in, envHost, envPort, out string
}{
{"host", "", "", fmt.Sprintf("host:%s", defaultPort)},
{"www.my-address.com", "", "", fmt.Sprintf("www.my-address.com:%s", defaultPort)},
{"localhost", "", "", fmt.Sprintf("localhost:%s", defaultPort)},
{":1111", "", "", fmt.Sprintf("%s:1111", defaultHostname)},
{"", "", "", defaultAddress},

Copy link
Contributor

@gbbr gbbr Apr 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the regression test, we have up to date TestResolveAgentAddr, which verify correct order of in-code options, env variables, and default values.

Great! I didn't notice it at first, the Github diff was not too great.

[...] I doubt that this change would contribute to cleaner code [...]

It's about being consistent. Also, you are passing an empty string to a function which is not used anywhere else and technically its argument has no value. You are intentionally passing bad input to force a certain code path. I don't think that's the right approach. It's an old piece of code. Better to get rid of it and be consistent. Or am I missing something? Why do we need it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon it's better to have the logic in the separate function, because otherwise 'configuration precedence order' relies only on the position in the code, which could be changed accidentaly in the future. Instead, I'll remove the arguments to the resolveAddr func and update tests according to that

c.httpClient = defaultHTTPClient()

if internal.BoolEnv("DD_TRACE_ANALYTICS_ENABLED", false) {
Expand Down
16 changes: 8 additions & 8 deletions ddtrace/tracer/transport.go
Expand Up @@ -101,8 +101,8 @@ func newHTTPTransport(addr string, client *http.Client) *httpTransport {
defaultHeaders["Datadog-Container-ID"] = cid
}
return &httpTransport{
traceURL: fmt.Sprintf("http://%s/v0.4/traces", resolveAgentAddr(addr)),
statsURL: fmt.Sprintf("http://%s/v0.6/stats", resolveAgentAddr(addr)),
traceURL: fmt.Sprintf("http://%s/v0.4/traces", addr),
statsURL: fmt.Sprintf("http://%s/v0.6/stats", addr),
client: client,
headers: defaultHeaders,
}
Expand Down Expand Up @@ -192,17 +192,17 @@ func resolveAgentAddr(addr string) string {
// no port in addr
host = addr
}
if v := os.Getenv("DD_AGENT_HOST"); v != "" && host == "" {
host = v
}
if v := os.Getenv("DD_TRACE_AGENT_PORT"); v != "" && port == "" {
port = v
}
if host == "" {
host = defaultHostname
}
if port == "" {
port = defaultPort
}
if v := os.Getenv("DD_AGENT_HOST"); v != "" {
host = v
}
if v := os.Getenv("DD_TRACE_AGENT_PORT"); v != "" {
port = v
}
return fmt.Sprintf("%s:%s", host, port)
}
15 changes: 8 additions & 7 deletions ddtrace/tracer/transport_test.go
Expand Up @@ -87,16 +87,17 @@ func TestResolveAgentAddr(t *testing.T) {
{":1111", "", "", fmt.Sprintf("%s:1111", defaultHostname)},
{"", "", "", defaultAddress},
{"custom:1234", "", "", "custom:1234"},
{"", "", "", defaultAddress},
{"", "ip.local", "", fmt.Sprintf("ip.local:%s", defaultPort)},
{"", "", "1234", fmt.Sprintf("%s:1234", defaultHostname)},
{"", "ip.local", "1234", "ip.local:1234"},
{"ip.other", "ip.local", "", fmt.Sprintf("ip.local:%s", defaultPort)},
{"ip.other:1234", "ip.local", "", "ip.local:1234"},
{":8888", "", "1234", fmt.Sprintf("%s:1234", defaultHostname)},
{"ip.other:8888", "", "1234", "ip.other:1234"},
{"ip.other", "ip.local", "1234", "ip.local:1234"},
{"ip.other:8888", "ip.local", "1234", "ip.local:1234"},
{"ip.other", "ip.local", "", fmt.Sprintf("ip.other:%s", defaultPort)},
{"ip.other:1234", "ip.local", "", "ip.other:1234"},
{":8888", "", "1234", fmt.Sprintf("%s:8888", defaultHostname)},
{"ip.other:8888", "", "1234", "ip.other:8888"},
{"ip.other", "ip.local", "1234", "ip.other:1234"},
{"ip.other:8888", "ip.local", "1234", "ip.other:8888"},
{"ip.other", "ip.local", "1234", fmt.Sprintf("ip.other:%v", 1234)},
{"ip.other:1234", "ip.local", "", "ip.other:1234"},
} {
t.Run("", func(t *testing.T) {
if tt.envHost != "" {
Expand Down