Change container info to a singleton ContainerInfo
DDTraceOTInfo wasn't the best place for container information.  Changed ContainerInfo.java to have a static instance
MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/ContainerInfo.java
dd-trace-ot/src/main/java/datadog/opentracing/ContainerInfo.java
@@ -12,6 +12,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.Getter;
import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
/**
* Parses container information from /proc/self/cgroup. Implementation based largely on
@@ -19,6 +20,7 @@ import lombok.Setter;
*/
@Getter
@Setter
+@Slf4j
public class ContainerInfo {
private static final Path CGROUP_DEFAULT_PROCFILE = Paths.get("/proc/self/cgroup");
private static final String UUID_REGEX =
@@ -30,9 +32,24 @@ public class ContainerInfo {
private static final Pattern CONTAINER_PATTERN =
Pattern.compile("(?:.+)?(" + UUID_REGEX + "|" + CONTAINER_REGEX + ")(?:.scope)?$");
+ private static final ContainerInfo INSTANCE;
+
public String containerId;
public String podId;
- public List<CGroupInfo> cGroups;
+ public List<CGroupInfo> cGroups = new ArrayList<>();
+
+ static {
+ ContainerInfo containerInfo = new ContainerInfo();
+ if (ContainerInfo.isRunningInContainer()) {
+ try {
+ containerInfo = ContainerInfo.fromDefaultProcFile();
+ } catch (final IOException | ParseException e) {
+ log.error("Unable to parse proc file");
+ }
+ }
+
+ INSTANCE = containerInfo;
+ }
@Getter
@Setter
@@ -44,6 +61,10 @@ public class ContainerInfo {
public String podId;
}
+ public static ContainerInfo get() {
+ return INSTANCE;
+ }
+
public static boolean isRunningInContainer() {
return Files.isReadable(CGROUP_DEFAULT_PROCFILE);
}
@@ -57,11 +78,10 @@ public class ContainerInfo {
final ContainerInfo containerInfo = new ContainerInfo();
final String[] lines = cgroupsContent.split("\n");
- final List<CGroupInfo> parsedCGroups = new ArrayList<>();
for (final String line : lines) {
final CGroupInfo cGroupInfo = parseLine(line);
- parsedCGroups.add(cGroupInfo);
+ containerInfo.getCGroups().add(cGroupInfo);
if (cGroupInfo.getPodId() != null) {
containerInfo.setPodId(cGroupInfo.getPodId());
@@ -72,8 +92,6 @@ public class ContainerInfo {
}
}
- containerInfo.setCGroups(parsedCGroups);
-
return containerInfo;
}

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
@@ -15,8 +13,6 @@ public class DDTraceOTInfo {
public static final String VERSION;
- public static final String CONTAINER_ID;
-
static {
String v;
try {
@@ -34,18 +30,6 @@ 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
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
+import datadog.opentracing.ContainerInfo;
import datadog.opentracing.DDSpan;
import datadog.opentracing.DDTraceOTInfo;
import datadog.trace.common.writer.unixdomainsockets.UnixDomainSocketFactory;
@@ -271,10 +272,11 @@ public class DDApi {
.addHeader(DATADOG_META_LANG_INTERPRETER_VENDOR, DDTraceOTInfo.JAVA_VM_VENDOR)
.addHeader(DATADOG_META_TRACER_VERSION, DDTraceOTInfo.VERSION);
- if (DDTraceOTInfo.CONTAINER_ID == null) {
+ final String containerId = ContainerInfo.get().getContainerId();
+ if (containerId == null) {
return builder;
} else {
- return builder.addHeader(DATADOG_CONTAINER_ID, DDTraceOTInfo.CONTAINER_ID);
+ return builder.addHeader(DATADOG_CONTAINER_ID, containerId);
}
}

Change container info to a singleton ContainerInfo

DDTraceOTInfo wasn't the best place for container information.  Changed ContainerInfo.java to have a static instance