Skip to content

Commit 10f0e36

Browse files
committed
cache discovered jar path
1 parent 14c667d commit 10f0e36

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

Diff for: instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/JarFileDetector.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,15 @@ public JarFileDetector() {
5151

5252
@Nullable
5353
Path getJarPath() {
54-
Path jarPath = getJarPathFromProcessHandle();
55-
if (jarPath != null) {
56-
return jarPath;
57-
}
58-
return getJarPathFromSunCommandLine();
54+
return ResourceDiscoveryCache.get(
55+
"jarPath",
56+
() -> {
57+
Path jarPath = getJarPathFromProcessHandle();
58+
if (jarPath != null) {
59+
return jarPath;
60+
}
61+
return getJarPathFromSunCommandLine();
62+
});
5963
}
6064

6165
Optional<Manifest> getManifestFromJarFile() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.resources;
7+
8+
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.function.Supplier;
10+
11+
public class ResourceDiscoveryCache {
12+
private static final ConcurrentHashMap<String, Object> cache = new ConcurrentHashMap<>();
13+
14+
private ResourceDiscoveryCache() {}
15+
16+
// visible for testing
17+
public static void resetForTest() {
18+
cache.clear();
19+
}
20+
21+
@SuppressWarnings("unchecked")
22+
public static <T> T get(String key, Supplier<T> supplier) {
23+
return (T) cache.computeIfAbsent(key, k -> supplier.get());
24+
}
25+
}

Diff for: instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetectorTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.function.Function;
1818
import java.util.function.Predicate;
1919
import java.util.stream.Stream;
20+
import org.junit.jupiter.api.BeforeEach;
2021
import org.junit.jupiter.api.Test;
2122
import org.junit.jupiter.api.extension.ExtendWith;
2223
import org.junit.jupiter.api.extension.ExtensionContext;
@@ -33,6 +34,11 @@ class JarServiceNameDetectorTest {
3334

3435
@Mock ConfigProperties config;
3536

37+
@BeforeEach
38+
void setUp() {
39+
ResourceDiscoveryCache.resetForTest();
40+
}
41+
3642
@Test
3743
void createResource_empty() {
3844
String[] processArgs = new String[0];

Diff for: instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/ManifestResourceProviderTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@
1919
import java.util.jar.Manifest;
2020
import java.util.stream.Collectors;
2121
import java.util.stream.Stream;
22+
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.DynamicTest;
2324
import org.junit.jupiter.api.TestFactory;
2425

2526
class ManifestResourceProviderTest {
2627

28+
@BeforeEach
29+
void setUp() {
30+
ResourceDiscoveryCache.resetForTest();
31+
}
32+
2733
private static class TestCase {
2834
private final String name;
2935
private final String expectedName;

0 commit comments

Comments
 (0)