Skip to content

Commit

Permalink
add hostname to attributes (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
shashank11p authored Oct 17, 2023
1 parent 21ebff7 commit 6e851f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,38 @@
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
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
Expand Down

0 comments on commit 6e851f8

Please sign in to comment.