diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/config/Config.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/config/Config.java index 726df2d0b60a..322a079ee3e6 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/config/Config.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/config/Config.java @@ -13,7 +13,6 @@ import java.time.Duration; import java.util.List; import java.util.Map; -import java.util.Properties; import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -96,7 +95,10 @@ public String getString(String name, String defaultValue) { /** * Returns a boolean-valued configuration property or {@code null} if a property with name {@code * name} has not been configured. + * + * @deprecated Use the {@link #getBoolean(String, boolean)} variant instead. */ + @Deprecated @Nullable public Boolean getBoolean(String name) { return getTypedProperty(name, ConfigValueParsers::parseBoolean); @@ -115,7 +117,9 @@ public boolean getBoolean(String name, boolean defaultValue) { * name} has not been configured. * * @throws ConfigParsingException if the property is not a valid integer. + * @deprecated Use the {@link #getInt(String, int)} variant instead. */ + @Deprecated @Nullable public Integer getInt(String name) { return getTypedProperty(name, ConfigValueParsers::parseInt); @@ -135,7 +139,9 @@ public int getInt(String name, int defaultValue) { * name} has not been configured. * * @throws ConfigParsingException if the property is not a valid long. + * @deprecated Use the {@link #getLong(String, long)} variant instead. */ + @Deprecated @Nullable public Long getLong(String name) { return getTypedProperty(name, ConfigValueParsers::parseLong); @@ -155,7 +161,9 @@ public long getLong(String name, long defaultValue) { * name} has not been configured. * * @throws ConfigParsingException if the property is not a valid long. + * @deprecated Use the {@link #getDouble(String, double)} variant instead. */ + @Deprecated @Nullable public Double getDouble(String name) { return getTypedProperty(name, ConfigValueParsers::parseDouble); @@ -187,7 +195,9 @@ public double getDouble(String name, double defaultValue) { *

If no unit is specified, milliseconds is the assumed duration unit. * * @throws ConfigParsingException if the property is not a valid long. + * @deprecated Use the {@link #getDuration(String, Duration)} variant instead. */ + @Deprecated @Nullable public Duration getDuration(String name) { return getTypedProperty(name, ConfigValueParsers::parseDuration); @@ -218,7 +228,10 @@ public Duration getDuration(String name, Duration defaultValue) { * Returns a list-valued configuration property or an empty list if a property with name {@code * name} has not been configured. The format of the original value must be comma-separated, e.g. * {@code one,two,three}. + * + * @deprecated Use the {@link #getList(String, List)} variant instead. */ + @Deprecated public List getList(String name) { List list = getTypedProperty(name, ConfigValueParsers::parseList); return list == null ? emptyList() : list; @@ -240,7 +253,9 @@ public List getList(String name, List defaultValue) { * key=value,anotherKey=anotherValue}. * * @throws ConfigParsingException if the property is not a valid long. + * @deprecated Use the {@link #getMap(String, Map)} variant instead. */ + @Deprecated public Map getMap(String name) { Map map = getTypedProperty(name, ConfigValueParsers::parseMap); return map == null ? emptyMap() : map; @@ -305,16 +320,4 @@ public boolean isInstrumentationPropertyEnabled( public boolean isAgentDebugEnabled() { return getBoolean("otel.javaagent.debug", false); } - - /** - * Converts this config instance to Java {@link Properties}. - * - * @deprecated Use {@link #getAllProperties()} instead. - */ - @Deprecated - public Properties asJavaProperties() { - Properties properties = new Properties(); - properties.putAll(getAllProperties()); - return properties; - } } diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/PeerServiceAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/PeerServiceAttributesExtractor.java index 884036e38ae9..c9d613e4ea18 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/PeerServiceAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/PeerServiceAttributesExtractor.java @@ -5,6 +5,8 @@ package io.opentelemetry.instrumentation.api.instrumenter; +import static java.util.Collections.emptyMap; + import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.instrumentation.api.config.Config; import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesExtractor; @@ -24,7 +26,7 @@ public final class PeerServiceAttributesExtractor implements AttributesExtractor { private static final Map JAVAAGENT_PEER_SERVICE_MAPPING = - Config.get().getMap("otel.instrumentation.common.peer-service-mapping"); + Config.get().getMap("otel.instrumentation.common.peer-service-mapping", emptyMap()); private final Map peerServiceMapping; private final NetClientAttributesExtractor netClientAttributesExtractor; diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/CapturedHttpHeaders.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/CapturedHttpHeaders.java index a6fcd2251373..b3f3f311efd2 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/CapturedHttpHeaders.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/CapturedHttpHeaders.java @@ -53,9 +53,11 @@ public static CapturedHttpHeaders client(Config config) { // fall back to the experimental properties if the stable one isn't supplied return CapturedHttpHeaders.create( config.getList( - CLIENT_REQUEST_PROPERTY, config.getList(EXPERIMENTAL_CLIENT_REQUEST_PROPERTY)), + CLIENT_REQUEST_PROPERTY, + config.getList(EXPERIMENTAL_CLIENT_REQUEST_PROPERTY, emptyList())), config.getList( - CLIENT_RESPONSE_PROPERTY, config.getList(EXPERIMENTAL_CLIENT_RESPONSE_PROPERTY))); + CLIENT_RESPONSE_PROPERTY, + config.getList(EXPERIMENTAL_CLIENT_RESPONSE_PROPERTY, emptyList()))); } private static final String SERVER_REQUEST_PROPERTY = @@ -77,9 +79,11 @@ public static CapturedHttpHeaders server(Config config) { // fall back to the experimental properties if the stable one isn't supplied return CapturedHttpHeaders.create( config.getList( - SERVER_REQUEST_PROPERTY, config.getList(EXPERIMENTAL_SERVER_REQUEST_PROPERTY)), + SERVER_REQUEST_PROPERTY, + config.getList(EXPERIMENTAL_SERVER_REQUEST_PROPERTY, emptyList())), config.getList( - SERVER_RESPONSE_PROPERTY, config.getList(EXPERIMENTAL_SERVER_RESPONSE_PROPERTY))); + SERVER_RESPONSE_PROPERTY, + config.getList(EXPERIMENTAL_SERVER_RESPONSE_PROPERTY, emptyList()))); } /** diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/net/NetPeerAttributes.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/net/NetPeerAttributes.java index 625b44bced10..79864c50bb54 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/net/NetPeerAttributes.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/net/NetPeerAttributes.java @@ -5,6 +5,8 @@ package io.opentelemetry.instrumentation.api.tracer.net; +import static java.util.Collections.emptyMap; + import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.instrumentation.api.config.Config; @@ -12,7 +14,6 @@ import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.util.Collections; import java.util.Map; import javax.annotation.Nullable; @@ -28,12 +29,12 @@ public final class NetPeerAttributes { public static final NetPeerAttributes INSTANCE = new NetPeerAttributes( - Config.get().getMap("otel.instrumentation.common.peer-service-mapping")); + Config.get().getMap("otel.instrumentation.common.peer-service-mapping", emptyMap())); private final Map peerServiceMapping; public NetPeerAttributes() { - this(Collections.emptyMap()); + this(emptyMap()); } public NetPeerAttributes(Map peerServiceMapping) { diff --git a/instrumentation/executors/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/javaconcurrent/AbstractExecutorInstrumentation.java b/instrumentation/executors/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/javaconcurrent/AbstractExecutorInstrumentation.java index 5fbfe3a95482..e25ff12f1fbc 100644 --- a/instrumentation/executors/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/javaconcurrent/AbstractExecutorInstrumentation.java +++ b/instrumentation/executors/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/javaconcurrent/AbstractExecutorInstrumentation.java @@ -6,6 +6,7 @@ package io.opentelemetry.javaagent.instrumentation.javaconcurrent; import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; +import static java.util.Collections.emptyList; import static net.bytebuddy.matcher.ElementMatchers.any; import static net.bytebuddy.matcher.ElementMatchers.named; @@ -99,7 +100,7 @@ protected AbstractExecutorInstrumentation() { "scala.concurrent.impl.ExecutionContextImpl", }; Set combined = new HashSet<>(Arrays.asList(includeExecutors)); - combined.addAll(Config.get().getList(EXECUTORS_INCLUDE_PROPERTY_NAME)); + combined.addAll(Config.get().getList(EXECUTORS_INCLUDE_PROPERTY_NAME, emptyList())); this.includeExecutors = Collections.unmodifiableSet(combined); String[] includePrefixes = {"slick.util.AsyncExecutor$"}; diff --git a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletRequestParametersExtractor.java b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletRequestParametersExtractor.java index 683c192c9fc0..28b499aa733d 100644 --- a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletRequestParametersExtractor.java +++ b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletRequestParametersExtractor.java @@ -5,6 +5,8 @@ package io.opentelemetry.javaagent.instrumentation.servlet; +import static java.util.Collections.emptyList; + import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.instrumentation.api.config.Config; @@ -20,7 +22,9 @@ public class ServletRequestParametersExtractor implements AttributesExtractor< ServletRequestContext, ServletResponseContext> { private static final List CAPTURE_REQUEST_PARAMETERS = - Config.get().getList("otel.instrumentation.servlet.experimental.capture-request-parameters"); + Config.get() + .getList( + "otel.instrumentation.servlet.experimental.capture-request-parameters", emptyList()); private static final ConcurrentMap>> parameterKeysCache = new ConcurrentHashMap<>(); diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigPropertiesAdapter.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigPropertiesAdapter.java deleted file mode 100644 index 5a467290536a..000000000000 --- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigPropertiesAdapter.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * 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.instrumentation.api.config.ConfigParsingException; -import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; -import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; -import java.time.Duration; -import java.util.List; -import java.util.Map; -import javax.annotation.Nullable; - -public final class ConfigPropertiesAdapter implements ConfigProperties { - private final Config config; - - public ConfigPropertiesAdapter(Config config) { - this.config = config; - } - - @Nullable - @Override - public String getString(String name) { - return config.getString(name); - } - - @Nullable - @Override - public Boolean getBoolean(String name) { - return config.getBoolean(name); - } - - @Nullable - @Override - public Integer getInt(String name) { - try { - return config.getInt(name); - } catch (ConfigParsingException e) { - throw new ConfigurationException(e.getMessage(), e); - } - } - - @Nullable - @Override - public Long getLong(String name) { - try { - return config.getLong(name); - } catch (ConfigParsingException e) { - throw new ConfigurationException(e.getMessage(), e); - } - } - - @Nullable - @Override - public Double getDouble(String name) { - try { - return config.getDouble(name); - } catch (ConfigParsingException e) { - throw new ConfigurationException(e.getMessage(), e); - } - } - - @Nullable - @Override - public Duration getDuration(String name) { - try { - return config.getDuration(name); - } catch (ConfigParsingException e) { - throw new ConfigurationException(e.getMessage(), e); - } - } - - @Override - public List getList(String name) { - return config.getList(name); - } - - @Override - public Map getMap(String name) { - try { - return config.getMap(name); - } catch (ConfigParsingException e) { - throw new ConfigurationException(e.getMessage(), e); - } - } -} diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ignore/UserExcludedClassesConfigurer.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ignore/UserExcludedClassesConfigurer.java index c69c166aea57..005230bd75ce 100644 --- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ignore/UserExcludedClassesConfigurer.java +++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ignore/UserExcludedClassesConfigurer.java @@ -5,6 +5,8 @@ package io.opentelemetry.javaagent.tooling.ignore; +import static java.util.Collections.emptyList; + import com.google.auto.service.AutoService; import io.opentelemetry.instrumentation.api.config.Config; import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesBuilder; @@ -19,7 +21,7 @@ public class UserExcludedClassesConfigurer implements IgnoredTypesConfigurer { @Override public void configure(Config config, IgnoredTypesBuilder builder) { - List excludedClasses = config.getList(EXCLUDED_CLASSES_CONFIG); + List excludedClasses = config.getList(EXCLUDED_CLASSES_CONFIG, emptyList()); for (String excludedClass : excludedClasses) { excludedClass = excludedClass.trim(); // remove the trailing * diff --git a/javaagent-tooling/src/test/java/io/opentelemetry/javaagent/tooling/config/ConfigPropertiesAdapterTest.java b/javaagent-tooling/src/test/java/io/opentelemetry/javaagent/tooling/config/ConfigPropertiesAdapterTest.java deleted file mode 100644 index 081c652509fc..000000000000 --- a/javaagent-tooling/src/test/java/io/opentelemetry/javaagent/tooling/config/ConfigPropertiesAdapterTest.java +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.tooling.config; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.assertj.core.api.Assertions.entry; - -import io.opentelemetry.instrumentation.api.config.Config; -import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; -import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; -import java.time.Duration; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import org.junit.jupiter.api.Test; - -// Copied from -// https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/ConfigPropertiesTest.java -class ConfigPropertiesAdapterTest { - - @Test - void allValid() { - Map properties = new HashMap<>(); - properties.put("string", "str"); - properties.put("int", "10"); - properties.put("long", "20"); - properties.put("double", "5.4"); - properties.put("list", "cat,dog,bear"); - properties.put("map", "cat=meow,dog=bark,bear=growl"); - properties.put("mapWithEmptyValue", "cat=meow,dog=,bear=growl"); - properties.put("duration", "1s"); - - ConfigProperties config = createConfig(properties); - assertThat(config.getString("string")).isEqualTo("str"); - assertThat(config.getInt("int")).isEqualTo(10); - assertThat(config.getLong("long")).isEqualTo(20); - assertThat(config.getDouble("double")).isEqualTo(5.4); - assertThat(config.getList("list")).containsExactly("cat", "dog", "bear"); - assertThat(config.getMap("map")) - .containsExactly(entry("cat", "meow"), entry("dog", "bark"), entry("bear", "growl")); - assertThat(config.getMap("mapWithEmptyValue")) - .containsExactly(entry("cat", "meow"), entry("dog", ""), entry("bear", "growl")); - assertThat(config.getDuration("duration")).isEqualTo(Duration.ofSeconds(1)); - } - - @Test - void allMissing() { - ConfigProperties config = createConfig(Collections.emptyMap()); - assertThat(config.getString("string")).isNull(); - assertThat(config.getInt("int")).isNull(); - assertThat(config.getLong("long")).isNull(); - assertThat(config.getDouble("double")).isNull(); - assertThat(config.getList("list")).isEmpty(); - assertThat(config.getMap("map")).isEmpty(); - assertThat(config.getDuration("duration")).isNull(); - } - - @Test - void allEmpty() { - Map properties = new HashMap<>(); - properties.put("string", ""); - properties.put("int", ""); - properties.put("long", ""); - properties.put("double", ""); - properties.put("list", ""); - properties.put("map", ""); - properties.put("duration", ""); - - ConfigProperties config = createConfig(properties); - assertThat(config.getString("string")).isEmpty(); - assertThat(config.getInt("int")).isNull(); - assertThat(config.getLong("long")).isNull(); - assertThat(config.getDouble("double")).isNull(); - assertThat(config.getList("list")).isEmpty(); - assertThat(config.getMap("map")).isEmpty(); - assertThat(config.getDuration("duration")).isNull(); - } - - @Test - void invalidInt() { - assertThatThrownBy(() -> createConfig(Collections.singletonMap("int", "bar")).getInt("int")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid value for property int=bar. Must be a integer."); - assertThatThrownBy( - () -> createConfig(Collections.singletonMap("int", "999999999999999")).getInt("int")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid value for property int=999999999999999. Must be a integer."); - } - - @Test - void invalidLong() { - assertThatThrownBy(() -> createConfig(Collections.singletonMap("long", "bar")).getLong("long")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid value for property long=bar. Must be a long."); - assertThatThrownBy( - () -> - createConfig(Collections.singletonMap("long", "99223372036854775807")) - .getLong("long")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid value for property long=99223372036854775807. Must be a long."); - } - - @Test - void invalidDouble() { - assertThatThrownBy( - () -> createConfig(Collections.singletonMap("double", "bar")).getDouble("double")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid value for property double=bar. Must be a double."); - assertThatThrownBy( - () -> createConfig(Collections.singletonMap("double", "1.0.1")).getDouble("double")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid value for property double=1.0.1. Must be a double."); - } - - @Test - void uncleanList() { - assertThat( - createConfig(Collections.singletonMap("list", " a ,b,c , d,, ,")).getList("list")) - .containsExactly("a", "b", "c", "d"); - } - - @Test - void uncleanMap() { - assertThat( - createConfig(Collections.singletonMap("map", " a=1 ,b=2,c = 3 , d= 4,, ,")) - .getMap("map")) - .containsExactly(entry("a", "1"), entry("b", "2"), entry("c", "3"), entry("d", "4")); - } - - @Test - void invalidMap() { - assertThatThrownBy(() -> createConfig(Collections.singletonMap("map", "a=1,b")).getMap("map")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid map property: map=a=1,b"); - assertThatThrownBy(() -> createConfig(Collections.singletonMap("map", "a=1,=b")).getMap("map")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid map property: map=a=1,=b"); - } - - @Test - void invalidDuration() { - assertThatThrownBy( - () -> - createConfig(Collections.singletonMap("duration", "1a1ms")).getDuration("duration")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid duration property duration=1a1ms. Expected number, found: 1a1"); - assertThatThrownBy( - () -> createConfig(Collections.singletonMap("duration", "9mm")).getDuration("duration")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid duration property duration=9mm. Invalid duration string, found: mm"); - } - - @Test - void durationUnitParsing() { - assertThat(createConfig(Collections.singletonMap("duration", "1")).getDuration("duration")) - .isEqualTo(Duration.ofMillis(1)); - assertThat(createConfig(Collections.singletonMap("duration", "2ms")).getDuration("duration")) - .isEqualTo(Duration.ofMillis(2)); - assertThat(createConfig(Collections.singletonMap("duration", "3s")).getDuration("duration")) - .isEqualTo(Duration.ofSeconds(3)); - assertThat(createConfig(Collections.singletonMap("duration", "4m")).getDuration("duration")) - .isEqualTo(Duration.ofMinutes(4)); - assertThat(createConfig(Collections.singletonMap("duration", "5h")).getDuration("duration")) - .isEqualTo(Duration.ofHours(5)); - assertThat(createConfig(Collections.singletonMap("duration", "6d")).getDuration("duration")) - .isEqualTo(Duration.ofDays(6)); - // Check Space handling - assertThat(createConfig(Collections.singletonMap("duration", "7 ms")).getDuration("duration")) - .isEqualTo(Duration.ofMillis(7)); - assertThat(createConfig(Collections.singletonMap("duration", "8 ms")).getDuration("duration")) - .isEqualTo(Duration.ofMillis(8)); - } - - private static ConfigProperties createConfig(Map properties) { - return new ConfigPropertiesAdapter(Config.builder().readProperties(properties).build()); - } -}