Skip to content

Commit 7b6c25b

Browse files
authored
Merge pull request #584 from DataDog/tyler/env-config
Add support for DD_TRACE_AGENT_PORT setting
2 parents 7d0aa46 + 5934958 commit 7b6c25b

File tree

5 files changed

+82
-18
lines changed

5 files changed

+82
-18
lines changed

dd-trace-api/src/main/java/datadog/trace/api/Config.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public class Config {
3131
public static final String SERVICE_NAME = "service.name";
3232
public static final String WRITER_TYPE = "writer.type";
3333
public static final String AGENT_HOST = "agent.host";
34-
public static final String AGENT_PORT = "agent.port";
34+
public static final String TRACE_AGENT_PORT = "trace.agent.port";
35+
public static final String AGENT_PORT_LEGACY = "agent.port";
3536
public static final String PRIORITY_SAMPLING = "priority.sampling";
3637
public static final String TRACE_RESOLVER_ENABLED = "trace.resolver.enabled";
3738
public static final String SERVICE_MAPPING = "service.mapping";
@@ -54,7 +55,7 @@ public class Config {
5455
public static final String DEFAULT_AGENT_WRITER_TYPE = DD_AGENT_WRITER_TYPE;
5556

5657
public static final String DEFAULT_AGENT_HOST = "localhost";
57-
public static final int DEFAULT_AGENT_PORT = 8126;
58+
public static final int DEFAULT_TRACE_AGENT_PORT = 8126;
5859

5960
private static final boolean DEFAULT_PRIORITY_SAMPLING_ENABLED = false;
6061
private static final boolean DEFAULT_TRACE_RESOLVER_ENABLED = true;
@@ -94,7 +95,10 @@ public class Config {
9495
serviceName = getSettingFromEnvironment(SERVICE_NAME, DEFAULT_SERVICE_NAME);
9596
writerType = getSettingFromEnvironment(WRITER_TYPE, DEFAULT_AGENT_WRITER_TYPE);
9697
agentHost = getSettingFromEnvironment(AGENT_HOST, DEFAULT_AGENT_HOST);
97-
agentPort = getIntegerSettingFromEnvironment(AGENT_PORT, DEFAULT_AGENT_PORT);
98+
agentPort =
99+
getIntegerSettingFromEnvironment(
100+
TRACE_AGENT_PORT,
101+
getIntegerSettingFromEnvironment(AGENT_PORT_LEGACY, DEFAULT_TRACE_AGENT_PORT));
98102
prioritySamplingEnabled =
99103
getBooleanSettingFromEnvironment(PRIORITY_SAMPLING, DEFAULT_PRIORITY_SAMPLING_ENABLED);
100104
traceResolverEnabled =
@@ -124,7 +128,11 @@ private Config(final Properties properties, final Config parent) {
124128
serviceName = properties.getProperty(SERVICE_NAME, parent.serviceName);
125129
writerType = properties.getProperty(WRITER_TYPE, parent.writerType);
126130
agentHost = properties.getProperty(AGENT_HOST, parent.agentHost);
127-
agentPort = getPropertyIntegerValue(properties, AGENT_PORT, parent.agentPort);
131+
agentPort =
132+
getPropertyIntegerValue(
133+
properties,
134+
TRACE_AGENT_PORT,
135+
getPropertyIntegerValue(properties, AGENT_PORT_LEGACY, parent.agentPort));
128136
prioritySamplingEnabled =
129137
getPropertyBooleanValue(properties, PRIORITY_SAMPLING, parent.prioritySamplingEnabled);
130138
traceResolverEnabled =

dd-trace-api/src/test/groovy/datadog/trace/api/ConfigTest.groovy

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class ConfigTest extends Specification {
1919
private static final DD_SPAN_TAGS_ENV = "DD_SPAN_TAGS"
2020
private static final DD_HEADER_TAGS_ENV = "DD_HEADER_TAGS"
2121
private static final DD_JMXFETCH_METRICS_CONFIGS_ENV = "DD_JMXFETCH_METRICS_CONFIGS"
22+
private static final DD_TRACE_AGENT_PORT_ENV = "DD_TRACE_AGENT_PORT"
23+
private static final DD_AGENT_PORT_LEGACY_ENV = "DD_AGENT_PORT"
2224

2325
def "verify defaults"() {
2426
when:
@@ -49,7 +51,8 @@ class ConfigTest extends Specification {
4951
System.setProperty(PREFIX + SERVICE_NAME, "something else")
5052
System.setProperty(PREFIX + WRITER_TYPE, "LoggingWriter")
5153
System.setProperty(PREFIX + AGENT_HOST, "somehost")
52-
System.setProperty(PREFIX + AGENT_PORT, "123")
54+
System.setProperty(PREFIX + TRACE_AGENT_PORT, "123")
55+
System.setProperty(PREFIX + AGENT_PORT_LEGACY, "456")
5356
System.setProperty(PREFIX + PRIORITY_SAMPLING, "true")
5457
System.setProperty(PREFIX + TRACE_RESOLVER_ENABLED, "false")
5558
System.setProperty(PREFIX + SERVICE_MAPPING, "a:1")
@@ -105,11 +108,12 @@ class ConfigTest extends Specification {
105108
setup:
106109
environmentVariables.set(DD_SERVICE_NAME_ENV, "still something else")
107110
environmentVariables.set(DD_WRITER_TYPE_ENV, "LoggingWriter")
111+
environmentVariables.set(DD_TRACE_AGENT_PORT_ENV, "777")
108112

109113
System.setProperty(PREFIX + SERVICE_NAME, "what we actually want")
110114
System.setProperty(PREFIX + WRITER_TYPE, "DDAgentWriter")
111115
System.setProperty(PREFIX + AGENT_HOST, "somewhere")
112-
System.setProperty(PREFIX + AGENT_PORT, "9999")
116+
System.setProperty(PREFIX + TRACE_AGENT_PORT, "123")
113117

114118
when:
115119
def config = new Config()
@@ -118,7 +122,49 @@ class ConfigTest extends Specification {
118122
config.serviceName == "what we actually want"
119123
config.writerType == "DDAgentWriter"
120124
config.agentHost == "somewhere"
121-
config.agentPort == 9999
125+
config.agentPort == 123
126+
}
127+
128+
def "sys props and env vars overrides for trace_agent_port and agent_port_legacy as expected"() {
129+
setup:
130+
if (overridePortEnvVar) {
131+
environmentVariables.set(DD_TRACE_AGENT_PORT_ENV, "777")
132+
}
133+
if (overrideLegacyPortEnvVar) {
134+
environmentVariables.set(DD_AGENT_PORT_LEGACY_ENV, "888")
135+
}
136+
137+
if (overridePort) {
138+
System.setProperty(PREFIX + TRACE_AGENT_PORT, "123")
139+
}
140+
if (overrideLegacyPort) {
141+
System.setProperty(PREFIX + AGENT_PORT_LEGACY, "456")
142+
}
143+
144+
when:
145+
def config = new Config()
146+
147+
then:
148+
config.agentPort == expectedPort
149+
150+
where:
151+
overridePort | overrideLegacyPort | overridePortEnvVar | overrideLegacyPortEnvVar | expectedPort
152+
true | true | false | false | 123
153+
true | false | false | false | 123
154+
false | true | false | false | 456
155+
false | false | false | false | 8126
156+
true | true | true | false | 123
157+
true | false | true | false | 123
158+
false | true | true | false | 777 // env var gets picked up instead.
159+
false | false | true | false | 777 // env var gets picked up instead.
160+
true | true | false | true | 123
161+
true | false | false | true | 123
162+
false | true | false | true | 456
163+
false | false | false | true | 888 // legacy env var gets picked up instead.
164+
true | true | true | true | 123
165+
true | false | true | true | 123
166+
false | true | true | true | 777 // env var gets picked up instead.
167+
false | false | true | true | 777 // env var gets picked up instead.
122168
}
123169

124170
def "sys props override properties"() {
@@ -127,7 +173,7 @@ class ConfigTest extends Specification {
127173
properties.setProperty(SERVICE_NAME, "something else")
128174
properties.setProperty(WRITER_TYPE, "LoggingWriter")
129175
properties.setProperty(AGENT_HOST, "somehost")
130-
properties.setProperty(AGENT_PORT, "123")
176+
properties.setProperty(TRACE_AGENT_PORT, "123")
131177
properties.setProperty(PRIORITY_SAMPLING, "true")
132178
properties.setProperty(TRACE_RESOLVER_ENABLED, "false")
133179
properties.setProperty(SERVICE_MAPPING, "a:1")

dd-trace-ot/src/main/java/datadog/trace/common/writer/DDAgentWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package datadog.trace.common.writer;
22

33
import static datadog.trace.api.Config.DEFAULT_AGENT_HOST;
4-
import static datadog.trace.api.Config.DEFAULT_AGENT_PORT;
4+
import static datadog.trace.api.Config.DEFAULT_TRACE_AGENT_PORT;
55

66
import datadog.opentracing.DDSpan;
77
import java.util.List;
@@ -64,7 +64,7 @@ public Thread newThread(final Runnable r) {
6464
private boolean queueFullReported = false;
6565

6666
public DDAgentWriter() {
67-
this(new DDApi(DEFAULT_AGENT_HOST, DEFAULT_AGENT_PORT));
67+
this(new DDApi(DEFAULT_AGENT_HOST, DEFAULT_TRACE_AGENT_PORT));
6868
}
6969

7070
public DDAgentWriter(final DDApi api) {

dd-trace-ot/src/test/groovy/datadog/trace/DDTracerTest.groovy

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import datadog.trace.api.Config
55
import datadog.trace.common.sampling.AllSampler
66
import datadog.trace.common.sampling.RateByServiceSampler
77
import datadog.trace.common.writer.DDAgentWriter
8-
import datadog.trace.common.writer.DDApi
98
import datadog.trace.common.writer.ListWriter
109
import datadog.trace.common.writer.LoggingWriter
1110
import org.junit.Rule
@@ -27,6 +26,16 @@ class DDTracerTest extends Specification {
2726
@Rule
2827
public final EnvironmentVariables environmentVariables = new EnvironmentVariables()
2928

29+
def setupSpec() {
30+
// assert that a trace agent isn't running locally as that messes up the test.
31+
try {
32+
(new Socket("localhost", 8126)).close()
33+
throw new IllegalStateException("Trace Agent unexpectedly running locally.")
34+
} catch (final ConnectException ioe) {
35+
// trace agent is not running locally.
36+
}
37+
}
38+
3039
def "verify defaults on tracer"() {
3140
when:
3241
def tracer = new DDTracer()
@@ -93,11 +102,12 @@ class DDTracerTest extends Specification {
93102

94103
where:
95104

96-
source | key | value | expected
97-
"writer" | "default" | "default" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://localhost:8126/v0.3/traces } }"
98-
"writer" | "writer.type" | "LoggingWriter" | "LoggingWriter { }"
99-
"writer" | "agent.host" | "somethingelse" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://somethingelse:8126/v0.3/traces } }"
100-
"writer" | "agent.port" | "9999" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://localhost:9999/v0.3/traces } }"
105+
source | key | value | expected
106+
"writer" | "default" | "default" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://localhost:8126/v0.3/traces } }"
107+
"writer" | "writer.type" | "LoggingWriter" | "LoggingWriter { }"
108+
"writer" | "agent.host" | "somethingelse" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://somethingelse:8126/v0.3/traces } }"
109+
"writer" | "agent.port" | "777" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://localhost:777/v0.3/traces } }"
110+
"writer" | "trace.agent.port" | "9999" | "DDAgentWriter { api=DDApi { tracesEndpoint=http://localhost:9999/v0.3/traces } }"
101111
}
102112

103113
def "verify sampler/writer constructor"() {

dd-trace-ot/src/traceAgentTest/groovy/DDApiIntegrationTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import java.util.concurrent.TimeUnit
1212
import java.util.concurrent.atomic.AtomicReference
1313

1414
import static datadog.trace.api.Config.DEFAULT_AGENT_HOST
15-
import static datadog.trace.api.Config.DEFAULT_AGENT_PORT
15+
import static datadog.trace.api.Config.DEFAULT_TRACE_AGENT_PORT
1616

1717
class DDApiIntegrationTest {
1818
static class DDApiIntegrationV4Test extends Specification {
@@ -33,7 +33,7 @@ class DDApiIntegrationTest {
3333
new PendingTrace(TRACER, "1", [:]),
3434
TRACER)
3535

36-
def api = new DDApi(DEFAULT_AGENT_HOST, DEFAULT_AGENT_PORT, v4())
36+
def api = new DDApi(DEFAULT_AGENT_HOST, DEFAULT_TRACE_AGENT_PORT, v4())
3737

3838
def endpoint = new AtomicReference<String>(null)
3939
def agentResponse = new AtomicReference<String>(null)

0 commit comments

Comments
 (0)