Skip to content

Commit 3ad4269

Browse files
authored
compatible different command line style for find jar path (#12544)
1 parent 905c274 commit 3ad4269

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ Path detectJarPath() {
4444
@Nullable
4545
private Path getJarPathFromProcessHandle() {
4646
String[] javaArgs = getProcessHandleArguments.get();
47-
for (int i = 0; i < javaArgs.length; ++i) {
48-
if ("-jar".equals(javaArgs[i]) && (i < javaArgs.length - 1)) {
49-
return Paths.get(javaArgs[i + 1]);
47+
boolean jarOptionFound = false;
48+
for (String javaArg : javaArgs) {
49+
if ("-jar".equals(javaArg)) {
50+
jarOptionFound = true;
51+
} else if (jarOptionFound
52+
&& !javaArg.startsWith(
53+
"-")) { // flags can appear between -jar and the jar path, ignore them
54+
return Paths.get(javaArg);
5055
}
5156
}
5257
return null;

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

+16
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ void createResource_processHandleJar() {
7373
.containsEntry(ServiceAttributes.SERVICE_NAME, "my-service");
7474
}
7575

76+
@Test
77+
void createResource_processHandleJarExtraFlag() {
78+
String path = Paths.get("path", "to", "app", "my-service.jar").toString();
79+
JarServiceNameDetector serviceNameProvider =
80+
getDetector(
81+
new String[] {"-Dtest=42", "-jar", "-Xmx512m", path, "abc", "def"},
82+
prop -> null,
83+
JarServiceNameDetectorTest::failPath);
84+
85+
Resource resource = serviceNameProvider.createResource(config);
86+
87+
assertThat(resource.getAttributes())
88+
.hasSize(1)
89+
.containsEntry(ServiceAttributes.SERVICE_NAME, "my-service");
90+
}
91+
7692
@Test
7793
void createResource_processHandleJarWithoutExtension() {
7894
JarServiceNameDetector serviceNameProvider =

0 commit comments

Comments
 (0)