Skip to content

Commit

Permalink
Add an SPI for customizing Config just before it's set
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed May 11, 2022
1 parent d1ee692 commit 22a43fb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,12 @@ private <T> T getTypedProperty(String name, ConfigValueParser<T> parser) {
private String getRawProperty(String name, String defaultValue) {
return getAllProperties().getOrDefault(NamingConvention.DOT.normalize(name), defaultValue);
}

/**
* Returns a new {@link ConfigBuilder} instance populated with the properties of this {@link
* Config}.
*/
public ConfigBuilder toBuilder() {
return new ConfigBuilder(getAllProperties());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
/** A builder of a {@link Config}. */
public final class ConfigBuilder {

private final Map<String, String> allProperties = new HashMap<>();
private final Map<String, String> allProperties;

/** Constructs a new {@link ConfigBuilder}. */
ConfigBuilder() {}
ConfigBuilder() {
allProperties = new HashMap<>();
}

ConfigBuilder(Map<String, String> propertiesToCopy) {
allProperties = new HashMap<>(propertiesToCopy);
}

/** Adds a single property to the config. */
public ConfigBuilder addProperty(String name, @Nullable String value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.config;

import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.javaagent.extension.Ordered;

/**
* Internal SPI that allows to customize the config just before it is set as the global.
*
* <p>This is a service provider interface that requires implementations to be registered in a
* provider-configuration file stored in the {@code META-INF/services} resource directory.
*/
public interface ConfigCustomizer extends Ordered {

Config customize(Config config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public final class ConfigInitializer {
static final String CONFIGURATION_FILE_ENV_VAR = "OTEL_JAVAAGENT_CONFIGURATION_FILE";

public static void initialize() {
Config.internalInitializeConfig(create(loadSpiConfiguration(), loadConfigurationFile()));
Config config = create(loadSpiConfiguration(), loadConfigurationFile());
for (ConfigCustomizer customizer : loadOrdered(ConfigCustomizer.class)) {
config = customizer.customize(config);
}
Config.internalInitializeConfig(config);
}

// visible for testing
Expand Down

0 comments on commit 22a43fb

Please sign in to comment.