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

Fix "connector.id" resource attribute issue #17

Merged
merged 1 commit into from
Mar 19, 2024
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
4 changes: 2 additions & 2 deletions host/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.instana.dc"
version = "0.1.0"
version = "0.1.1"

repositories {
mavenCentral()
Expand All @@ -20,7 +20,7 @@ dependencies {
implementation("io.opentelemetry:opentelemetry-exporter-sender-okhttp:1.34.1")
implementation("io.opentelemetry.semconv:opentelemetry-semconv:1.23.1-alpha")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.16.0-rc1")
implementation(files("libs/otel-dc-0.9.5.jar"))
implementation(files("libs/otel-dc-0.9.6.jar"))

testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion internal/otel-dc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.instana.dc"
version = "0.9.5"
version = "0.9.6"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -17,42 +18,58 @@

import static java.util.Optional.empty;

/** Utility for extracting the container ID from runtimes inside cgroup v2 containers. */
/**
* Utility for extracting the container ID from runtimes inside cgroup v2 containers.
*/
class CgroupV2ContainerIdExtractor {

private static final Logger logger =
Logger.getLogger(CgroupV2ContainerIdExtractor.class.getName());

static final Path V2_CGROUP_PATH = Paths.get("/proc/self/mountinfo");
private static final Pattern CONTAINER_ID_RE = Pattern.compile("^[0-9a-f]{64}$");
private static final Logger logger =
Logger.getLogger(CgroupV2ContainerIdExtractor.class.getName());

private final ContainerResource.Filesystem filesystem;
static final Path V2_CGROUP_PATH = Paths.get("/proc/self/mountinfo");
private static final Pattern CONTAINER_ID_RE = Pattern.compile("^[0-9a-f]{64}$");
private static final Pattern CRI_CONTAINER_ID_RE = Pattern.compile("cri-containerd:[0-9a-f]{64}");

CgroupV2ContainerIdExtractor() {
this(ContainerResource.FILESYSTEM_INSTANCE);
}
private final ContainerResource.Filesystem filesystem;

// Exists for testing
CgroupV2ContainerIdExtractor(ContainerResource.Filesystem filesystem) {
this.filesystem = filesystem;
}
CgroupV2ContainerIdExtractor() {
this(ContainerResource.FILESYSTEM_INSTANCE);
}

Optional<String> extractContainerId() {
if (!filesystem.isReadable(V2_CGROUP_PATH)) {
return empty();
// Exists for testing
CgroupV2ContainerIdExtractor(ContainerResource.Filesystem filesystem) {
this.filesystem = filesystem;
}
try {
return filesystem
.lines(V2_CGROUP_PATH)
.filter(line -> line.contains("hostname"))
.flatMap(line -> Stream.of(line.split("/")))
.map(CONTAINER_ID_RE::matcher)
.filter(Matcher::matches)
.findFirst()
.map(matcher -> matcher.group(0));
} catch (IOException e) {
logger.log(Level.WARNING, "Unable to read v2 cgroup path", e);

Optional<String> extractContainerId() {
if (!filesystem.isReadable(V2_CGROUP_PATH)) {
return empty();
}

List<String> fileAsList;
try {
fileAsList = filesystem.lineList(V2_CGROUP_PATH);
} catch (IOException e) {
logger.log(Level.WARNING, "Unable to read v2 cgroup path", e);
return empty();
}

Optional<String> optCid =
fileAsList.stream()
.filter(line -> line.contains("/containers/"))
.flatMap(line -> Stream.of(line.split("/")))
.map(CONTAINER_ID_RE::matcher)
.filter(Matcher::matches)
.reduce((first, second) -> second)
.map(matcher -> matcher.group(0));
if (optCid.isPresent()) {
return optCid;
}
return fileAsList.stream()
.filter(line -> line.contains("cri-containerd:"))
.map(CRI_CONTAINER_ID_RE::matcher)
.filter(Matcher::find)
.findFirst()
.map(matcher -> matcher.group(0).substring(15));
}
return empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.opentelemetry.semconv.ResourceAttributes.CONTAINER_ID;
Expand All @@ -23,58 +25,66 @@
*/
public final class ContainerResource {

static final Filesystem FILESYSTEM_INSTANCE = new Filesystem();
private static final Resource INSTANCE = buildSingleton();

private static Resource buildSingleton() {
// can't initialize this statically without running afoul of animalSniffer on paths
return new ContainerResource().buildResource();
}

private final CgroupV1ContainerIdExtractor v1Extractor;
private final CgroupV2ContainerIdExtractor v2Extractor;

private ContainerResource() {
this(new CgroupV1ContainerIdExtractor(), new CgroupV2ContainerIdExtractor());
}

// Visible for testing
ContainerResource(
CgroupV1ContainerIdExtractor v1Extractor, CgroupV2ContainerIdExtractor v2Extractor) {
this.v1Extractor = v1Extractor;
this.v2Extractor = v2Extractor;
}

// Visible for testing
Resource buildResource() {
return getContainerId()
.map(id -> Resource.create(Attributes.of(CONTAINER_ID, id)))
.orElseGet(Resource::empty);
}

private Optional<String> getContainerId() {
Optional<String> v1Result = v1Extractor.extractContainerId();
if (v1Result.isPresent()) {
return v1Result;
static final Filesystem FILESYSTEM_INSTANCE = new Filesystem();
private static final Resource INSTANCE = buildSingleton();

private static Resource buildSingleton() {
// can't initialize this statically without running afoul of animalSniffer on paths
return new ContainerResource().buildResource();
}

private final CgroupV1ContainerIdExtractor v1Extractor;
private final CgroupV2ContainerIdExtractor v2Extractor;

private ContainerResource() {
this(new CgroupV1ContainerIdExtractor(), new CgroupV2ContainerIdExtractor());
}
return v2Extractor.extractContainerId();
}

/** Returns resource with container information. */
public static Resource get() {
return INSTANCE;
}
// Visible for testing
ContainerResource(
CgroupV1ContainerIdExtractor v1Extractor, CgroupV2ContainerIdExtractor v2Extractor) {
this.v1Extractor = v1Extractor;
this.v2Extractor = v2Extractor;
}

// Exists for testing
static class Filesystem {
// Visible for testing
Resource buildResource() {
return getContainerId()
.map(id -> Resource.create(Attributes.of(CONTAINER_ID, id)))
.orElseGet(Resource::empty);
}

boolean isReadable(Path path) {
return Files.isReadable(path);
private Optional<String> getContainerId() {
Optional<String> v1Result = v1Extractor.extractContainerId();
if (v1Result.isPresent()) {
return v1Result;
}
return v2Extractor.extractContainerId();
}

@MustBeClosed
Stream<String> lines(Path path) throws IOException {
return Files.lines(path);
/**
* Returns resource with container information.
*/
public static Resource get() {
return INSTANCE;
}

// Exists for testing
static class Filesystem {

boolean isReadable(Path path) {
return Files.isReadable(path);
}

@MustBeClosed
Stream<String> lines(Path path) throws IOException {
return Files.lines(path);
}

List<String> lineList(Path path) throws IOException {
try (Stream<String> lines = lines(path)) {
return lines.collect(Collectors.toList());
}
}
}
}
}
4 changes: 2 additions & 2 deletions rdb/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.instana.dc"
version = "0.5.0"
version = "0.5.1"

repositories {
mavenCentral()
Expand All @@ -20,7 +20,7 @@ dependencies {
implementation("io.opentelemetry:opentelemetry-exporter-sender-okhttp:1.34.1")
implementation("io.opentelemetry.semconv:opentelemetry-semconv:1.23.1-alpha")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.16.0-rc1")
implementation(files("libs/otel-dc-0.9.5.jar"))
implementation(files("libs/otel-dc-0.9.6.jar"))
implementation(files("libs/ngdbc-2.4.64.jar"))
implementation("org.apache.commons:commons-dbcp2:2.11.0")

Expand Down
Binary file not shown.