-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAgentBootstrap.java
More file actions
178 lines (157 loc) · 7.43 KB
/
AgentBootstrap.java
File metadata and controls
178 lines (157 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package dev.braintrust.system;
import java.io.File;
import java.lang.instrument.Instrumentation;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarFile;
/**
* Braintrust Java Agent entry point.
*
* <p>Minimal code which bootstraps braintrust classloading and instrumentation.
*/
public class AgentBootstrap {
private static final String AGENT_CLASS = "dev.braintrust.agent.BraintrustAgent";
private static final String INSTALLER_METHOD = "install";
static volatile boolean installed = false;
/** Entry point when the agent is loaded at JVM startup via {@code -javaagent}. */
public static void premain(String agentArgs, Instrumentation inst) {
install(agentArgs, inst);
}
/** Entry point when the agent is attached to a running JVM via the Attach API. */
public static void agentmain(String agentArgs, Instrumentation inst) {
install(agentArgs, inst);
}
private static synchronized void install(String agentArgs, Instrumentation inst) {
if (AgentBootstrap.class.getClassLoader() != ClassLoader.getSystemClassLoader()) {
log(
"WARNING: install attempted on non-system classloader. aborting. classloader: "
+ AgentBootstrap.class.getClassLoader());
return;
}
if (installed) {
log("Agent already installed, skipping.");
return;
}
log("Braintrust Java Agent starting...");
if (jvmRunningWithOtelAgent()) {
log(
"ERROR: Braintrust agent is not yet compatible with the OTel -javaagent."
+ " aborting install.");
return;
}
if (jvmRunningWithDatadogOtelConfig() && (!isRunningAfterDatadogAgent())) {
log("ERROR: Braintrust agent must run _after_ datadog -javaagent. aborting install.");
return;
}
boolean installOnBootstrap = !jvmRunningWithDatadogOtelConfig();
try {
// Locate the agent JAR from our own code source
URL agentJarURL =
AgentBootstrap.class.getProtectionDomain().getCodeSource().getLocation();
File agentJarFile = new File(agentJarURL.toURI());
log("Agent JAR: " + agentJarFile);
// Enable OTel autoconfigure BEFORE adding to bootstrap, so system properties
// are set before anything can trigger GlobalOpenTelemetry.get().
enableOtelSDKAutoconfiguration();
ClassLoader btClassLoaderParent;
if (installOnBootstrap) {
inst.appendToBootstrapClassLoaderSearch(new JarFile(agentJarFile, false));
btClassLoaderParent = ClassLoader.getPlatformClassLoader();
log("Added agent JAR to bootstrap classpath.");
} else {
btClassLoaderParent =
new URLClassLoader(
new URL[] {agentJarFile.toURI().toURL()},
ClassLoader.getPlatformClassLoader());
log("skipping bootstrap classpath setup");
}
// Load and invoke the real agent installer through the isolated classloader.
ClassLoader btClassLoader = createBTClassLoader(agentJarURL, btClassLoaderParent);
Class<?> installerClass = btClassLoader.loadClass(AGENT_CLASS);
installerClass
.getMethod(INSTALLER_METHOD, String.class, Instrumentation.class)
.invoke(null, agentArgs, inst);
log("Braintrust Java Agent installed.");
installed = true;
} catch (Throwable t) {
log("ERROR: Failed to install Braintrust Java Agent: " + t.getMessage());
log(t);
}
}
private static ClassLoader createBTClassLoader(URL agentJarURL, ClassLoader btClassLoaderParent)
throws Exception {
// NOTE: not caching because we only invoke this once
var bridgeClass =
btClassLoaderParent.loadClass("dev.braintrust.bootstrap.BraintrustBridge");
var createMethod =
bridgeClass.getMethod("createBraintrustClassLoader", URL.class, ClassLoader.class);
return (ClassLoader) createMethod.invoke(null, agentJarURL, btClassLoaderParent);
}
/**
* Checks whether the OpenTelemetry Java agent is present by looking for its premain class on
* the system classloader. Since {@code -javaagent} JARs are always on the system classpath,
* this works regardless of agent ordering.
*/
private static boolean jvmRunningWithOtelAgent() {
try {
Class.forName(
"io.opentelemetry.javaagent.OpenTelemetryAgent",
false,
ClassLoader.getSystemClassLoader());
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
/** Checks whether the Datadog agent is present and configured for OTel integration */
private static boolean jvmRunningWithDatadogOtelConfig() {
String sysProp = System.getProperty("dd.trace.otel.enabled");
if (sysProp != null) {
return Boolean.parseBoolean(sysProp);
}
String envVar = System.getenv("DD_TRACE_OTEL_ENABLED");
return Boolean.parseBoolean(envVar);
}
/**
* Returns true if the Datadog agent's premain has already executed, meaning it was listed
* before the Braintrust agent in the {@code -javaagent} flags.
*/
private static boolean isRunningAfterDatadogAgent() {
// DD's premain appends its jars to the bootstrap classpath, making
// {@code datadog.trace.bootstrap.Agent} loadable from the bootstrap (null)
// classloader. If that class is not found on bootstrap, DD either isn't
// present or hasn't run its premain yet (i.e. BT is first).
try {
Class.forName("datadog.trace.bootstrap.Agent", false, null);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
private static void enableOtelSDKAutoconfiguration() {
// Enable OTel SDK autoconfiguration. When anyone first calls
// GlobalOpenTelemetry.get(), the SDK will be built using autoconfigure, which
// discovers our BraintrustAutoConfigCustomizer via ServiceLoader and injects the
// Braintrust span processor/exporter into the tracer provider.
setPropertyIfAbsent("otel.java.global-autoconfigure.enabled", "true");
// Set default exporter config. We don't want autoconfigure to try loading
// OTLP exporters from the bootstrap classpath (they live in BraintrustClassLoader).
// Our BraintrustAutoConfigCustomizer adds the real span processor/exporter.
setPropertyIfAbsent("otel.traces.exporter", "none");
setPropertyIfAbsent("otel.metrics.exporter", "none");
setPropertyIfAbsent("otel.logs.exporter", "none");
log("Enabled OTel SDK autoconfiguration.");
}
/** Sets a system property only if it hasn't been set already (respects user overrides). */
private static void setPropertyIfAbsent(String key, String value) {
if (System.getProperty(key) == null) {
System.setProperty(key, value);
}
}
private static void log(String msg) {
System.out.println("[braintrust] " + msg);
}
private static void log(Throwable t) {
t.printStackTrace(System.err);
}
}