-
Notifications
You must be signed in to change notification settings - Fork 2
Ark/autoinstrumentation #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Java plugin, toolchain (Java 17 / Adoptium), options.release, and repositories | ||
| // are inherited from the parent's subprojects {} block. | ||
|
|
||
| // These are compile-time dependencies so bootstrap classes can reference OTel types. | ||
| // At runtime, the actual OTel JARs are bundled as normal .class files in the agent JAR | ||
| // and placed on the bootstrap classpath via Instrumentation.appendToBootstrapClassLoaderSearch(). | ||
| dependencies { | ||
| compileOnly "io.opentelemetry:opentelemetry-api:${otelVersion}" | ||
| compileOnly "io.opentelemetry:opentelemetry-sdk:${otelVersion}" | ||
| compileOnly "io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:${otelVersion}" | ||
| compileOnly "io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi:${otelVersion}" | ||
| } |
31 changes: 31 additions & 0 deletions
31
braintrust-java-agent/bootstrap/src/main/java/dev/braintrust/bootstrap/BraintrustBridge.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package dev.braintrust.bootstrap; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| /** Globally available bootstrap classpath resource class */ | ||
| public class BraintrustBridge { | ||
| public static final String INSTRUMENTATION_NAME = "braintrust-java"; | ||
|
|
||
| /** | ||
| * Diagnostic utility tracking the number of times braintrust otel has been installed. | ||
| * | ||
| * <p>In a production app, this should be zero until global otel get() is invoked, then it | ||
| * should remain at 1 for the rest of app's lifetime. | ||
| */ | ||
| public static final AtomicInteger otelInstallCount = new AtomicInteger(0); | ||
|
|
||
| private static final AtomicReference<BraintrustClassLoader> agentClassLoaderRef = | ||
| new AtomicReference<>(); | ||
|
|
||
| public static BraintrustClassLoader getAgentClassLoader() { | ||
| return agentClassLoaderRef.get(); | ||
| } | ||
|
|
||
| public static void setAgentClassLoaderIfAbsent(BraintrustClassLoader classLoader) { | ||
| var witness = agentClassLoaderRef.compareAndExchange(null, classLoader); | ||
| if (null != witness) { | ||
| throw new IllegalStateException("agent classloader must only be set once"); | ||
| } | ||
| } | ||
| } |
130 changes: 130 additions & 0 deletions
130
...st-java-agent/bootstrap/src/main/java/dev/braintrust/bootstrap/BraintrustClassLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| package dev.braintrust.bootstrap; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.URL; | ||
| import java.security.CodeSource; | ||
| import java.security.SecureClassLoader; | ||
| import java.security.cert.Certificate; | ||
| import java.util.Collections; | ||
| import java.util.Enumeration; | ||
| import java.util.jar.JarEntry; | ||
| import java.util.jar.JarFile; | ||
|
|
||
| /** | ||
| * A classloader that loads agent-internal classes from {@code .classdata} entries inside the agent | ||
| * JAR. | ||
| * | ||
| * <p>Classes stored under the {@code internal/} prefix with a {@code .classdata} extension are | ||
| * invisible to the JVM's default classloading mechanism. This classloader knows how to find them, | ||
| * providing full classloader isolation between the agent's internals and the application's | ||
| * classpath. | ||
| */ | ||
| public class BraintrustClassLoader extends SecureClassLoader { | ||
| private static final String ENTRY_PREFIX = "internal/"; | ||
| private static final String CLASS_DATA_SUFFIX = ".classdata"; | ||
|
|
||
| private final JarFile agentJarFile; | ||
| private final CodeSource agentCodeSource; | ||
| private final String agentResourcePrefix; | ||
|
|
||
| static { | ||
| registerAsParallelCapable(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new BraintrustClassLoader. | ||
| * | ||
| * @param agentJarURL the URL of the agent JAR file (from the -javaagent path) | ||
| * @param parent the parent classloader (typically the system/platform classloader) | ||
| */ | ||
| public BraintrustClassLoader(URL agentJarURL, ClassLoader parent) throws Exception { | ||
| super(parent); | ||
| this.agentJarFile = new JarFile(new java.io.File(agentJarURL.toURI()), false); | ||
| this.agentCodeSource = new CodeSource(agentJarURL, (Certificate[]) null); | ||
| this.agentResourcePrefix = "jar:file:" + agentJarFile.getName() + "!/"; | ||
| } | ||
|
|
||
| @Override | ||
| protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
| // Convert "dev.braintrust.agent.internal.BraintrustAgent" | ||
| // -> "internal/dev/braintrust/agent/internal/BraintrustAgent.classdata" | ||
| String entryName = ENTRY_PREFIX + name.replace('.', '/') + CLASS_DATA_SUFFIX; | ||
| JarEntry entry = agentJarFile.getJarEntry(entryName); | ||
| if (entry == null) { | ||
| throw new ClassNotFoundException(name); | ||
| } | ||
|
|
||
| byte[] classBytes = readEntry(entry, name); | ||
| return defineClass(name, classBytes, 0, classBytes.length, agentCodeSource); | ||
| } | ||
|
|
||
| @Override | ||
| protected URL findResource(String name) { | ||
| // For .class resource lookups, map to .classdata | ||
| String entryName; | ||
| if (name.endsWith(".class")) { | ||
| entryName = | ||
| ENTRY_PREFIX | ||
| + name.substring(0, name.length() - ".class".length()) | ||
| + CLASS_DATA_SUFFIX; | ||
| } else { | ||
| entryName = ENTRY_PREFIX + name; | ||
| } | ||
|
|
||
| JarEntry entry = agentJarFile.getJarEntry(entryName); | ||
| if (entry != null) { | ||
| try { | ||
| return new URL(agentResourcePrefix + entryName); | ||
| } catch (java.net.MalformedURLException e) { | ||
| // fall through | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| protected Enumeration<URL> findResources(String name) { | ||
| URL resource = findResource(name); | ||
| if (resource != null) { | ||
| return Collections.enumeration(Collections.singletonList(resource)); | ||
| } | ||
| return Collections.emptyEnumeration(); | ||
| } | ||
|
|
||
| private byte[] readEntry(JarEntry entry, String className) throws ClassNotFoundException { | ||
| int size = (int) entry.getSize(); | ||
| if (size < 0) { | ||
| try (InputStream in = agentJarFile.getInputStream(entry); | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream()) { | ||
| byte[] buf = new byte[8192]; | ||
| int bytesRead; | ||
| while ((bytesRead = in.read(buf)) >= 0) { | ||
| out.write(buf, 0, bytesRead); | ||
| } | ||
| return out.toByteArray(); | ||
| } catch (IOException e) { | ||
| throw new ClassNotFoundException(className, e); | ||
| } | ||
| } | ||
| byte[] buf = new byte[size]; | ||
| try (InputStream in = agentJarFile.getInputStream(entry)) { | ||
| int offset = 0; | ||
| while (offset < size) { | ||
| int bytesRead = in.read(buf, offset, size - offset); | ||
| if (bytesRead < 0) { | ||
| break; | ||
| } | ||
| offset += bytesRead; | ||
| } | ||
| if (offset != size) { | ||
| throw new ClassNotFoundException( | ||
| className + " (incomplete read: " + offset + "/" + size + " bytes)"); | ||
| } | ||
| return buf; | ||
| } catch (IOException e) { | ||
| throw new ClassNotFoundException(className, e); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.