diff --git a/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ContainerResource.java b/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ContainerResource.java index df988e7e0ac2..33d7a0da4d71 100644 --- a/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ContainerResource.java +++ b/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ContainerResource.java @@ -12,12 +12,11 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Objects; import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Stream; -import javax.annotation.Nullable; +import javax.annotation.Nonnull; import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement; /** Factory for {@link Resource} retrieving Container ID information. */ @@ -35,13 +34,11 @@ private static Resource buildSingleton(String uniqueHostNameFileName) { // package private for testing static Resource buildResource(Path path) { - String containerId = extractContainerId(path); - - if (containerId == null || containerId.isEmpty()) { - return Resource.empty(); - } else { - return Resource.create(Attributes.of(ResourceAttributes.CONTAINER_ID, containerId)); - } + return extractContainerId(path) + .map( + containerId -> + Resource.create(Attributes.of(ResourceAttributes.CONTAINER_ID, containerId))) + .orElseGet(Resource::empty); } /** Returns resource with container information. */ @@ -57,33 +54,30 @@ public static Resource get() { * @return containerId */ @IgnoreJRERequirement - @Nullable - private static String extractContainerId(Path cgroupFilePath) { + @Nonnull + private static Optional extractContainerId(Path cgroupFilePath) { if (!Files.exists(cgroupFilePath) || !Files.isReadable(cgroupFilePath)) { - return null; + return Optional.empty(); } try (Stream lines = Files.lines(cgroupFilePath)) { - Optional value = - lines - .filter(line -> !line.isEmpty()) - .map(ContainerResource::getIdFromLine) - .filter(Objects::nonNull) - .findFirst(); - if (value.isPresent()) { - return value.get(); - } + return lines + .filter(line -> !line.isEmpty()) + .map(ContainerResource::getIdFromLine) + .filter(Optional::isPresent) + .findFirst() + .orElse(Optional.empty()); } catch (Exception e) { logger.log(Level.WARNING, "Unable to read file", e); } - return null; + return Optional.empty(); } - @Nullable - private static String getIdFromLine(String line) { + @Nonnull + private static Optional getIdFromLine(String line) { // This cgroup output line should have the container id in it int lastSlashIdx = line.lastIndexOf('/'); if (lastSlashIdx < 0) { - return null; + return Optional.empty(); } String containerId; @@ -105,16 +99,16 @@ private static String getIdFromLine(String line) { endIdx = lastSection.length(); } if (startIdx > endIdx) { - return null; + return Optional.empty(); } containerId = lastSection.substring(startIdx, endIdx); } if (OtelEncodingUtils.isValidBase16String(containerId) && !containerId.isEmpty()) { - return containerId; + return Optional.of(containerId); } else { - return null; + return Optional.empty(); } }