Skip to content

Commit 923e97a

Browse files
committed
remove ResourceDiscoveryCache
1 parent 3ac7eac commit 923e97a

File tree

6 files changed

+64
-90
lines changed

6 files changed

+64
-90
lines changed

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/JarPathFinder.java

+16-41
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55

66
package io.opentelemetry.instrumentation.resources;
77

8-
import static java.util.logging.Level.WARNING;
9-
10-
import java.io.InputStream;
11-
import java.net.URL;
128
import java.nio.file.Files;
139
import java.nio.file.InvalidPathException;
1410
import java.nio.file.Path;
@@ -17,69 +13,48 @@
1713
import java.util.function.Function;
1814
import java.util.function.Predicate;
1915
import java.util.function.Supplier;
20-
import java.util.jar.Manifest;
21-
import java.util.logging.Logger;
2216
import javax.annotation.Nullable;
2317

2418
class JarPathFinder {
2519
private final Supplier<String[]> getProcessHandleArguments;
2620
private final Function<String, String> getSystemProperty;
2721
private final Predicate<Path> fileExists;
28-
private final Function<Path, Optional<Manifest>> manifestReader;
2922

30-
private static final Logger logger = Logger.getLogger(JarPathFinder.class.getName());
23+
private static Optional<Optional<Path>> jarPath = Optional.empty();
3124

3225
public JarPathFinder() {
33-
this(
34-
ProcessArguments::getProcessArguments,
35-
System::getProperty,
36-
Files::isRegularFile,
37-
JarPathFinder::readManifest);
26+
this(ProcessArguments::getProcessArguments, System::getProperty, Files::isRegularFile);
3827
}
3928

4029
// visible for tests
4130
JarPathFinder(
4231
Supplier<String[]> getProcessHandleArguments,
4332
Function<String, String> getSystemProperty,
44-
Predicate<Path> fileExists,
45-
Function<Path, Optional<Manifest>> manifestReader) {
33+
Predicate<Path> fileExists) {
4634
this.getProcessHandleArguments = getProcessHandleArguments;
4735
this.getSystemProperty = getSystemProperty;
4836
this.fileExists = fileExists;
49-
this.manifestReader = manifestReader;
5037
}
5138

52-
@Nullable
53-
Path getJarPath() {
54-
return ResourceDiscoveryCache.get(
55-
"jarPath",
56-
() -> {
57-
Path jarPath = getJarPathFromProcessHandle();
58-
if (jarPath != null) {
59-
return jarPath;
60-
}
61-
return getJarPathFromSunCommandLine();
62-
});
39+
// visible for testing
40+
static void resetForTest() {
41+
jarPath = Optional.empty();
6342
}
6443

65-
Optional<Manifest> getManifestFromJarFile() {
66-
Path jarPath = getJarPath();
67-
if (jarPath == null) {
68-
return Optional.empty();
44+
Optional<Path> getJarPath() {
45+
if (jarPath.isPresent()) {
46+
return jarPath.get();
6947
}
70-
return manifestReader.apply(jarPath);
48+
jarPath = Optional.of(Optional.ofNullable(readJarPath()));
49+
return jarPath.get();
7150
}
7251

73-
private static Optional<Manifest> readManifest(Path jarPath) {
74-
try (InputStream s =
75-
new URL(String.format("jar:%s!/META-INF/MANIFEST.MF", jarPath.toUri())).openStream()) {
76-
Manifest manifest = new Manifest();
77-
manifest.read(s);
78-
return Optional.of(manifest);
79-
} catch (Exception e) {
80-
logger.log(WARNING, "Error reading manifest", e);
81-
return Optional.empty();
52+
private Path readJarPath() {
53+
Path jarPath = getJarPathFromProcessHandle();
54+
if (jarPath != null) {
55+
return jarPath;
8256
}
57+
return getJarPathFromSunCommandLine();
8358
}
8459

8560
@Nullable

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetector.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ public JarServiceNameDetector() {
4040

4141
@Override
4242
public Resource createResource(ConfigProperties config) {
43-
Path jarPath = jarPathFinder.getJarPath();
44-
if (jarPath == null) {
45-
return Resource.empty();
46-
}
47-
String serviceName = getServiceName(jarPath);
48-
logger.log(FINE, "Auto-detected service name from the jar file name: {0}", serviceName);
49-
return Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, serviceName));
43+
return jarPathFinder
44+
.getJarPath()
45+
.map(
46+
jarPath -> {
47+
String serviceName = getServiceName(jarPath);
48+
logger.log(
49+
FINE, "Auto-detected service name from the jar file name: {0}", serviceName);
50+
return Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, serviceName));
51+
})
52+
.orElseGet(Resource::empty);
5053
}
5154

5255
@Override

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ManifestResourceProvider.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55

66
package io.opentelemetry.instrumentation.resources;
77

8+
import static java.util.logging.Level.WARNING;
9+
810
import com.google.auto.service.AutoService;
911
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
1012
import io.opentelemetry.semconv.ResourceAttributes;
13+
import java.io.InputStream;
14+
import java.net.URL;
15+
import java.nio.file.Path;
1116
import java.util.Optional;
17+
import java.util.function.Function;
1218
import java.util.jar.Manifest;
19+
import java.util.logging.Logger;
1320

1421
/**
1522
* A {@link ResourceProvider} that will attempt to detect the <code>service.name</code> and <code>
@@ -18,18 +25,21 @@
1825
@AutoService(ResourceProvider.class)
1926
public final class ManifestResourceProvider extends AttributeResourceProvider<Manifest> {
2027

28+
private static final Logger logger = Logger.getLogger(ManifestResourceProvider.class.getName());
29+
2130
@SuppressWarnings("unused") // SPI
2231
public ManifestResourceProvider() {
23-
this(new JarPathFinder());
32+
this(new JarPathFinder(), ManifestResourceProvider::readManifest);
2433
}
2534

2635
// Visible for testing
27-
ManifestResourceProvider(JarPathFinder jarPathFinder) {
36+
ManifestResourceProvider(
37+
JarPathFinder jarPathFinder, Function<Path, Optional<Manifest>> manifestReader) {
2838
super(
2939
new AttributeProvider<Manifest>() {
3040
@Override
3141
public Optional<Manifest> readData() {
32-
return jarPathFinder.getManifestFromJarFile();
42+
return jarPathFinder.getJarPath().flatMap(manifestReader);
3343
}
3444

3545
@Override
@@ -53,6 +63,18 @@ public void registerAttributes(Builder<Manifest> builder) {
5363
});
5464
}
5565

66+
private static Optional<Manifest> readManifest(Path jarPath) {
67+
try (InputStream s =
68+
new URL(String.format("jar:%s!/META-INF/MANIFEST.MF", jarPath.toUri())).openStream()) {
69+
Manifest manifest = new Manifest();
70+
manifest.read(s);
71+
return Optional.of(manifest);
72+
} catch (Exception e) {
73+
logger.log(WARNING, "Error reading manifest", e);
74+
return Optional.empty();
75+
}
76+
}
77+
5678
@Override
5779
public int order() {
5880
// make it run later than SpringBootServiceNameDetector

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ResourceDiscoveryCache.java

-25
This file was deleted.

instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetectorTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import io.opentelemetry.semconv.ResourceAttributes;
1414
import java.nio.file.Path;
1515
import java.nio.file.Paths;
16-
import java.util.Optional;
1716
import java.util.function.Function;
1817
import java.util.function.Predicate;
1918
import java.util.stream.Stream;
@@ -36,7 +35,7 @@ class JarServiceNameDetectorTest {
3635

3736
@BeforeEach
3837
void setUp() {
39-
ResourceDiscoveryCache.resetForTest();
38+
JarPathFinder.resetForTest();
4039
}
4140

4241
@Test
@@ -54,7 +53,7 @@ void createResource_empty() {
5453
private static JarServiceNameDetector getDetector(
5554
String[] processArgs, Function<String, String> getProperty, Predicate<Path> fileExists) {
5655
return new JarServiceNameDetector(
57-
new JarPathFinder(() -> processArgs, getProperty, fileExists, p -> Optional.empty()));
56+
new JarPathFinder(() -> processArgs, getProperty, fileExists));
5857
}
5958

6059
@Test

instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/ManifestResourceProviderTest.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ManifestResourceProviderTest {
2727

2828
@BeforeEach
2929
void setUp() {
30-
ResourceDiscoveryCache.resetForTest();
30+
JarPathFinder.resetForTest();
3131
}
3232

3333
private static class TestCase {
@@ -63,16 +63,16 @@ Collection<DynamicTest> createResource() {
6363
new JarPathFinder(
6464
() -> JarServiceNameDetectorTest.getArgs("app.jar"),
6565
prop -> null,
66-
JarServiceNameDetectorTest::failPath,
67-
p -> {
68-
try {
69-
Manifest manifest = new Manifest();
70-
manifest.read(t.input);
71-
return Optional.of(manifest);
72-
} catch (Exception e) {
73-
return Optional.empty();
74-
}
75-
}));
66+
JarServiceNameDetectorTest::failPath),
67+
p -> {
68+
try {
69+
Manifest manifest = new Manifest();
70+
manifest.read(t.input);
71+
return Optional.of(manifest);
72+
} catch (Exception e) {
73+
return Optional.empty();
74+
}
75+
});
7676
provider.shouldApply(config, Resource.getDefault());
7777

7878
Resource resource = provider.createResource(config);

0 commit comments

Comments
 (0)