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) @AutoService(ComponentInstaller.class)
public class RuntimeMetricsInstaller implements ComponentInstaller { public class RuntimeMetricsInstaller implements ComponentInstaller {
@Override @Override
public void afterByteBuddyAgent() { public void afterByteBuddyAgent(Config config) {
if (Config.get().isInstrumentationEnabled(Collections.singleton("runtime-metrics"), true)) { if (config.isInstrumentationEnabled(Collections.singleton("runtime-metrics"), true)) {
GarbageCollector.registerObservers(); GarbageCollector.registerObservers();
MemoryPools.registerObservers(); MemoryPools.registerObservers();
} }

View File

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

View File

@ -5,6 +5,8 @@
package io.opentelemetry.javaagent.spi; 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 * {@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 * 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 * and InstrumentationContext falls back to the less performant Map implementation for those
* classes. * classes.
*/ */
default void beforeByteBuddyAgent() {} default void beforeByteBuddyAgent(Config config) {}
/** Runs after instrumentations are added to ByteBuddy. */ /** 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) { public static void installBytebuddyAgent(Instrumentation inst) {
logVersionInfo(); logVersionInfo();
if (Config.get().getBooleanProperty(JAVAAGENT_ENABLED_CONFIG, true)) { Config config = Config.get();
if (config.getBooleanProperty(JAVAAGENT_ENABLED_CONFIG, true)) {
Iterable<ComponentInstaller> componentInstallers = loadComponentProviders(); Iterable<ComponentInstaller> componentInstallers = loadComponentProviders();
installComponentsBeforeByteBuddy(componentInstallers, config);
installBytebuddyAgent(inst, componentInstallers); installBytebuddyAgent(inst, componentInstallers);
} else { } else {
log.debug("Tracing is disabled, not installing instrumentations."); log.debug("Tracing is disabled, not installing instrumentations.");
@ -98,8 +100,6 @@ public class AgentInstaller {
public static ResettableClassFileTransformer installBytebuddyAgent( public static ResettableClassFileTransformer installBytebuddyAgent(
Instrumentation inst, Iterable<ComponentInstaller> componentInstallers) { Instrumentation inst, Iterable<ComponentInstaller> componentInstallers) {
installComponentsBeforeByteBuddy(componentInstallers);
INSTRUMENTATION = inst; INSTRUMENTATION = inst;
FieldBackedProvider.resetContextMatchers(); FieldBackedProvider.resetContextMatchers();
@ -122,10 +122,11 @@ public class AgentInstaller {
// .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED) // .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED)
.ignore(any(), GlobalClassloaderIgnoresMatcher.skipClassLoader(ignoreMatcherProvider)); .ignore(any(), GlobalClassloaderIgnoresMatcher.skipClassLoader(ignoreMatcherProvider));
Config config = Config.get();
ignoredAgentBuilder = ignoredAgentBuilder =
ignoredAgentBuilder.or( ignoredAgentBuilder.or(
globalIgnoresMatcher( globalIgnoresMatcher(
Config.get().getBooleanProperty(ADDITIONAL_LIBRARY_IGNORES_ENABLED, true), config.getBooleanProperty(ADDITIONAL_LIBRARY_IGNORES_ENABLED, true),
ignoreMatcherProvider)); ignoreMatcherProvider));
ignoredAgentBuilder = ignoredAgentBuilder.or(matchesConfiguredExcludes()); ignoredAgentBuilder = ignoredAgentBuilder.or(matchesConfiguredExcludes());
@ -156,20 +157,20 @@ public class AgentInstaller {
agentBuilder = customizeByteBuddyAgent(agentBuilder); agentBuilder = customizeByteBuddyAgent(agentBuilder);
log.debug("Installed {} instrumenter(s)", numInstrumenters); log.debug("Installed {} instrumenter(s)", numInstrumenters);
ResettableClassFileTransformer resettableClassFileTransformer = agentBuilder.installOn(inst); ResettableClassFileTransformer resettableClassFileTransformer = agentBuilder.installOn(inst);
installComponentsAfterByteBuddy(componentInstallers); installComponentsAfterByteBuddy(componentInstallers, config);
return resettableClassFileTransformer; return resettableClassFileTransformer;
} }
private static void installComponentsBeforeByteBuddy( private static void installComponentsBeforeByteBuddy(
Iterable<ComponentInstaller> componentInstallers) { Iterable<ComponentInstaller> componentInstallers, Config config) {
Thread.currentThread().setContextClassLoader(AgentInstaller.class.getClassLoader()); Thread.currentThread().setContextClassLoader(AgentInstaller.class.getClassLoader());
for (ComponentInstaller componentInstaller : componentInstallers) { for (ComponentInstaller componentInstaller : componentInstallers) {
componentInstaller.beforeByteBuddyAgent(); componentInstaller.beforeByteBuddyAgent(config);
} }
} }
private static void installComponentsAfterByteBuddy( 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. * 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."); log.debug("Custom logger detected. Delaying Agent Tracer initialization.");
registerClassLoadCallback( registerClassLoadCallback(
"java.util.logging.LogManager", "java.util.logging.LogManager",
new InstallComponentAfterByteBuddyCallback(componentInstallers)); new InstallComponentAfterByteBuddyCallback(config, componentInstallers));
} else { } else {
for (ComponentInstaller componentInstaller : componentInstallers) { for (ComponentInstaller componentInstaller : componentInstallers) {
componentInstaller.afterByteBuddyAgent(); componentInstaller.afterByteBuddyAgent(config);
} }
} }
} }
@ -392,10 +393,12 @@ public class AgentInstaller {
protected static class InstallComponentAfterByteBuddyCallback extends ClassLoadCallBack { protected static class InstallComponentAfterByteBuddyCallback extends ClassLoadCallBack {
private final Iterable<ComponentInstaller> componentInstallers; private final Iterable<ComponentInstaller> componentInstallers;
private final Config config;
protected InstallComponentAfterByteBuddyCallback( protected InstallComponentAfterByteBuddyCallback(
Iterable<ComponentInstaller> componentInstallers) { Config config, Iterable<ComponentInstaller> componentInstallers) {
this.componentInstallers = componentInstallers; this.componentInstallers = componentInstallers;
this.config = config;
} }
@Override @Override
@ -406,7 +409,7 @@ public class AgentInstaller {
@Override @Override
public void execute() { public void execute() {
for (ComponentInstaller componentInstaller : componentInstallers) { 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.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.Properties;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -63,16 +62,16 @@ public class AgentTracerProviderConfigurer implements SdkTracerProviderConfigure
} }
private static void maybeConfigureExporterJar(SdkTracerProviderBuilder sdkTracerProviderBuilder) { 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) { if (exporterJar == null) {
return; return;
} }
Properties config = Config.get().asJavaProperties();
installExportersFromJar(exporterJar, config, sdkTracerProviderBuilder); installExportersFromJar(exporterJar, config, sdkTracerProviderBuilder);
} }
private static synchronized void installExportersFromJar( private static synchronized void installExportersFromJar(
String exporterJar, Properties config, SdkTracerProviderBuilder builder) { String exporterJar, Config config, SdkTracerProviderBuilder builder) {
URL url; URL url;
try { try {
url = new File(exporterJar).toURI().toURL(); url = new File(exporterJar).toURI().toURL();
@ -117,18 +116,16 @@ public class AgentTracerProviderConfigurer implements SdkTracerProviderConfigure
} }
private static void installSpanExporter( private static void installSpanExporter(
SpanExporterFactory spanExporterFactory, SpanExporterFactory spanExporterFactory, Config config, SdkTracerProviderBuilder builder) {
Properties config, SpanExporter spanExporter = spanExporterFactory.fromConfig(config.asJavaProperties());
SdkTracerProviderBuilder builder) {
SpanExporter spanExporter = spanExporterFactory.fromConfig(config);
SpanProcessor spanProcessor = BatchSpanProcessor.builder(spanExporter).build(); SpanProcessor spanProcessor = BatchSpanProcessor.builder(spanExporter).build();
builder.addSpanProcessor(spanProcessor); builder.addSpanProcessor(spanProcessor);
log.info("Installed span exporter: " + spanExporter.getClass().getName()); log.info("Installed span exporter: " + spanExporter.getClass().getName());
} }
private static void installMetricExporter( private static void installMetricExporter(
MetricExporterFactory metricExporterFactory, Properties config) { MetricExporterFactory metricExporterFactory, Config config) {
MetricExporter metricExporter = metricExporterFactory.fromConfig(config); MetricExporter metricExporter = metricExporterFactory.fromConfig(config.asJavaProperties());
IntervalMetricReader.builder() IntervalMetricReader.builder()
.setMetricExporter(metricExporter) .setMetricExporter(metricExporter)
.setMetricProducers(Collections.singleton((SdkMeterProvider) GlobalMetricsProvider.get())) .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"; static final String JAVAAGENT_ENABLED_CONFIG = "otel.javaagent.enabled";
@Override @Override
public void beforeByteBuddyAgent() { public void beforeByteBuddyAgent(Config config) {
installAgentTracer(); 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") @SuppressWarnings("unused")
public static synchronized void installAgentTracer() { public static synchronized void installAgentTracer(Config config) {
if (Config.get().getBooleanProperty(JAVAAGENT_ENABLED_CONFIG, true)) { if (config.getBooleanProperty(JAVAAGENT_ENABLED_CONFIG, true)) {
copySystemProperties(); copySystemProperties(config);
OpenTelemetrySdk sdk = OpenTelemetrySdkAutoConfiguration.initialize(); OpenTelemetrySdk sdk = OpenTelemetrySdkAutoConfiguration.initialize();
OpenTelemetrySdkAccess.internalSetForceFlush( OpenTelemetrySdkAccess.internalSetForceFlush(
@ -44,8 +48,8 @@ public class OpenTelemetryInstaller implements ComponentInstaller {
// OpenTelemetrySdkAutoConfiguration currently only supports configuration from environment. We // OpenTelemetrySdkAutoConfiguration currently only supports configuration from environment. We
// massage any properties we have that aren't in the environment to system properties. // massage any properties we have that aren't in the environment to system properties.
// TODO(anuraaga): Make this less hacky // TODO(anuraaga): Make this less hacky
private static void copySystemProperties() { private static void copySystemProperties(Config config) {
Properties allProperties = Config.get().asJavaProperties(); Properties allProperties = config.asJavaProperties();
Properties environmentProperties = Properties environmentProperties =
new ConfigBuilder() new ConfigBuilder()
.readEnvironmentVariables() .readEnvironmentVariables()