diff --git a/instrumentation/runtime-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/RuntimeMetricsInstaller.java b/instrumentation/runtime-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/RuntimeMetricsInstaller.java index c329ddda65..c5a799ecec 100644 --- a/instrumentation/runtime-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/RuntimeMetricsInstaller.java +++ b/instrumentation/runtime-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/RuntimeMetricsInstaller.java @@ -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(); } diff --git a/javaagent-spi/javaagent-spi.gradle b/javaagent-spi/javaagent-spi.gradle index b34463ba65..b313c84b71 100644 --- a/javaagent-spi/javaagent-spi.gradle +++ b/javaagent-spi/javaagent-spi.gradle @@ -7,4 +7,5 @@ dependencies { compileOnly deps.opentelemetrySdk compileOnly deps.opentelemetrySdkMetrics compileOnly deps.bytebuddy + compileOnly project(":instrumentation-api") } \ No newline at end of file diff --git a/javaagent-spi/src/main/java/io/opentelemetry/javaagent/spi/ComponentInstaller.java b/javaagent-spi/src/main/java/io/opentelemetry/javaagent/spi/ComponentInstaller.java index 343d86b5a2..d1f8a66b28 100644 --- a/javaagent-spi/src/main/java/io/opentelemetry/javaagent/spi/ComponentInstaller.java +++ b/javaagent-spi/src/main/java/io/opentelemetry/javaagent/spi/ComponentInstaller.java @@ -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) {} } diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentInstaller.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentInstaller.java index 53a2572df2..c57bb097f6 100644 --- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentInstaller.java +++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentInstaller.java @@ -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 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 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 componentInstallers) { + Iterable componentInstallers, Config config) { Thread.currentThread().setContextClassLoader(AgentInstaller.class.getClassLoader()); for (ComponentInstaller componentInstaller : componentInstallers) { - componentInstaller.beforeByteBuddyAgent(); + componentInstaller.beforeByteBuddyAgent(config); } } private static void installComponentsAfterByteBuddy( - Iterable componentInstallers) { + Iterable 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 componentInstallers; + private final Config config; protected InstallComponentAfterByteBuddyCallback( - Iterable componentInstallers) { + Config config, Iterable 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); } } } diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentTracerProviderConfigurer.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentTracerProviderConfigurer.java index c8dcc46e41..48f8aa4853 100644 --- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentTracerProviderConfigurer.java +++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentTracerProviderConfigurer.java @@ -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())) diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java index 4f6e8f515a..df801b4ebf 100644 --- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java +++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java @@ -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()