Skip to content

Commit fe331a2

Browse files
[release/v1.22.x] Fix potential startup failure (#7572)
Clean cherry-pick of #7567 to the `release/v1.22.x` branch. Co-authored-by: Trask Stalnaker <[email protected]>
1 parent 299db25 commit fe331a2

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.opentelemetry.sdk.resources.Resource;
1616
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
1717
import java.nio.file.Files;
18+
import java.nio.file.InvalidPathException;
1819
import java.nio.file.Path;
1920
import java.nio.file.Paths;
2021
import java.util.Map;
@@ -101,17 +102,27 @@ private Path getJarPathFromSunCommandLine() {
101102
while (true) {
102103
int nextSpace = programArguments.indexOf(' ', next);
103104
if (nextSpace == -1) {
104-
Path candidate = Paths.get(programArguments);
105-
return fileExists.test(candidate) ? candidate : null;
105+
return pathIfExists(programArguments);
106106
}
107-
Path candidate = Paths.get(programArguments.substring(0, nextSpace));
107+
Path path = pathIfExists(programArguments.substring(0, nextSpace));
108108
next = nextSpace + 1;
109-
if (fileExists.test(candidate)) {
110-
return candidate;
109+
if (path != null) {
110+
return path;
111111
}
112112
}
113113
}
114114

115+
@Nullable
116+
private Path pathIfExists(String programArguments) {
117+
Path candidate;
118+
try {
119+
candidate = Paths.get(programArguments);
120+
} catch (InvalidPathException e) {
121+
return null;
122+
}
123+
return fileExists.test(candidate) ? candidate : null;
124+
}
125+
115126
private static String getServiceName(Path jarPath) {
116127
String jarName = jarPath.getFileName().toString();
117128
int dotIndex = jarName.lastIndexOf(".");

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ void createResource_sunCommandLine(String commandLine, Path jarPath) {
9898
.containsEntry(ResourceAttributes.SERVICE_NAME, "my-service");
9999
}
100100

101+
// regression test for
102+
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/7567
103+
@Test
104+
void createResource_sunCommandLineProblematicArgs() {
105+
Function<String, String> getProperty =
106+
key -> key.equals("sun.java.command") ? "one C:/two" : null;
107+
Predicate<Path> fileExists = path -> false;
108+
109+
JarServiceNameDetector serviceNameProvider =
110+
new JarServiceNameDetector(() -> new String[0], getProperty, fileExists);
111+
112+
Resource resource = serviceNameProvider.createResource(config);
113+
114+
assertThat(resource.getAttributes()).isEmpty();
115+
}
116+
101117
static final class SunCommandLineProvider implements ArgumentsProvider {
102118

103119
@Override

0 commit comments

Comments
 (0)