Skip to content

Commit f925f74

Browse files
jeanbisuttitrask
andauthored
Fix "URI is not hierarchical" during runtime attachment (#359)
* Fix "java.lang.IllegalArgumentException: URI is not hierarchical" during runtime attachment * Fix build issue * Fiw file creation and deletion * Add null checks * Only delete jar file on JVM exit * Fix merge * Fix spotless * Remove null checks * Spotless Co-authored-by: Trask Stalnaker <[email protected]>
1 parent d565093 commit f925f74

File tree

3 files changed

+74
-39
lines changed

3 files changed

+74
-39
lines changed

runtime-attach/src/main/java/io/opentelemetry/contrib/attach/AgentFileLocator.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.attach;
7+
8+
import io.opentelemetry.javaagent.OpenTelemetryAgent;
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.net.URL;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.security.CodeSource;
16+
17+
final class AgentFileProvider {
18+
19+
static File getAgentFile() {
20+
21+
verifyExistenceOfAgentJarFile();
22+
23+
Path tempDirPath = createTempDir();
24+
25+
Path tempAgentJarPath = createTempAgentJarFile(tempDirPath);
26+
27+
deleteTempDirOnJvmExit(tempDirPath, tempAgentJarPath);
28+
29+
return tempAgentJarPath.toFile();
30+
}
31+
32+
private static void deleteTempDirOnJvmExit(Path tempDirPath, Path tempAgentJarPath) {
33+
tempAgentJarPath.toFile().deleteOnExit();
34+
tempDirPath.toFile().deleteOnExit();
35+
}
36+
37+
private static void verifyExistenceOfAgentJarFile() {
38+
CodeSource codeSource = OpenTelemetryAgent.class.getProtectionDomain().getCodeSource();
39+
if (codeSource == null) {
40+
throw new IllegalStateException("could not get agent jar location");
41+
}
42+
}
43+
44+
private static Path createTempDir() {
45+
Path tempDir;
46+
try {
47+
tempDir = Files.createTempDirectory("otel-agent");
48+
} catch (IOException e) {
49+
throw new IllegalStateException("Runtime attachment can't create temp directory", e);
50+
}
51+
return tempDir;
52+
}
53+
54+
private static Path createTempAgentJarFile(Path tempDir) {
55+
URL url = OpenTelemetryAgent.class.getProtectionDomain().getCodeSource().getLocation();
56+
try {
57+
return copyTo(url, tempDir, "agent.jar");
58+
} catch (IOException e) {
59+
throw new IllegalStateException(
60+
"Runtime attachment can't create agent jar file in temp directory", e);
61+
}
62+
}
63+
64+
private static Path copyTo(URL url, Path tempDir, String fileName) throws IOException {
65+
Path tempFile = tempDir.resolve(fileName);
66+
try (InputStream in = url.openStream()) {
67+
Files.copy(in, tempFile);
68+
}
69+
return tempFile;
70+
}
71+
72+
private AgentFileProvider() {}
73+
}

runtime-attach/src/main/java/io/opentelemetry/contrib/attach/RuntimeAttach.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static void attachJavaagentToCurrentJVM() {
2828
return;
2929
}
3030

31-
File javaagentFile = AgentFileLocator.locateAgentFile();
31+
File javaagentFile = AgentFileProvider.getAgentFile();
3232
ByteBuddyAgent.attach(javaagentFile, getPid());
3333

3434
if (!agentIsAttached()) {

0 commit comments

Comments
 (0)