Skip to content

Commit 83f3613

Browse files
authored
Use more OTel code for runtime attachment (#2414)
1 parent 7e049a8 commit 83f3613

File tree

4 files changed

+22
-227
lines changed

4 files changed

+22
-227
lines changed

agent/runtime-attach/build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ val otelVersion: String by project
1010
val agent: Configuration by configurations.creating
1111

1212
dependencies {
13-
implementation("net.bytebuddy:byte-buddy-agent") // To replace with implementation("io.opentelemetry.contrib:opentelemetry-runtime-attach:$otelContribAlphaVersion")
14-
// after next OTel java contrib release
13+
implementation("io.opentelemetry.contrib:opentelemetry-runtime-attach-core:$otelContribAlphaVersion")
1514
agent(project(":agent:agent", configuration = "shadow"))
1615
}
1716

agent/runtime-attach/src/main/java/com/microsoft/applicationinsights/attach/AppInsightAgentFileProvider.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

agent/runtime-attach/src/main/java/com/microsoft/applicationinsights/attach/ApplicationInsights.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
package com.microsoft.applicationinsights.attach;
2323

24+
import io.opentelemetry.contrib.attach.CoreRuntimeAttach;
2425
import java.io.BufferedReader;
25-
import java.io.File;
2626
import java.io.IOException;
2727
import java.io.InputStream;
2828
import java.io.InputStreamReader;
2929
import java.nio.charset.StandardCharsets;
3030
import java.util.Optional;
31+
import java.util.Properties;
3132
import java.util.logging.Logger;
3233
import java.util.stream.Collectors;
3334

@@ -62,18 +63,10 @@ public static void attach() {
6263
System.setProperty(RUNTIME_ATTACHED_JSON_PROPERTY, jsonConfig.get());
6364
}
6465

65-
File agentFile = AppInsightAgentFileProvider.getAgentFile();
66+
String appInsightResourceName = findAppInsightResourceName();
67+
CoreRuntimeAttach runtimeAttach = new CoreRuntimeAttach(appInsightResourceName);
6668

67-
try {
68-
RuntimeAttach.attachJavaagentToCurrentJvm(agentFile);
69-
} catch (IllegalStateException e) {
70-
if (e.getMessage() != null
71-
&& e.getMessage()
72-
.contains("No compatible attachment provider is available")) { // Byte Buddy exception
73-
throw new IllegalStateException("Runtime attachment was not done. You may use a JRE.", e);
74-
}
75-
throw e;
76-
}
69+
runtimeAttach.attachJavaagentToCurrentJVM();
7770
}
7871

7972
private static Optional<String> findJsonConfig() {
@@ -102,4 +95,20 @@ private static boolean agentIsAttached() {
10295
return false;
10396
}
10497
}
98+
99+
private static String findAppInsightResourceName() {
100+
String appInsightVersion = findAppInsightVersion();
101+
return "/applicationinsights-agent-" + appInsightVersion + ".jar";
102+
}
103+
104+
private static String findAppInsightVersion() {
105+
try (InputStream jarAsInputStream =
106+
ApplicationInsights.class.getResourceAsStream("/ai.sdk-version.properties")) {
107+
Properties props = new Properties();
108+
props.load(jarAsInputStream);
109+
return props.getProperty("version");
110+
} catch (IOException e) {
111+
throw new IllegalStateException("Unable to find Application Insights version", e);
112+
}
113+
}
105114
}

agent/runtime-attach/src/main/java/com/microsoft/applicationinsights/attach/RuntimeAttach.java

Lines changed: 0 additions & 126 deletions
This file was deleted.

0 commit comments

Comments
 (0)