Skip to content

Commit 93c046a

Browse files
committed
Add support for DD_TRACE_AGENT_PORT setting
Makes this more consistent with other apm language clients.
1 parent 7d0aa46 commit 93c046a

5 files changed

Lines changed: 69 additions & 18 deletions

File tree

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: 37 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")
@@ -92,24 +95,47 @@ class ConfigTest extends Specification {
9295
environmentVariables.set(DD_WRITER_TYPE_ENV, "LoggingWriter")
9396
environmentVariables.set(DD_JMXFETCH_METRICS_CONFIGS_ENV, "some/file")
9497

98+
if (overridePort) {
99+
environmentVariables.set(DD_TRACE_AGENT_PORT_ENV, "123")
100+
}
101+
if (overrideLegacyPort) {
102+
environmentVariables.set(DD_AGENT_PORT_LEGACY_ENV, "456")
103+
}
104+
95105
when:
96106
def config = new Config()
97107

98108
then:
99109
config.serviceName == "still something else"
100110
config.writerType == "LoggingWriter"
101111
config.jmxFetchMetricsConfigs == ["some/file"]
112+
config.agentPort == expectedPort
113+
114+
where:
115+
overridePort | overrideLegacyPort | expectedPort
116+
true | true | 123
117+
true | false | 123
118+
false | true | 456
119+
false | false | 8126
120+
102121
}
103122

104123
def "sys props override env vars"() {
105124
setup:
106125
environmentVariables.set(DD_SERVICE_NAME_ENV, "still something else")
107126
environmentVariables.set(DD_WRITER_TYPE_ENV, "LoggingWriter")
127+
environmentVariables.set(DD_TRACE_AGENT_PORT_ENV, "777")
108128

109129
System.setProperty(PREFIX + SERVICE_NAME, "what we actually want")
110130
System.setProperty(PREFIX + WRITER_TYPE, "DDAgentWriter")
111131
System.setProperty(PREFIX + AGENT_HOST, "somewhere")
112-
System.setProperty(PREFIX + AGENT_PORT, "9999")
132+
133+
if (overridePort) {
134+
System.setProperty(PREFIX + TRACE_AGENT_PORT, "123")
135+
}
136+
if (overrideLegacyPort) {
137+
System.setProperty(PREFIX + AGENT_PORT_LEGACY, "456")
138+
}
113139

114140
when:
115141
def config = new Config()
@@ -118,7 +144,14 @@ class ConfigTest extends Specification {
118144
config.serviceName == "what we actually want"
119145
config.writerType == "DDAgentWriter"
120146
config.agentHost == "somewhere"
121-
config.agentPort == 9999
147+
config.agentPort == expectedPort
148+
149+
where:
150+
overridePort | overrideLegacyPort | expectedPort
151+
true | true | 123
152+
true | false | 123
153+
false | true | 777 // env var gets picked up instead.
154+
false | false | 777 // env var gets picked up instead.
122155
}
123156

124157
def "sys props override properties"() {
@@ -127,7 +160,7 @@ class ConfigTest extends Specification {
127160
properties.setProperty(SERVICE_NAME, "something else")
128161
properties.setProperty(WRITER_TYPE, "LoggingWriter")
129162
properties.setProperty(AGENT_HOST, "somehost")
130-
properties.setProperty(AGENT_PORT, "123")
163+
properties.setProperty(TRACE_AGENT_PORT, "123")
131164
properties.setProperty(PRIORITY_SAMPLING, "true")
132165
properties.setProperty(TRACE_RESOLVER_ENABLED, "false")
133166
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)