From c5d05628c7d0b046bf360bd042e127bdbbd8d31e Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Thu, 14 Mar 2024 14:39:15 +0200 Subject: [PATCH] simplify main jar holder --- .../resources/JarServiceNameDetector.java | 15 +++++++--- .../resources/MainJarPathHolder.java | 29 ++++--------------- .../resources/ManifestResourceProvider.java | 16 ++++++---- .../resources/JarServiceNameDetectorTest.java | 6 ---- .../ManifestResourceProviderTest.java | 6 ---- 5 files changed, 28 insertions(+), 44 deletions(-) diff --git a/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetector.java b/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetector.java index 3eb9425b3f34..ab855705e272 100644 --- a/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetector.java +++ b/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetector.java @@ -16,6 +16,8 @@ import io.opentelemetry.semconv.ResourceAttributes; import java.nio.file.Path; import java.util.Map; +import java.util.Optional; +import java.util.function.Supplier; import java.util.logging.Logger; /** @@ -26,21 +28,26 @@ public final class JarServiceNameDetector implements ConditionalResourceProvider private static final Logger logger = Logger.getLogger(JarServiceNameDetector.class.getName()); - private final MainJarPathFinder jarPathFinder; + private final Supplier> jarPathSupplier; @SuppressWarnings("unused") // SPI public JarServiceNameDetector() { - this(new MainJarPathFinder()); + this(MainJarPathHolder::getJarPath); + } + + private JarServiceNameDetector(Supplier> jarPathSupplier) { + this.jarPathSupplier = jarPathSupplier; } // visible for tests JarServiceNameDetector(MainJarPathFinder jarPathFinder) { - this.jarPathFinder = jarPathFinder; + this(() -> Optional.ofNullable(jarPathFinder.detectJarPath())); } @Override public Resource createResource(ConfigProperties config) { - return MainJarPathHolder.getJarPath(jarPathFinder) + return jarPathSupplier + .get() .map( jarPath -> { String serviceName = getServiceName(jarPath); diff --git a/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/MainJarPathHolder.java b/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/MainJarPathHolder.java index 1f86971fac7a..c6948d3eca39 100644 --- a/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/MainJarPathHolder.java +++ b/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/MainJarPathHolder.java @@ -8,30 +8,13 @@ import java.nio.file.Path; import java.util.Optional; -public class MainJarPathHolder { +class MainJarPathHolder { + private static final Optional jarPath = + Optional.ofNullable(new MainJarPathFinder().detectJarPath()); - private MainJarPathHolder() {} - - // visible for testing - static void resetForTest() { - detectionResult = Optional.empty(); - } - - private static class DetectionResult { - private final Optional jarPath; - - private DetectionResult(Optional jarPath) { - this.jarPath = jarPath; - } + static Optional getJarPath() { + return jarPath; } - private static Optional detectionResult = Optional.empty(); - - static Optional getJarPath(MainJarPathFinder finder) { - if (!detectionResult.isPresent()) { - detectionResult = - Optional.of(new DetectionResult(Optional.ofNullable(finder.detectJarPath()))); - } - return detectionResult.get().jarPath; - } + private MainJarPathHolder() {} } diff --git a/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ManifestResourceProvider.java b/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ManifestResourceProvider.java index 91001820d223..335583d12445 100644 --- a/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ManifestResourceProvider.java +++ b/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ManifestResourceProvider.java @@ -14,6 +14,7 @@ import java.nio.file.Path; import java.util.Optional; import java.util.function.Function; +import java.util.function.Supplier; import java.util.jar.JarFile; import java.util.jar.Manifest; import java.util.logging.Logger; @@ -29,17 +30,16 @@ public final class ManifestResourceProvider extends AttributeResourceProvider> manifestReader) { + private ManifestResourceProvider( + Supplier> jarPathSupplier, Function> manifestReader) { super( new AttributeProvider() { @Override public Optional readData() { - return MainJarPathHolder.getJarPath(jarPathFinder).flatMap(manifestReader); + return jarPathSupplier.get().flatMap(manifestReader); } @Override @@ -63,6 +63,12 @@ public void registerAttributes(Builder builder) { }); } + // Visible for testing + ManifestResourceProvider( + MainJarPathFinder jarPathFinder, Function> manifestReader) { + this(() -> Optional.ofNullable(jarPathFinder.detectJarPath()), manifestReader); + } + private static Optional readManifest(Path jarPath) { try (JarFile jarFile = new JarFile(jarPath.toFile(), false)) { return Optional.of(jarFile.getManifest()); diff --git a/instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetectorTest.java b/instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetectorTest.java index bf33f0348009..bb4d6c9d16ae 100644 --- a/instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetectorTest.java +++ b/instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetectorTest.java @@ -16,7 +16,6 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Stream; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtensionContext; @@ -33,11 +32,6 @@ class JarServiceNameDetectorTest { @Mock ConfigProperties config; - @BeforeEach - void setUp() { - MainJarPathHolder.resetForTest(); - } - @Test void createResource_empty() { String[] processArgs = new String[0]; diff --git a/instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/ManifestResourceProviderTest.java b/instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/ManifestResourceProviderTest.java index fea8b1219b74..d61b53a43e50 100644 --- a/instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/ManifestResourceProviderTest.java +++ b/instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/ManifestResourceProviderTest.java @@ -20,17 +20,11 @@ import java.util.jar.Manifest; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; class ManifestResourceProviderTest { - @BeforeEach - void setUp() { - MainJarPathHolder.resetForTest(); - } - private static class TestCase { private final String name; private final String expectedName;