Skip to content

Commit f052e9a

Browse files
authored
Merge pull request #241 from DataDog/brian/AMLII-1676-send-both-cid-entity-id
Origin Detection: Send both container ID and entity ID
2 parents 88afc7b + 6d94b38 commit f052e9a

2 files changed

Lines changed: 41 additions & 21 deletions

File tree

src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ protected static String format(ThreadLocal<NumberFormat> formatter, Number value
276276
}
277277
}
278278
// Support "dd.internal.entity_id" internal tag.
279-
final boolean hasEntityID = updateTagsWithEntityID(costantPreTags, entityID);
279+
updateTagsWithEntityID(costantPreTags, entityID);
280280
for (final Literal literal : Literal.values()) {
281281
final String envVal = literal.envVal();
282282
if (envVal != null && !envVal.trim().isEmpty()) {
@@ -291,12 +291,8 @@ protected static String format(ThreadLocal<NumberFormat> formatter, Number value
291291
}
292292
costantPreTags = null;
293293
// Origin detection
294-
if (hasEntityID) {
295-
containerID = null;
296-
} else {
297-
boolean originEnabled = isOriginDetectionEnabled(containerID, originDetectionEnabled, hasEntityID);
298-
containerID = getContainerID(containerID, originEnabled);
299-
}
294+
boolean originEnabled = isOriginDetectionEnabled(containerID, originDetectionEnabled);
295+
containerID = getContainerID(containerID, originEnabled);
300296
}
301297

302298
try {
@@ -1310,10 +1306,9 @@ protected boolean isInvalidSample(double sampleRate) {
13101306
return sampleRate != 1 && ThreadLocalRandom.current().nextDouble() > sampleRate;
13111307
}
13121308

1313-
boolean isOriginDetectionEnabled(String containerID, boolean originDetectionEnabled, boolean hasEntityID) {
1314-
if (!originDetectionEnabled || hasEntityID || (containerID != null && !containerID.isEmpty())) {
1309+
boolean isOriginDetectionEnabled(String containerID, boolean originDetectionEnabled) {
1310+
if (!originDetectionEnabled || (containerID != null && !containerID.isEmpty())) {
13151311
// origin detection is explicitly disabled
1316-
// or DD_ENTITY_ID was found
13171312
// or a user-defined container ID was provided
13181313
return false;
13191314
}

src/test/java/com/timgroup/statsd/NonBlockingStatsDClientTest.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -714,12 +714,13 @@ public void sends_gauge_entityID_from_env_deprecated() throws Exception {
714714
.hostname("localhost")
715715
.port(STATSD_SERVER_PORT)
716716
.queueSize(Integer.MAX_VALUE)
717+
.containerID("fake-container-id")
717718
.build();
718719
try {
719720
client.gauge("value", 423);
720721
server.waitForMessage("my.prefix");
721722

722-
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity")));
723+
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity|c:fake-container-id")));
723724
} finally {
724725
client.stop();
725726
}
@@ -734,12 +735,13 @@ public void sends_gauge_entityID_from_env() throws Exception {
734735
.hostname("localhost")
735736
.port(STATSD_SERVER_PORT)
736737
.queueSize(Integer.MAX_VALUE)
738+
.containerID("fake-container-id")
737739
.build();
738740
try {
739741
client.gauge("value", 423);
740742
server.waitForMessage();
741743

742-
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity")));
744+
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity|c:fake-container-id")));
743745
} finally {
744746
client.stop();
745747
}
@@ -756,12 +758,13 @@ public void sends_gauge_entityID_from_env_and_constant_tags_deprecated() throws
756758
.port(STATSD_SERVER_PORT)
757759
.queueSize(Integer.MAX_VALUE)
758760
.constantTags(constantTags)
761+
.containerID("fake-container-id")
759762
.build();
760763
try {
761764
client.gauge("value", 423);
762765
server.waitForMessage("my.prefix");
763766

764-
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity," + constantTags)));
767+
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity," + constantTags + "|c:fake-container-id")));
765768
} finally {
766769
client.stop();
767770
}
@@ -802,13 +805,14 @@ public void sends_gauge_entityID_from_args_deprecated() throws Exception {
802805
.constantTags(null)
803806
.errorHandler(null)
804807
.entityID(entity_value+"-arg")
808+
.containerID("fake-container-id")
805809
.build();
806810

807811
try {
808812
client.gauge("value", 423);
809813
server.waitForMessage("my.prefix");
810814

811-
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity-arg")));
815+
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity-arg|c:fake-container-id")));
812816
} finally {
813817
client.stop();
814818
}
@@ -824,21 +828,21 @@ public void sends_gauge_entityID_from_args() throws Exception {
824828
.port(STATSD_SERVER_PORT)
825829
.queueSize(Integer.MAX_VALUE)
826830
.entityID(entity_value+"-arg")
831+
.containerID("fake-container-id")
827832
.build();
828833
try {
829834
client.gauge("value", 423);
830835
server.waitForMessage("my.prefix");
831836

832-
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity-arg")));
837+
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity-arg|c:fake-container-id")));
833838
} finally {
834839
client.stop();
835840
}
836841
}
837842

838-
839843
@Test(timeout = 5000L)
840844
public void init_client_from_env_vars() throws Exception {
841-
final String entity_value = "foo-entity";
845+
final String entity_value = "foo-entity";
842846
environmentVariables.set(NonBlockingStatsDClient.DD_DOGSTATSD_PORT_ENV_VAR, Integer.toString(STATSD_SERVER_PORT));
843847
environmentVariables.set(NonBlockingStatsDClient.DD_AGENT_HOST_ENV_VAR, "localhost");
844848
final NonBlockingStatsDClient client = new NonBlockingStatsDClientBuilder()
@@ -1660,6 +1664,27 @@ public void sends_event_with_containerID() throws Exception {
16601664
);
16611665
}
16621666

1667+
@Test(timeout = 5000L)
1668+
public void test_entity_id_and_container_id() throws Exception {
1669+
final String entity_value = "foo-entity";
1670+
environmentVariables.set(NonBlockingStatsDClient.DD_ENTITY_ID_ENV_VAR, entity_value);
1671+
final NonBlockingStatsDClient client = new NonBlockingStatsDClientBuilder()
1672+
.prefix("my.prefix")
1673+
.hostname("localhost")
1674+
.port(STATSD_SERVER_PORT)
1675+
.enableTelemetry(false)
1676+
.containerID("fake-container-id")
1677+
.build();
1678+
try {
1679+
client.gauge("value", 423);
1680+
server.waitForMessage();
1681+
1682+
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity|c:fake-container-id")));
1683+
} finally {
1684+
client.stop();
1685+
}
1686+
}
1687+
16631688
@Test(timeout = 5000L)
16641689
public void origin_detection_env_false() throws Exception {
16651690
environmentVariables.set(NonBlockingStatsDClient.ORIGIN_DETECTION_ENABLED_ENV_VAR, "false");
@@ -1673,7 +1698,7 @@ public void origin_detection_env_false() throws Exception {
16731698
.enableTelemetry(false)
16741699
.build();
16751700

1676-
assertFalse(client.isOriginDetectionEnabled(null, NonBlockingStatsDClient.DEFAULT_ENABLE_ORIGIN_DETECTION, false));
1701+
assertFalse(client.isOriginDetectionEnabled(null, NonBlockingStatsDClient.DEFAULT_ENABLE_ORIGIN_DETECTION));
16771702
environmentVariables.clear(NonBlockingStatsDClient.ORIGIN_DETECTION_ENABLED_ENV_VAR);
16781703
}
16791704

@@ -1690,7 +1715,7 @@ public void origin_detection_env_unknown() throws Exception {
16901715
.enableTelemetry(false)
16911716
.build();
16921717

1693-
assertTrue(client.isOriginDetectionEnabled(null, NonBlockingStatsDClient.DEFAULT_ENABLE_ORIGIN_DETECTION, false));
1718+
assertTrue(client.isOriginDetectionEnabled(null, NonBlockingStatsDClient.DEFAULT_ENABLE_ORIGIN_DETECTION));
16941719
environmentVariables.clear(NonBlockingStatsDClient.ORIGIN_DETECTION_ENABLED_ENV_VAR);
16951720
}
16961721

@@ -1705,7 +1730,7 @@ public void origin_detection_env_unset() throws Exception {
17051730
.enableTelemetry(false)
17061731
.build();
17071732

1708-
assertTrue(client.isOriginDetectionEnabled(null, NonBlockingStatsDClient.DEFAULT_ENABLE_ORIGIN_DETECTION, false));
1733+
assertTrue(client.isOriginDetectionEnabled(null, NonBlockingStatsDClient.DEFAULT_ENABLE_ORIGIN_DETECTION));
17091734
}
17101735

17111736
@Test(timeout = 5000L)
@@ -1719,7 +1744,7 @@ public void origin_detection_arg_false() throws Exception {
17191744
.enableTelemetry(false)
17201745
.build();
17211746

1722-
assertFalse(client.isOriginDetectionEnabled(null, false, false));
1747+
assertFalse(client.isOriginDetectionEnabled(null, false));
17231748
}
17241749

17251750
private static class SlowStatsDNonBlockingStatsDClient extends NonBlockingStatsDClient {

0 commit comments

Comments
 (0)