Let the ComponentInstaller be passed a Config instance (#2569)

* Let the ComponentInstaller be passed a Config instance, and prefer using Config over Properties

* revert span exporter factory and metric exporter factory and stick with the ol' properties for now.

* rollback
This commit is contained in:
jason plumb 2021-03-17 15:11:42 -07:00 committed by GitHub
parent fcc7004353
commit 6ea1e8d0ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 34 deletions

View File

@ -16,8 +16,8 @@ import java.util.Collections;
@AutoService(ComponentInstaller.class)
public class RuntimeMetricsInstaller implements ComponentInstaller {
@Override
public void afterByteBuddyAgent() {
if (Config.get().isInstrumentationEnabled(Collections.singleton("runtime-metrics"), true)) {
public void afterByteBuddyAgent(Config config) {
if (config.isInstrumentationEnabled(Collections.singleton("runtime-metrics"), true)) {
GarbageCollector.registerObservers();
MemoryPools.registerObservers();
}

View File

@ -7,4 +7,5 @@ dependencies {
compileOnly deps.opentelemetrySdk
compileOnly deps.opentelemetrySdkMetrics
compileOnly deps.bytebuddy
compileOnly project(":instrumentation-api")
}

View File

@ -5,6 +5,8 @@
package io.opentelemetry.javaagent.spi;
import io.opentelemetry.instrumentation.api.config.Config;
/**
* {@link ComponentInstaller} can be used to install any implementation providers that are used by
* instrumentations. For instance Java agent uses this to install OpenTelemetry SDK. The
@ -25,8 +27,8 @@ public interface ComponentInstaller {
* and InstrumentationContext falls back to the less performant Map implementation for those
* classes.
*/
default void beforeByteBuddyAgent() {}
default void beforeByteBuddyAgent(Config config) {}
/** Runs after instrumentations are added to ByteBuddy. */
default void afterByteBuddyAgent() {}
default void afterByteBuddyAgent(Config config) {}
}

View File

@ -80,8 +80,10 @@ public class AgentInstaller {
public static void installBytebuddyAgent(Instrumentation inst) {
logVersionInfo();
if (Config.get().getBooleanProperty(JAVAAGENT_ENABLED_CONFIG, true)) {
Config config = Config.get();
if (config.getBooleanProperty(JAVAAGENT_ENABLED_CONFIG, true)) {
Iterable<ComponentInstaller> componentInstallers = loadComponentProviders();
installComponentsBeforeByteBuddy(componentInstallers, config);
installBytebuddyAgent(inst, componentInstallers);
} else {
log.debug("Tracing is disabled, not installing instrumentations.");
@ -98,8 +100,6 @@ public class AgentInstaller {
public static ResettableClassFileTransformer installBytebuddyAgent(
Instrumentation inst, Iterable<ComponentInstaller> componentInstallers) {
installComponentsBeforeByteBuddy(componentInstallers);
INSTRUMENTATION = inst;
FieldBackedProvider.resetContextMatchers();
@ -122,10 +122,11 @@ public class AgentInstaller {
// .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED)
.ignore(any(), GlobalClassloaderIgnoresMatcher.skipClassLoader(ignoreMatcherProvider));
Config config = Config.get();
ignoredAgentBuilder =
ignoredAgentBuilder.or(
globalIgnoresMatcher(
Config.get().getBooleanProperty(ADDITIONAL_LIBRARY_IGNORES_ENABLED, true),
config.getBooleanProperty(ADDITIONAL_LIBRARY_IGNORES_ENABLED, true),
ignoreMatcherProvider));
ignoredAgentBuilder = ignoredAgentBuilder.or(matchesConfiguredExcludes());
@ -156,20 +157,20 @@ public class AgentInstaller {
agentBuilder = customizeByteBuddyAgent(agentBuilder);
log.debug("Installed {} instrumenter(s)", numInstrumenters);
ResettableClassFileTransformer resettableClassFileTransformer = agentBuilder.installOn(inst);
installComponentsAfterByteBuddy(componentInstallers);
installComponentsAfterByteBuddy(componentInstallers, config);
return resettableClassFileTransformer;
}
private static void installComponentsBeforeByteBuddy(
Iterable<ComponentInstaller> componentInstallers) {
Iterable<ComponentInstaller> componentInstallers, Config config) {
Thread.currentThread().setContextClassLoader(AgentInstaller.class.getClassLoader());
for (ComponentInstaller componentInstaller : componentInstallers) {
componentInstaller.beforeByteBuddyAgent();
componentInstaller.beforeByteBuddyAgent(config);
}
}
private static void installComponentsAfterByteBuddy(
Iterable<ComponentInstaller> componentInstallers) {
Iterable<ComponentInstaller> componentInstallers, Config config) {
/*
* java.util.logging.LogManager maintains a final static LogManager, which is created during class initialization.
*
@ -194,10 +195,10 @@ public class AgentInstaller {
log.debug("Custom logger detected. Delaying Agent Tracer initialization.");
registerClassLoadCallback(
"java.util.logging.LogManager",
new InstallComponentAfterByteBuddyCallback(componentInstallers));
new InstallComponentAfterByteBuddyCallback(config, componentInstallers));
} else {
for (ComponentInstaller componentInstaller : componentInstallers) {
componentInstaller.afterByteBuddyAgent();
componentInstaller.afterByteBuddyAgent(config);
}
}
}
@ -392,10 +393,12 @@ public class AgentInstaller {
protected static class InstallComponentAfterByteBuddyCallback extends ClassLoadCallBack {
private final Iterable<ComponentInstaller> componentInstallers;
private final Config config;
protected InstallComponentAfterByteBuddyCallback(
Iterable<ComponentInstaller> componentInstallers) {
Config config, Iterable<ComponentInstaller> componentInstallers) {
this.componentInstallers = componentInstallers;
this.config = config;
}
@Override
@ -406,7 +409,7 @@ public class AgentInstaller {
@Override
public void execute() {
for (ComponentInstaller componentInstaller : componentInstallers) {
componentInstaller.afterByteBuddyAgent();
componentInstaller.afterByteBuddyAgent(config);
}
}
}

View File

@ -25,7 +25,6 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Iterator;
import java.util.Properties;
import java.util.ServiceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -63,16 +62,16 @@ public class AgentTracerProviderConfigurer implements SdkTracerProviderConfigure
}
private static void maybeConfigureExporterJar(SdkTracerProviderBuilder sdkTracerProviderBuilder) {
String exporterJar = Config.get().getProperty(EXPORTER_JAR_CONFIG);
Config config = Config.get();
String exporterJar = config.getProperty(EXPORTER_JAR_CONFIG);
if (exporterJar == null) {
return;
}
Properties config = Config.get().asJavaProperties();
installExportersFromJar(exporterJar, config, sdkTracerProviderBuilder);
}
private static synchronized void installExportersFromJar(
String exporterJar, Properties config, SdkTracerProviderBuilder builder) {
String exporterJar, Config config, SdkTracerProviderBuilder builder) {
URL url;
try {
url = new File(exporterJar).toURI().toURL();
@ -117,18 +116,16 @@ public class AgentTracerProviderConfigurer implements SdkTracerProviderConfigure
}
private static void installSpanExporter(
SpanExporterFactory spanExporterFactory,
Properties config,
SdkTracerProviderBuilder builder) {
SpanExporter spanExporter = spanExporterFactory.fromConfig(config);
SpanExporterFactory spanExporterFactory, Config config, SdkTracerProviderBuilder builder) {
SpanExporter spanExporter = spanExporterFactory.fromConfig(config.asJavaProperties());
SpanProcessor spanProcessor = BatchSpanProcessor.builder(spanExporter).build();
builder.addSpanProcessor(spanProcessor);
log.info("Installed span exporter: " + spanExporter.getClass().getName());
}
private static void installMetricExporter(
MetricExporterFactory metricExporterFactory, Properties config) {
MetricExporter metricExporter = metricExporterFactory.fromConfig(config);
MetricExporterFactory metricExporterFactory, Config config) {
MetricExporter metricExporter = metricExporterFactory.fromConfig(config.asJavaProperties());
IntervalMetricReader.builder()
.setMetricExporter(metricExporter)
.setMetricProducers(Collections.singleton((SdkMeterProvider) GlobalMetricsProvider.get()))

View File

@ -23,15 +23,19 @@ public class OpenTelemetryInstaller implements ComponentInstaller {
static final String JAVAAGENT_ENABLED_CONFIG = "otel.javaagent.enabled";
@Override
public void beforeByteBuddyAgent() {
installAgentTracer();
public void beforeByteBuddyAgent(Config config) {
installAgentTracer(config);
}
/** Register agent tracer if no agent tracer is already registered. */
/**
* Register agent tracer if no agent tracer is already registered.
*
* @param config Configuration instance
*/
@SuppressWarnings("unused")
public static synchronized void installAgentTracer() {
if (Config.get().getBooleanProperty(JAVAAGENT_ENABLED_CONFIG, true)) {
copySystemProperties();
public static synchronized void installAgentTracer(Config config) {
if (config.getBooleanProperty(JAVAAGENT_ENABLED_CONFIG, true)) {
copySystemProperties(config);
OpenTelemetrySdk sdk = OpenTelemetrySdkAutoConfiguration.initialize();
OpenTelemetrySdkAccess.internalSetForceFlush(
@ -44,8 +48,8 @@ public class OpenTelemetryInstaller implements ComponentInstaller {
// OpenTelemetrySdkAutoConfiguration currently only supports configuration from environment. We
// massage any properties we have that aren't in the environment to system properties.
// TODO(anuraaga): Make this less hacky
private static void copySystemProperties() {
Properties allProperties = Config.get().asJavaProperties();
private static void copySystemProperties(Config config) {
Properties allProperties = config.asJavaProperties();
Properties environmentProperties =
new ConfigBuilder()
.readEnvironmentVariables()