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 ef4cff5e5c6c..702f07a50b49 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 @@ -15,6 +15,7 @@ import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Map; @@ -101,17 +102,27 @@ private Path getJarPathFromSunCommandLine() { while (true) { int nextSpace = programArguments.indexOf(' ', next); if (nextSpace == -1) { - Path candidate = Paths.get(programArguments); - return fileExists.test(candidate) ? candidate : null; + return pathIfExists(programArguments); } - Path candidate = Paths.get(programArguments.substring(0, nextSpace)); + Path path = pathIfExists(programArguments.substring(0, nextSpace)); next = nextSpace + 1; - if (fileExists.test(candidate)) { - return candidate; + if (path != null) { + return path; } } } + @Nullable + private Path pathIfExists(String programArguments) { + Path candidate; + try { + candidate = Paths.get(programArguments); + } catch (InvalidPathException e) { + return null; + } + return fileExists.test(candidate) ? candidate : null; + } + private static String getServiceName(Path jarPath) { String jarName = jarPath.getFileName().toString(); int dotIndex = jarName.lastIndexOf("."); 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 c6c8052265fb..479940b8eeeb 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 @@ -98,6 +98,22 @@ void createResource_sunCommandLine(String commandLine, Path jarPath) { .containsEntry(ResourceAttributes.SERVICE_NAME, "my-service"); } + // regression test for + // https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/7567 + @Test + void createResource_sunCommandLineProblematicArgs() { + Function getProperty = + key -> key.equals("sun.java.command") ? "one C:/two" : null; + Predicate fileExists = path -> false; + + JarServiceNameDetector serviceNameProvider = + new JarServiceNameDetector(() -> new String[0], getProperty, fileExists); + + Resource resource = serviceNameProvider.createResource(config); + + assertThat(resource.getAttributes()).isEmpty(); + } + static final class SunCommandLineProvider implements ArgumentsProvider { @Override