Add container id headers to request builder
MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/ContainerInfo.java
dd-trace-ot/src/main/java/datadog/opentracing/ContainerInfo.java
package datadog.opentracing;
-import lombok.Data;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -11,6 +10,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import lombok.Data;
/**
* Parses container information from /proc/self/cgroup. Implementation based largely on
@@ -23,9 +23,10 @@ public class ContainerInfo {
"[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}";
private static final String CONTAINER_REGEX = "[0-9a-f]{64}";
private static final Pattern LINE_PATTERN = Pattern.compile("(\\d+):([^:]*):(.+)$");
- private static final Pattern POD_PATTERN = Pattern.compile("pod(" + UUID_REGEX + ")(?:.slice)?$");
+ private static final Pattern POD_PATTERN =
+ Pattern.compile("(?:.+)?pod(" + UUID_REGEX + ")(?:.slice)?$");
private static final Pattern CONTAINER_PATTERN =
- Pattern.compile("(" + UUID_REGEX + "|" + CONTAINER_REGEX + ")(?:.scope)?$");
+ Pattern.compile("(?:.+)?(" + UUID_REGEX + "|" + CONTAINER_REGEX + ")(?:.scope)?$");
public String containerId;
public String podId;

MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/DDTraceOTInfo.java
dd-trace-ot/src/main/java/datadog/opentracing/DDTraceOTInfo.java
package datadog.opentracing;
import java.io.BufferedReader;
+import java.io.IOException;
import java.io.InputStreamReader;
+import java.text.ParseException;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@@ -13,6 +15,8 @@ public class DDTraceOTInfo {
public static final String VERSION;
+ public static final String CONTAINER_ID;
+
static {
String v;
try {
@@ -30,6 +34,18 @@ public class DDTraceOTInfo {
}
VERSION = v;
log.info("dd-trace - version: {}", v);
+
+ ContainerInfo containerInfo = null;
+
+ if (ContainerInfo.isRunningInContainer()) {
+ try {
+ containerInfo = ContainerInfo.fromDefaultProcFile();
+ } catch (final IOException | ParseException e) {
+ log.error("Unable to parse proc file");
+ }
+ }
+
+ CONTAINER_ID = containerInfo == null ? null : containerInfo.getContainerId();
}
public static void main(final String... args) {

MODIFY
dd-trace-ot/src/main/java/datadog/trace/common/writer/DDApi.java
dd-trace-ot/src/main/java/datadog/trace/common/writer/DDApi.java
@@ -35,6 +35,7 @@ public class DDApi {
private static final String DATADOG_META_LANG_INTERPRETER_VENDOR =
"Datadog-Meta-Lang-Interpreter-Vendor";
private static final String DATADOG_META_TRACER_VERSION = "Datadog-Meta-Tracer-Version";
+ private static final String DATADOG_CONTAINER_ID = "Datadog-Container-ID";
private static final String X_DATADOG_TRACE_COUNT = "X-Datadog-Trace-Count";
private static final int HTTP_TIMEOUT = 1; // 1 second for conenct/read/write operations
@@ -261,13 +262,20 @@ public class DDApi {
}
private static Request.Builder prepareRequest(final HttpUrl url) {
- return new Request.Builder()
+ final Request.Builder builder =
+ new Request.Builder()
.url(url)
.addHeader(DATADOG_META_LANG, "java")
.addHeader(DATADOG_META_LANG_VERSION, DDTraceOTInfo.JAVA_VERSION)
.addHeader(DATADOG_META_LANG_INTERPRETER, DDTraceOTInfo.JAVA_VM_NAME)
.addHeader(DATADOG_META_LANG_INTERPRETER_VENDOR, DDTraceOTInfo.JAVA_VM_VENDOR)
.addHeader(DATADOG_META_TRACER_VERSION, DDTraceOTInfo.VERSION);
+
+ if (DDTraceOTInfo.CONTAINER_ID == null) {
+ return builder;
+ } else {
+ return builder.addHeader(DATADOG_CONTAINER_ID, DDTraceOTInfo.CONTAINER_ID);
+ }
}
@Override

MODIFY
dd-trace-ot/src/test/groovy/datadog/opentracing/ContainerInfoTest.groovy
dd-trace-ot/src/test/groovy/datadog/opentracing/ContainerInfoTest.groovy
@@ -46,7 +46,7 @@ class ContainerInfoTest extends Specification {
6 | ["cpuset"] | "/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3d274242-8ee0-11e9-a8a6-1e68d864ef1a" | "6:cpuset:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1"
5 | ["devices"] | "/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3d274242-8ee0-11e9-a8a6-1e68d864ef1a" | "5:devices:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1"
4 | ["freezer"] | "/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3d274242-8ee0-11e9-a8a6-1e68d864ef1a" | "4:freezer:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1"
- 3 | ["net_cls", "net_prop"] | "/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3d274242-8ee0-11e9-a8a6-1e68d864ef1a" | "3:net_cls,net_prio:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1"
+ 3 | ["net_cls", "net_prio"] | "/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3d274242-8ee0-11e9-a8a6-1e68d864ef1a" | "3:net_cls,net_prio:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1"
2 | ["hugetlb"] | "/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3d274242-8ee0-11e9-a8a6-1e68d864ef1a" | "2:hugetlb:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1"
1 | ["name=systemd"] | "/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1" | "3d274242-8ee0-11e9-a8a6-1e68d864ef1a" | "1:name=systemd:/kubepods/besteffort/pod3d274242-8ee0-11e9-a8a6-1e68d864ef1a/3e74d3fd9db4c9dd921ae05c2502fb984d0cde1b36e581b13f79c639da4518a1"

Add container id headers to request builder