diff --git a/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/CgroupsReader.java b/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/CgroupsReader.java index afa20bcb..df6ffd99 100644 --- a/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/CgroupsReader.java +++ b/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/CgroupsReader.java @@ -25,8 +25,7 @@ public class CgroupsReader { - private static final Logger log = - LoggerFactory.getLogger(HypertraceResourceProvider.class.getName()); + private static final Logger log = LoggerFactory.getLogger(CgroupsReader.class.getName()); private static final String DEFAULT_CGROUPS_PATH = "/proc/self/cgroup"; private static final int CONTAINER_ID_LENGTH = 64; diff --git a/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/processor/AddTagsSpanProcessor.java b/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/processor/AddTagsSpanProcessor.java index c78a1ff1..06356b6e 100644 --- a/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/processor/AddTagsSpanProcessor.java +++ b/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/processor/AddTagsSpanProcessor.java @@ -23,16 +23,28 @@ import io.opentelemetry.sdk.trace.SpanProcessor; import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; import org.hypertrace.agent.otel.extensions.CgroupsReader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class AddTagsSpanProcessor implements SpanProcessor { + private static final Logger log = LoggerFactory.getLogger(AddTagsSpanProcessor.class.getName()); + // initialize at startup because the processor is executed for every span. - private String containerId; + private final String containerId; + private final String hostName; /** Note - the container id is not available using this technique if cgroup2 is installed. */ public AddTagsSpanProcessor() { CgroupsReader cgroupsReader = new CgroupsReader(); containerId = cgroupsReader.readContainerId(); + String hostnameEnv = ""; + try { + hostnameEnv = System.getenv("HOSTNAME"); + } catch (SecurityException e) { + log.error("could not get hostname", e); + } + hostName = hostnameEnv; } @Override @@ -40,6 +52,9 @@ public void onStart(Context parentContext, ReadWriteSpan span) { if (containerId != null && !containerId.isEmpty()) { span.setAttribute(ResourceAttributes.CONTAINER_ID, containerId); } + if (hostName != null && !hostName.isEmpty()) { + span.setAttribute(ResourceAttributes.HOST_NAME, hostName); + } } @Override