Skip to content

Commit 5bef441

Browse files
authored
Improve exception handling and documentation of runtime attachment (#388)
1 parent 7d19e67 commit 5bef441

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

runtime-attach/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ If you can't update the JVM arguments to attach the [OpenTelemetry Java agent](h
44

55
The `io.opentelemetry.contrib.attach.RuntimeAttach` class has an `attachJavaagentToCurrentJVM` method allowing to trigger the attachment of the OTel agent for Java.
66

7-
The attachment will not be initiated in the following cases:
7+
The attachment will _not_ be initiated in the following cases:
88
* The `otel.javaagent.enabled` property is set to `false`
99
* The `OTEL_JAVAAGENT_ENABLED` environment variable is set to `false`
1010
* The attachment is not requested from the _main_ thread
1111
* The attachment is not requested from the `public static void main(String[] args)` method
1212
* The agent is already attached
13+
* The application is running on a JRE (a JDK is necessary)
14+
* The application is running on a read-only file system
1315

1416
_The attachment must be requested at the beginning of the `public static void main(String[] args)` method._ We give below an example for Spring Boot applications:
1517

runtime-attach/runtime-attach-core/src/main/java/io/opentelemetry/contrib/attach/AgentFileProvider.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ private static Path createTempDir() {
4040
try {
4141
tempDir = Files.createTempDirectory("otel-agent");
4242
} catch (IOException e) {
43-
throw new IllegalStateException("Runtime attachment can't create temp directory", e);
43+
throw new RuntimeAttachException(
44+
"Runtime attachment can't create a temp directory. Are you using a read-only file system?",
45+
e);
4446
}
4547
return tempDir;
4648
}
@@ -50,12 +52,12 @@ private Path createTempAgentJarFileIn(Path tempDir) {
5052
try (InputStream jarAsInputStream =
5153
AgentFileProvider.class.getResourceAsStream(this.agentJarResourceName)) {
5254
if (jarAsInputStream == null) {
53-
throw new IllegalStateException(this.agentJarResourceName + " resource can't be found");
55+
throw new RuntimeAttachException(this.agentJarResourceName + " resource can't be found");
5456
}
5557
Files.copy(jarAsInputStream, agentJarPath);
5658
} catch (IOException e) {
57-
throw new IllegalStateException(
58-
"Runtime attachment can't create agent jar file in temp directory", e);
59+
throw new RuntimeAttachException(
60+
"Runtime attachment can't create an agent jar file in temp directory", e);
5961
}
6062
return agentJarPath;
6163
}

runtime-attach/runtime-attach-core/src/main/java/io/opentelemetry/contrib/attach/CoreRuntimeAttach.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public void attachJavaagentToCurrentJVM() {
4040
AgentFileProvider agentFileProvider = new AgentFileProvider(agentJarResourceName);
4141

4242
File javaagentFile = agentFileProvider.getAgentFile();
43-
ByteBuddyAgent.attach(javaagentFile, getPid());
43+
44+
try {
45+
ByteBuddyAgent.attach(javaagentFile, getPid());
46+
} catch (RuntimeException e) {
47+
handleByteBuddyException(e);
48+
}
4449

4550
if (!agentIsAttached()) {
4651
printError("Agent was not attached. An unexpected issue has happened.");
@@ -88,6 +93,24 @@ private static boolean agentIsDisabledWithEnvVar() {
8893
return "false".equals(agentEnabledEnvVarValue);
8994
}
9095

96+
private static void handleByteBuddyException(RuntimeException exception) {
97+
handleNoAgentProvider(exception);
98+
throw new RuntimeAttachException(
99+
"A problem has occurred during the runtime attachment of the Java agent.", exception);
100+
}
101+
102+
private static void handleNoAgentProvider(RuntimeException exception) {
103+
if (exception instanceof IllegalStateException) {
104+
String message = exception.getMessage();
105+
if (message != null
106+
&& message.contains(
107+
"No compatible attachment provider is available")) { // ByteBuddy message
108+
throw new RuntimeAttachException(
109+
"Runtime attachment has failed. Are you using a JRE (not a JDK)?", exception);
110+
}
111+
}
112+
}
113+
91114
private static boolean agentIsAttached() {
92115
try {
93116
Class.forName("io.opentelemetry.javaagent.OpenTelemetryAgent", false, null);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.attach;
7+
8+
/**
9+
* Exception that gets thrown if a problem occurs during the attachment of the OpenTelemetry agent.
10+
*/
11+
public final class RuntimeAttachException extends RuntimeException {
12+
13+
private static final long serialVersionUID = 1982913847038355735L;
14+
15+
private RuntimeAttachException() {}
16+
17+
RuntimeAttachException(String message) {
18+
super(message);
19+
}
20+
21+
RuntimeAttachException(String message, Throwable cause) {
22+
super(message, cause);
23+
}
24+
}

0 commit comments

Comments
 (0)