diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/config/ConfigValueParsers.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/config/ConfigValueParsers.java index 5bcb3090d032..e01a928369db 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/config/ConfigValueParsers.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/config/ConfigValueParsers.java @@ -55,15 +55,15 @@ private static ConfigParsingException newInvalidPropertyException( } static List parseList(@SuppressWarnings("unused") String propertyName, String value) { - return Collections.unmodifiableList(filterBlanksAndNulls(value.split(","))); + return Collections.unmodifiableList(filterBlanks(value.split(","))); } static Map parseMap(String propertyName, String value) { return parseList(propertyName, value).stream() - .map(keyValuePair -> filterBlanksAndNulls(keyValuePair.split("=", 2))) + .map(keyValuePair -> trim(keyValuePair.split("=", 2))) .map( splitKeyValuePairs -> { - if (splitKeyValuePairs.size() != 2) { + if (splitKeyValuePairs.size() != 2 || splitKeyValuePairs.get(0).isEmpty()) { throw new ConfigParsingException( "Invalid map property: " + propertyName + "=" + value); } @@ -77,13 +77,17 @@ static Map parseMap(String propertyName, String value) { Map.Entry::getKey, Map.Entry::getValue, (first, next) -> next, LinkedHashMap::new)); } - private static List filterBlanksAndNulls(String[] values) { + private static List filterBlanks(String[] values) { return Arrays.stream(values) .map(String::trim) .filter(s -> !s.isEmpty()) .collect(Collectors.toList()); } + private static List trim(String[] values) { + return Arrays.stream(values).map(String::trim).collect(Collectors.toList()); + } + static Duration parseDuration(String propertyName, String value) { String unitString = getUnitString(value); String numberString = value.substring(0, value.length() - unitString.length()); 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 index da72c127a440..f9ce31998cb4 100644 --- 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 @@ -31,6 +31,7 @@ void allValid() { 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); @@ -41,6 +42,8 @@ void allValid() { assertThat(config.getCommaSeparatedValues("list")).containsExactly("cat", "dog", "bear"); assertThat(config.getCommaSeparatedMap("map")) .containsExactly(entry("cat", "meow"), entry("dog", "bark"), entry("bear", "growl")); + assertThat(config.getCommaSeparatedMap("mapWithEmptyValue")) + .containsExactly(entry("cat", "meow"), entry("dog", ""), entry("bear", "growl")); assertThat(config.getDuration("duration")).isEqualTo(Duration.ofSeconds(1)); } @@ -131,11 +134,6 @@ void uncleanMap() { @Test void invalidMap() { - assertThatThrownBy( - () -> - createConfig(Collections.singletonMap("map", "a=1,b=")).getCommaSeparatedMap("map")) - .isInstanceOf(ConfigurationException.class) - .hasMessage("Invalid map property: map=a=1,b="); assertThatThrownBy( () -> createConfig(Collections.singletonMap("map", "a=1,b")).getCommaSeparatedMap("map"))