Skip to content

Commit

Permalink
cache discovered jar path
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Feb 23, 2024
1 parent 14c667d commit 10f0e36
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ public JarFileDetector() {

@Nullable
Path getJarPath() {
Path jarPath = getJarPathFromProcessHandle();
if (jarPath != null) {
return jarPath;
}
return getJarPathFromSunCommandLine();
return ResourceDiscoveryCache.get(
"jarPath",
() -> {
Path jarPath = getJarPathFromProcessHandle();
if (jarPath != null) {
return jarPath;
}
return getJarPathFromSunCommandLine();
});
}

Optional<Manifest> getManifestFromJarFile() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.resources;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public class ResourceDiscoveryCache {
private static final ConcurrentHashMap<String, Object> cache = new ConcurrentHashMap<>();

private ResourceDiscoveryCache() {}

// visible for testing
public static void resetForTest() {
cache.clear();
}

@SuppressWarnings("unchecked")
public static <T> T get(String key, Supplier<T> supplier) {
return (T) cache.computeIfAbsent(key, k -> supplier.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand All @@ -33,6 +34,11 @@ class JarServiceNameDetectorTest {

@Mock ConfigProperties config;

@BeforeEach
void setUp() {
ResourceDiscoveryCache.resetForTest();
}

@Test
void createResource_empty() {
String[] processArgs = new String[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@
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() {
ResourceDiscoveryCache.resetForTest();
}

private static class TestCase {
private final String name;
private final String expectedName;
Expand Down

0 comments on commit 10f0e36

Please sign in to comment.