Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hostname to attributes #390

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading