From d9ba6c46afc6634b1d7bc396d2801bd470c9cc69 Mon Sep 17 00:00:00 2001 From: Shashank Patidar Date: Thu, 12 Oct 2023 16:54:14 +0530 Subject: [PATCH] add hostname to attributes --- .../agent/otel/extensions/CgroupsReader.java | 3 +-- .../processor/AddTagsSpanProcessor.java | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) 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 afa20bcbd..df6ffd996 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 c78a1ff19..06356b6ee 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