Minor changes to startup code (#5992)

* Minor changes to startup code

* Fix test
This commit is contained in:
Trask Stalnaker 2022-05-10 09:26:38 -07:00 committed by GitHub
parent 2fad192fc1
commit 8afa13eec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 28 deletions

View File

@ -28,12 +28,10 @@ import io.opentelemetry.javaagent.bootstrap.DefineClassHelper;
import io.opentelemetry.javaagent.bootstrap.InstrumentedTaskClasses;
import io.opentelemetry.javaagent.extension.AgentListener;
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesConfigurer;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.tooling.asyncannotationsupport.WeakRefAsyncOperationEndStrategies;
import io.opentelemetry.javaagent.tooling.bootstrap.BootstrapPackagesBuilderImpl;
import io.opentelemetry.javaagent.tooling.bootstrap.BootstrapPackagesConfigurer;
import io.opentelemetry.javaagent.tooling.config.AgentConfig;
import io.opentelemetry.javaagent.tooling.config.ConfigInitializer;
import io.opentelemetry.javaagent.tooling.ignore.IgnoredClassLoadersMatcher;
import io.opentelemetry.javaagent.tooling.ignore.IgnoredTypesBuilderImpl;
import io.opentelemetry.javaagent.tooling.ignore.IgnoredTypesMatcher;
@ -60,7 +58,7 @@ import net.bytebuddy.utility.JavaModule;
public class AgentInstaller {
private static final Logger logger;
private static final Logger logger = Logger.getLogger(AgentInstaller.class.getName());
static final String JAVAAGENT_ENABLED_CONFIG = "otel.javaagent.enabled";
static final String JAVAAGENT_NOOP_CONFIG = "otel.javaagent.experimental.use-noop-api";
@ -77,50 +75,33 @@ public class AgentInstaller {
private static final Map<String, List<Runnable>> CLASS_LOAD_CALLBACKS = new HashMap<>();
static {
LoggingConfigurer.configureLogger();
logger = Logger.getLogger(AgentInstaller.class.getName());
public static void installBytebuddyAgent(Instrumentation inst, Config config) {
addByteBuddyRawSetting();
// this needs to be done as early as possible - before the first Config.get() call
ConfigInitializer.initialize();
Integer strictContextStressorMillis = Integer.getInteger(STRICT_CONTEXT_STRESSOR_MILLIS);
if (strictContextStressorMillis != null) {
io.opentelemetry.context.ContextStorage.addWrapper(
storage -> new StrictContextStressor(storage, strictContextStressorMillis));
}
}
public static void installBytebuddyAgent(Instrumentation inst) {
logVersionInfo();
Config config = Config.get();
if (config.getBoolean(JAVAAGENT_ENABLED_CONFIG, true)) {
setupUnsafe(inst);
List<AgentListener> agentListeners = loadOrdered(AgentListener.class);
installBytebuddyAgent(inst, agentListeners);
installBytebuddyAgent(inst, config, agentListeners);
} else {
logger.fine("Tracing is disabled, not installing instrumentations.");
}
}
/**
* Install the core bytebuddy agent along with all implementations of {@link
* InstrumentationModule}.
*
* @param inst Java Instrumentation used to install bytebuddy
* @return the agent's class transformer
*/
public static ResettableClassFileTransformer installBytebuddyAgent(
Instrumentation inst, Iterable<AgentListener> agentListeners) {
private static void installBytebuddyAgent(
Instrumentation inst, Config config, Iterable<AgentListener> agentListeners) {
WeakRefAsyncOperationEndStrategies.initialize();
EmbeddedInstrumentationProperties.setPropertiesLoader(
AgentInitializer.getExtensionsClassLoader());
Config config = Config.get();
setBootstrapPackages(config);
setDefineClassHandler();
@ -192,8 +173,6 @@ public class AgentInstaller {
if (autoConfiguredSdk != null) {
runAfterAgentListeners(agentListeners, config, autoConfiguredSdk);
}
return resettableClassFileTransformer;
}
private static void setupUnsafe(Instrumentation inst) {

View File

@ -5,8 +5,10 @@
package io.opentelemetry.javaagent.tooling;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.javaagent.bootstrap.AgentInitializer;
import io.opentelemetry.javaagent.bootstrap.AgentStarter;
import io.opentelemetry.javaagent.tooling.config.ConfigInitializer;
import java.io.File;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
@ -62,7 +64,10 @@ public class AgentStarterImpl implements AgentStarter {
ClassLoader savedContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(extensionClassLoader);
AgentInstaller.installBytebuddyAgent(instrumentation);
// setting up logging and configuration need to be done as early as possible
LoggingConfigurer.configureLogger();
ConfigInitializer.initialize();
AgentInstaller.installBytebuddyAgent(instrumentation, Config.get());
} finally {
Thread.currentThread().setContextClassLoader(savedContextClassLoader);
}

View File

@ -5,6 +5,7 @@
package io.opentelemetry.javaagent.test
import io.opentelemetry.instrumentation.api.config.Config
import io.opentelemetry.javaagent.tooling.AgentInstaller
import io.opentelemetry.javaagent.tooling.HelperInjector
import io.opentelemetry.javaagent.tooling.Utils
@ -60,7 +61,7 @@ class HelperInjectionTest extends Specification {
def "helpers injected on bootstrap classloader"() {
setup:
ByteBuddyAgent.install()
AgentInstaller.installBytebuddyAgent(ByteBuddyAgent.getInstrumentation())
AgentInstaller.installBytebuddyAgent(ByteBuddyAgent.getInstrumentation(), Config.get())
String helperClassName = HelperInjectionTest.getPackage().getName() + '.HelperClass'
HelperInjector injector = new HelperInjector("test", [helperClassName], [], this.class.classLoader, ByteBuddyAgent.getInstrumentation())
URLClassLoader bootstrapChild = new URLClassLoader(new URL[0], (ClassLoader) null)