|
21 | 21 |
|
22 | 22 | package com.microsoft.applicationinsights.attach;
|
23 | 23 |
|
| 24 | +import io.opentelemetry.contrib.attach.CoreRuntimeAttach; |
24 | 25 | import java.io.BufferedReader;
|
25 |
| -import java.io.File; |
26 | 26 | import java.io.IOException;
|
27 | 27 | import java.io.InputStream;
|
28 | 28 | import java.io.InputStreamReader;
|
29 | 29 | import java.nio.charset.StandardCharsets;
|
30 | 30 | import java.util.Optional;
|
| 31 | +import java.util.Properties; |
31 | 32 | import java.util.logging.Logger;
|
32 | 33 | import java.util.stream.Collectors;
|
33 | 34 |
|
@@ -62,18 +63,10 @@ public static void attach() {
|
62 | 63 | System.setProperty(RUNTIME_ATTACHED_JSON_PROPERTY, jsonConfig.get());
|
63 | 64 | }
|
64 | 65 |
|
65 |
| - File agentFile = AppInsightAgentFileProvider.getAgentFile(); |
| 66 | + String appInsightResourceName = findAppInsightResourceName(); |
| 67 | + CoreRuntimeAttach runtimeAttach = new CoreRuntimeAttach(appInsightResourceName); |
66 | 68 |
|
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(); |
77 | 70 | }
|
78 | 71 |
|
79 | 72 | private static Optional<String> findJsonConfig() {
|
@@ -102,4 +95,20 @@ private static boolean agentIsAttached() {
|
102 | 95 | return false;
|
103 | 96 | }
|
104 | 97 | }
|
| 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 | + } |
105 | 114 | }
|
0 commit comments