diff --git a/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/MainJarPathFinder.java b/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/MainJarPathFinder.java index 8f9b9287ac68..c0c7588698f2 100644 --- a/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/MainJarPathFinder.java +++ b/instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/MainJarPathFinder.java @@ -44,9 +44,14 @@ Path detectJarPath() { @Nullable private Path getJarPathFromProcessHandle() { String[] javaArgs = getProcessHandleArguments.get(); - for (int i = 0; i < javaArgs.length; ++i) { - if ("-jar".equals(javaArgs[i]) && (i < javaArgs.length - 1)) { - return Paths.get(javaArgs[i + 1]); + boolean jarOptionFound = false; + for (String javaArg : javaArgs) { + if ("-jar".equals(javaArg)) { + jarOptionFound = true; + } else if (jarOptionFound + && !javaArg.startsWith( + "-")) { // flags can appear between -jar and the jar path, ignore them + return Paths.get(javaArg); } } return null; 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 bcb0ff2c631d..b44851bfaa13 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 @@ -73,6 +73,22 @@ void createResource_processHandleJar() { .containsEntry(ServiceAttributes.SERVICE_NAME, "my-service"); } + @Test + void createResource_processHandleJarExtraFlag() { + String path = Paths.get("path", "to", "app", "my-service.jar").toString(); + JarServiceNameDetector serviceNameProvider = + getDetector( + new String[] {"-Dtest=42", "-jar", "-Xmx512m", path, "abc", "def"}, + prop -> null, + JarServiceNameDetectorTest::failPath); + + Resource resource = serviceNameProvider.createResource(config); + + assertThat(resource.getAttributes()) + .hasSize(1) + .containsEntry(ServiceAttributes.SERVICE_NAME, "my-service"); + } + @Test void createResource_processHandleJarWithoutExtension() { JarServiceNameDetector serviceNameProvider =