Skip to content

Make config file available in early agent initialization phase #7550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2023
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 @@ -16,7 +16,6 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextStorage;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import io.opentelemetry.instrumentation.api.internal.EmbeddedInstrumentationProperties;
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader;
import io.opentelemetry.javaagent.bootstrap.BootstrapPackagePrefixesHolder;
Expand All @@ -32,6 +31,7 @@
import io.opentelemetry.javaagent.tooling.bytebuddy.SafeTypeStrategy;
import io.opentelemetry.javaagent.tooling.config.AgentConfig;
import io.opentelemetry.javaagent.tooling.config.ConfigPropertiesBridge;
import io.opentelemetry.javaagent.tooling.config.EarlyInitAgentConfig;
import io.opentelemetry.javaagent.tooling.ignore.IgnoredClassLoadersMatcher;
import io.opentelemetry.javaagent.tooling.ignore.IgnoredTypesBuilderImpl;
import io.opentelemetry.javaagent.tooling.ignore.IgnoredTypesMatcher;
Expand Down Expand Up @@ -87,7 +87,8 @@ public static void installBytebuddyAgent(Instrumentation inst, ClassLoader exten
}

logVersionInfo();
if (ConfigPropertiesUtil.getBoolean(JAVAAGENT_ENABLED_CONFIG, true)) {
EarlyInitAgentConfig agentConfig = EarlyInitAgentConfig.create();
if (agentConfig.getBoolean(JAVAAGENT_ENABLED_CONFIG, true)) {
setupUnsafe(inst);
List<AgentListener> agentListeners = loadOrdered(AgentListener.class, extensionClassLoader);
installBytebuddyAgent(inst, extensionClassLoader, agentListeners);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ public final class ConfigurationFileLoader implements AutoConfigurationCustomize

static final String CONFIGURATION_FILE_PROPERTY = "otel.javaagent.configuration-file";

private static Map<String, String> configFileContents;

@Override
public void customize(AutoConfigurationCustomizer autoConfiguration) {
autoConfiguration.addPropertiesSupplier(ConfigurationFileLoader::loadConfigFile);
autoConfiguration.addPropertiesSupplier(ConfigurationFileLoader::getConfigFileContents);
}

static Map<String, String> getConfigFileContents() {
if (configFileContents == null) {
configFileContents = loadConfigFile();
}
return configFileContents;
}

// visible for tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.config;

import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import java.util.Map;

/**
* Agent config class that is only supposed to be used before the SDK (and {@link
* io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties}) is initialized.
*/
public final class EarlyInitAgentConfig {

public static EarlyInitAgentConfig create() {
return new EarlyInitAgentConfig(ConfigurationFileLoader.getConfigFileContents());
}

private final Map<String, String> configFileContents;

private EarlyInitAgentConfig(Map<String, String> configFileContents) {
this.configFileContents = configFileContents;
}

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