Skip to content

Commit

Permalink
Use more OTel code for runtime attachment (#2414)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanbisutti authored Jul 21, 2022
1 parent 7e049a8 commit 83f3613
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 227 deletions.
3 changes: 1 addition & 2 deletions agent/runtime-attach/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ val otelVersion: String by project
val agent: Configuration by configurations.creating

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

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

package com.microsoft.applicationinsights.attach;

import io.opentelemetry.contrib.attach.CoreRuntimeAttach;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.Properties;
import java.util.logging.Logger;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -62,18 +63,10 @@ public static void attach() {
System.setProperty(RUNTIME_ATTACHED_JSON_PROPERTY, jsonConfig.get());
}

File agentFile = AppInsightAgentFileProvider.getAgentFile();
String appInsightResourceName = findAppInsightResourceName();
CoreRuntimeAttach runtimeAttach = new CoreRuntimeAttach(appInsightResourceName);

try {
RuntimeAttach.attachJavaagentToCurrentJvm(agentFile);
} catch (IllegalStateException e) {
if (e.getMessage() != null
&& e.getMessage()
.contains("No compatible attachment provider is available")) { // Byte Buddy exception
throw new IllegalStateException("Runtime attachment was not done. You may use a JRE.", e);
}
throw e;
}
runtimeAttach.attachJavaagentToCurrentJVM();
}

private static Optional<String> findJsonConfig() {
Expand Down Expand Up @@ -102,4 +95,20 @@ private static boolean agentIsAttached() {
return false;
}
}

private static String findAppInsightResourceName() {
String appInsightVersion = findAppInsightVersion();
return "/applicationinsights-agent-" + appInsightVersion + ".jar";
}

private static String findAppInsightVersion() {
try (InputStream jarAsInputStream =
ApplicationInsights.class.getResourceAsStream("/ai.sdk-version.properties")) {
Properties props = new Properties();
props.load(jarAsInputStream);
return props.getProperty("version");
} catch (IOException e) {
throw new IllegalStateException("Unable to find Application Insights version", e);
}
}
}

This file was deleted.

0 comments on commit 83f3613

Please sign in to comment.