Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import java.util.List;

Expand All @@ -24,7 +25,7 @@ public ApplicationLoggingInstrumentationModule() {
public boolean defaultEnabled(ConfigProperties config) {
// only enable this instrumentation if the application logger is enabled
return super.defaultEnabled(config)
&& "application".equals(config.getString("otel.javaagent.logging"));
&& "application".equals(EarlyInitAgentConfig.get().getLogging());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.incubator.config.internal.DeclarativeConfigUtil;
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.extension.instrumentation.internal.AsmApi;
import io.opentelemetry.javaagent.instrumentation.instrumentationannotations.AnnotationExcludedMethods;
import io.opentelemetry.javaagent.instrumentation.kotlinxcoroutines.instrumentationannotations.SpanAttributeUtil.Parameter;
import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig;
import java.util.Arrays;
import java.util.List;
import kotlin.coroutines.Continuation;
Expand Down Expand Up @@ -58,8 +58,7 @@ class WithSpanInstrumentation implements TypeInstrumentation {
private static final boolean CHECK_CLASS =
DeclarativeConfigUtil.getInstrumentationConfig(
GlobalOpenTelemetry.get(), "kotlinx_coroutines")
.getBoolean(
"check_class", ConfigPropertiesUtil.getBoolean("otel.javaagent.debug", false));
.getBoolean("check_class", EarlyInitAgentConfig.get().isDebug());

private final ElementMatcher.Junction<AnnotationSource> annotatedMethodMatcher;
// this matcher matches all methods that should be excluded from transformation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ public String name() {
}

@Override
public void init(EarlyInitAgentConfig earlyConfig) {
int limit =
earlyConfig.getInt("otel.javaagent.logging.application.logs-buffer-max-records", 2048);
public void init() {
int limit = EarlyInitAgentConfig.get().getLoggingApplicationLogsBufferMaxRecords();
InMemoryLogStore inMemoryLogStore = new InMemoryLogStore(limit);
ApplicationLoggerFactory loggerFactory = new ApplicationLoggerFactory(inMemoryLogStore);
// register a shutdown hook that'll dump the logs to stderr in case something goes wrong
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public String name() {
}

@Override
public void init(EarlyInitAgentConfig earlyConfig) {
public void init() {
setSystemPropertyDefault(SIMPLE_LOGGER_SHOW_DATE_TIME_PROPERTY, "true");
setSystemPropertyDefault(
SIMPLE_LOGGER_DATE_TIME_FORMAT_PROPERTY, SIMPLE_LOGGER_DATE_TIME_FORMAT_DEFAULT);

if (earlyConfig.getBoolean("otel.javaagent.debug", false)) {
if (EarlyInitAgentConfig.get().isDebug()) {
setSystemPropertyDefault(SIMPLE_LOGGER_DEFAULT_LOG_LEVEL_PROPERTY, "DEBUG");
setSystemPropertyDefault(SIMPLE_LOGGER_PREFIX + "okhttp3.internal.http2", "INFO");
setSystemPropertyDefault(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public class AgentInstaller {

private static final Logger logger = Logger.getLogger(AgentInstaller.class.getName());

static final String JAVAAGENT_ENABLED_CONFIG = "otel.javaagent.enabled";

// This property may be set to force synchronous AgentListener#afterAgent() execution: the
// condition for delaying the AgentListener initialization is pretty broad and in case it covers
// too much javaagent users can file a bug, force sync execution by setting this property to true
Expand All @@ -96,8 +94,7 @@ public class AgentInstaller {

private static volatile boolean instrumentationInstalled;

public static void installBytebuddyAgent(
Instrumentation inst, ClassLoader extensionClassLoader, EarlyInitAgentConfig earlyConfig) {
public static void installBytebuddyAgent(Instrumentation inst, ClassLoader extensionClassLoader) {
addByteBuddyRawSetting();

Integer strictContextStressorMillis = Integer.getInteger(STRICT_CONTEXT_STRESSOR_MILLIS);
Expand All @@ -107,9 +104,9 @@ public static void installBytebuddyAgent(
}

logVersionInfo();
if (earlyConfig.getBoolean(JAVAAGENT_ENABLED_CONFIG, true)) {
if (EarlyInitAgentConfig.get().isEnabled()) {
List<AgentListener> agentListeners = loadOrdered(AgentListener.class, extensionClassLoader);
installBytebuddyAgent(inst, extensionClassLoader, agentListeners, earlyConfig);
installBytebuddyAgent(inst, extensionClassLoader, agentListeners);
} else {
logger.fine("Agent is disabled, not installing instrumentations.");
}
Expand All @@ -118,13 +115,12 @@ public static void installBytebuddyAgent(
private static void installBytebuddyAgent(
Instrumentation inst,
ClassLoader extensionClassLoader,
Iterable<AgentListener> agentListeners,
EarlyInitAgentConfig earlyConfig) {
Iterable<AgentListener> agentListeners) {

WeakRefAsyncOperationEndStrategies.initialize();
EmbeddedInstrumentationProperties.setPropertiesLoader(extensionClassLoader);
setDefineClassHandler();
FieldBackedImplementationConfiguration.configure(earlyConfig);
FieldBackedImplementationConfiguration.configure();
// preload ThreadLocalRandom to avoid occasional
// java.lang.ClassCircularityError: java/util/concurrent/ThreadLocalRandom
// see https://github.com/raphw/byte-buddy/issues/1666 and
Expand Down Expand Up @@ -163,7 +159,7 @@ private static void installBytebuddyAgent(
installEarlyInstrumentation(agentBuilder, inst);

AutoConfiguredOpenTelemetrySdk autoConfiguredSdk =
installOpenTelemetrySdk(extensionClassLoader, earlyConfig);
installOpenTelemetrySdk(extensionClassLoader);

ConfigProperties sdkConfig = AutoConfigureUtil.getConfig(autoConfiguredSdk);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ public boolean delayStart() {
public void start() {
installTransformers();

EarlyInitAgentConfig earlyConfig = EarlyInitAgentConfig.create();
extensionClassLoader = createExtensionClassLoader(getClass().getClassLoader(), earlyConfig);
extensionClassLoader = createExtensionClassLoader(getClass().getClassLoader());
// allows loading instrumenter customizers from agent and extensions
ServiceLoaderUtil.setLoadFunction(clazz -> ServiceLoader.load(clazz, extensionClassLoader));

String loggerImplementationName = earlyConfig.getString("otel.javaagent.logging");
String loggerImplementationName = EarlyInitAgentConfig.get().getLogging();
// default to the built-in stderr slf4j-simple logger
if (loggerImplementationName == null) {
loggerImplementationName = "simple";
Expand All @@ -98,10 +97,10 @@ public void start() {

Throwable startupError = null;
try {
loggingCustomizer.init(earlyConfig);
earlyConfig.logEarlyConfigErrorsIfAny();
loggingCustomizer.init();
EarlyInitAgentConfig.get().logEarlyConfigErrorsIfAny();

AgentInstaller.installBytebuddyAgent(instrumentation, extensionClassLoader, earlyConfig);
AgentInstaller.installBytebuddyAgent(instrumentation, extensionClassLoader);
WeakConcurrentMapCleaner.start();

// LazyStorage reads system properties. Initialize it here where we have permissions to avoid
Expand Down Expand Up @@ -141,10 +140,9 @@ public ClassLoader getExtensionClassLoader() {
return extensionClassLoader;
}

private ClassLoader createExtensionClassLoader(
ClassLoader agentClassLoader, EarlyInitAgentConfig earlyConfig) {
private ClassLoader createExtensionClassLoader(ClassLoader agentClassLoader) {
return ExtensionClassLoader.getInstance(
agentClassLoader, javaagentFile, isSecurityManagerSupportEnabled, earlyConfig);
agentClassLoader, javaagentFile, isSecurityManagerSupportEnabled);
}

private static class LaunchHelperClassFileTransformer implements ClassFileTransformer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.instrumentation.thread.internal.AddThreadDetailsSpanProcessor;
import io.opentelemetry.javaagent.tooling.config.AgentConfig;
import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
Expand Down Expand Up @@ -44,7 +44,7 @@ private static SdkTracerProviderBuilder configure(

private static void maybeEnableLoggingExporter(
SdkTracerProviderBuilder builder, ConfigProperties config) {
if (AgentConfig.isDebugModeEnabled(config)) {
if (EarlyInitAgentConfig.get().isDebug()) {
// don't install another instance if the user has already explicitly requested it.
if (loggingExporterIsNotAlreadyConfigured(config)) {
builder.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
// TODO find a way to initialize logging before using this class
@SuppressWarnings("SystemOut")
public class ExtensionClassLoader extends URLClassLoader {
public static final String EXTENSIONS_CONFIG = "otel.javaagent.extensions";

private final boolean isSecurityManagerSupportEnabled;

Expand All @@ -54,15 +53,12 @@ public class ExtensionClassLoader extends URLClassLoader {
}

public static ClassLoader getInstance(
ClassLoader parent,
File javaagentFile,
boolean isSecurityManagerSupportEnabled,
EarlyInitAgentConfig earlyConfig) {
ClassLoader parent, File javaagentFile, boolean isSecurityManagerSupportEnabled) {
List<URL> extensions = new ArrayList<>();

includeEmbeddedExtensionsIfFound(extensions, javaagentFile);

extensions.addAll(parseLocation(earlyConfig.getString(EXTENSIONS_CONFIG), javaagentFile));
extensions.addAll(parseLocation(EarlyInitAgentConfig.get().getExtensions(), javaagentFile));

// TODO when logging is configured add warning about deprecated property

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.opentelemetry.javaagent.tooling;

import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;

// only one LoggingCustomizer is allowed, and its presence will suppress the
Expand All @@ -22,7 +21,7 @@ public interface LoggingCustomizer {
// note that if this throws an exception, it will end up calling onStartupFailure, because
// otherwise that exception will bubble up to OpenTelemetryAgent where a distro cannot control the
// logging of it.
void init(EarlyInitAgentConfig earlyConfig);
void init();

/**
* Register a callback which will be called on synchronous startup success.
Expand All @@ -35,7 +34,7 @@ public interface LoggingCustomizer {

/**
* Register a callback which will be called on synchronous startup failure (including if {@link
* #init(EarlyInitAgentConfig)} fails).
* #init()} fails).
*
* <p>Synchronous startup may or may not include running {@link
* io.opentelemetry.javaagent.extension.AgentListener#afterAgent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.javaagent.tooling;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig;

@AutoService(LoggingCustomizer.class)
public final class NoopLoggingCustomizer implements LoggingCustomizer {
Expand All @@ -17,7 +16,7 @@ public String name() {
}

@Override
public void init(EarlyInitAgentConfig earlyConfig) {}
public void init() {}

@Override
@SuppressWarnings("SystemOut")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class OpenTelemetryInstaller {
* @return the {@link AutoConfiguredOpenTelemetrySdk}
*/
public static AutoConfiguredOpenTelemetrySdk installOpenTelemetrySdk(
ClassLoader extensionClassLoader, EarlyInitAgentConfig earlyConfig) {
ClassLoader extensionClassLoader) {

AutoConfiguredOpenTelemetrySdk autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
Expand All @@ -52,7 +52,7 @@ public static AutoConfiguredOpenTelemetrySdk installOpenTelemetrySdk(
// Provide a fake ConfigProperties until we have migrated all runtime configuration
// access to use declarative configuration API
configProvider = ((ExtendedOpenTelemetry) sdk).getConfigProvider();
configProperties = getDeclarativeConfigBridgedProperties(earlyConfig, configProvider);
configProperties = getDeclarativeConfigBridgedProperties(configProvider);
}

setForceFlush(sdk);
Expand All @@ -66,16 +66,15 @@ public static AutoConfiguredOpenTelemetrySdk installOpenTelemetrySdk(
}

// Visible for testing
static ConfigProperties getDeclarativeConfigBridgedProperties(
EarlyInitAgentConfig earlyConfig, ConfigProvider configProvider) {
static ConfigProperties getDeclarativeConfigBridgedProperties(ConfigProvider configProvider) {
return new DeclarativeConfigPropertiesBridgeBuilder()
.addMapping("otel.javaagent", "agent")
.addOverride("otel.instrumentation.common.default-enabled", defaultEnabled(configProvider))
// these properties are used to initialize the SDK before the configuration file
// is loaded for consistency, we pass them to the bridge, so that they can be read
// later with the same value from the {@link DeclarativeConfigPropertiesBridge}
.addOverride("otel.javaagent.debug", earlyConfig.getBoolean("otel.javaagent.debug", false))
.addOverride("otel.javaagent.logging", earlyConfig.getString("otel.javaagent.logging"))
.addOverride("otel.javaagent.debug", EarlyInitAgentConfig.get().isDebug())
.addOverride("otel.javaagent.logging", EarlyInitAgentConfig.get().getLogging())
.buildFromInstrumentationConfig(configProvider.getInstrumentationConfig());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
package io.opentelemetry.javaagent.tooling;

import com.google.auto.service.AutoService;
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import io.opentelemetry.instrumentation.logging.internal.AbstractSpanLoggingCustomizerProvider;
import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig;
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationCustomizerProvider;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;

Expand All @@ -17,8 +17,6 @@ public class SpanLoggingCustomizerProvider extends AbstractSpanLoggingCustomizer

@Override
protected boolean isEnabled(OpenTelemetryConfigurationModel model) {
// read from system properties as it's an early init property and the config bridge is not
// available here
return ConfigPropertiesUtil.getBoolean("otel.javaagent.debug", false);
return EarlyInitAgentConfig.get().isDebug();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.config.internal.DeclarativeConfigUtil;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;

public final class AgentConfig {

public static boolean isDebugModeEnabled(ConfigProperties config) {
return config.getBoolean("otel.javaagent.debug", false);
}

public static String instrumentationMode() {
String mode =
DeclarativeConfigUtil.getInstrumentationConfig(GlobalOpenTelemetry.get(), "agent")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
public final class EarlyInitAgentConfig {

public static EarlyInitAgentConfig create() {
return new EarlyInitAgentConfig(ConfigurationFile.getProperties());
private static final EarlyInitAgentConfig INSTANCE =
new EarlyInitAgentConfig(ConfigurationFile.getProperties());

public static EarlyInitAgentConfig get() {
return INSTANCE;
}

private final Map<String, String> configFileContents;
Expand All @@ -26,22 +29,48 @@ private EarlyInitAgentConfig(Map<String, String> configFileContents) {
}

@Nullable
public String getString(String propertyName) {
public String getLogging() {
return getString("otel.javaagent.logging");
}

@Nullable
public String getExtensions() {
return getString("otel.javaagent.extensions");
}

public boolean isDebug() {
return getBoolean("otel.javaagent.debug", false);
}

public boolean isEnabled() {
return getBoolean("otel.javaagent.enabled", true);
}

public boolean isExperimentalFieldInjectionEnabled() {
return getBoolean("otel.javaagent.experimental.field-injection.enabled", true);
}

public int getLoggingApplicationLogsBufferMaxRecords() {
return getInt("otel.javaagent.logging.application.logs-buffer-max-records", 2048);
}

@Nullable
private String getString(String propertyName) {
String value = ConfigPropertiesUtil.getString(propertyName);
if (value != null) {
return value;
}
return configFileContents.get(propertyName);
}

public boolean getBoolean(String propertyName, boolean defaultValue) {
private boolean getBoolean(String propertyName, boolean defaultValue) {
String configFileValueStr = configFileContents.get(propertyName);
boolean configFileValue =
configFileValueStr == null ? defaultValue : Boolean.parseBoolean(configFileValueStr);
return ConfigPropertiesUtil.getBoolean(propertyName, configFileValue);
}

public int getInt(String propertyName, int defaultValue) {
private int getInt(String propertyName, int defaultValue) {
try {
String configFileValueStr = configFileContents.get(propertyName);
int configFileValue =
Expand Down
Loading
Loading