Skip to content

Commit 6e851f8

Browse files
authored
add hostname to attributes (#390)
1 parent 21ebff7 commit 6e851f8

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/CgroupsReader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525

2626
public class CgroupsReader {
2727

28-
private static final Logger log =
29-
LoggerFactory.getLogger(HypertraceResourceProvider.class.getName());
28+
private static final Logger log = LoggerFactory.getLogger(CgroupsReader.class.getName());
3029

3130
private static final String DEFAULT_CGROUPS_PATH = "/proc/self/cgroup";
3231
private static final int CONTAINER_ID_LENGTH = 64;

otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/processor/AddTagsSpanProcessor.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,38 @@
2323
import io.opentelemetry.sdk.trace.SpanProcessor;
2424
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
2525
import org.hypertrace.agent.otel.extensions.CgroupsReader;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2628

2729
public class AddTagsSpanProcessor implements SpanProcessor {
2830

31+
private static final Logger log = LoggerFactory.getLogger(AddTagsSpanProcessor.class.getName());
32+
2933
// initialize at startup because the processor is executed for every span.
30-
private String containerId;
34+
private final String containerId;
35+
private final String hostName;
3136

3237
/** Note - the container id is not available using this technique if cgroup2 is installed. */
3338
public AddTagsSpanProcessor() {
3439
CgroupsReader cgroupsReader = new CgroupsReader();
3540
containerId = cgroupsReader.readContainerId();
41+
String hostnameEnv = "";
42+
try {
43+
hostnameEnv = System.getenv("HOSTNAME");
44+
} catch (SecurityException e) {
45+
log.error("could not get hostname", e);
46+
}
47+
hostName = hostnameEnv;
3648
}
3749

3850
@Override
3951
public void onStart(Context parentContext, ReadWriteSpan span) {
4052
if (containerId != null && !containerId.isEmpty()) {
4153
span.setAttribute(ResourceAttributes.CONTAINER_ID, containerId);
4254
}
55+
if (hostName != null && !hostName.isEmpty()) {
56+
span.setAttribute(ResourceAttributes.HOST_NAME, hostName);
57+
}
4358
}
4459

4560
@Override

0 commit comments

Comments
 (0)