Skip to content

Commit

Permalink
Make config file available in early agent initialization phase (#7550)
Browse files Browse the repository at this point in the history
Fixes #7540

I think this kind of early config might be useful in general -- for
instance, I think I'd like to use it for properties introduced in
#7339
  • Loading branch information
Mateusz Rzeszutek authored Jan 12, 2023
1 parent 09b63d2 commit 18cffb7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
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);
}
}

0 comments on commit 18cffb7

Please sign in to comment.