Skip to content

Commit

Permalink
DefaultConfigProperties#getMap filters entries with blank values inte…
Browse files Browse the repository at this point in the history
…ad of throwing (#5784)
  • Loading branch information
jack-berg authored Sep 7, 2023
1 parent 1cb4f1d commit 3f152eb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ default List<String> getList(String name, List<String> defaultValue) {
/**
* Returns a Map configuration property. The format of the original value must be comma-separated
* for each key, with an '=' separating the key and value. For instance, <code>
* service.name=Greatest Service,host.name=localhost</code> Empty values will be removed.
* service.name=Greatest Service,host.name=localhost</code>. Empty values will be removed.
*
* @return an empty map if the property has not been configured.
* @throws ConfigurationException for malformed map strings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static java.util.stream.Collectors.joining;

import io.opentelemetry.api.internal.ConfigUtil;
import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import java.time.Duration;
Expand Down Expand Up @@ -209,16 +210,21 @@ public static Set<String> getSet(ConfigProperties config, String name) {
@Override
public Map<String, String> getMap(String name) {
return getList(ConfigUtil.normalizePropertyKey(name)).stream()
.map(keyValuePair -> filterBlanksAndNulls(keyValuePair.split("=", 2)))
.map(
splitKeyValuePairs -> {
if (splitKeyValuePairs.size() != 2) {
entry -> {
String[] split = entry.split("=", 2);
if (split.length != 2 || StringUtils.isNullOrEmpty(split[0])) {
throw new ConfigurationException(
"Invalid map property: " + name + "=" + config.get(name));
}
return new AbstractMap.SimpleImmutableEntry<>(
splitKeyValuePairs.get(0), splitKeyValuePairs.get(1));
return filterBlanksAndNulls(split);
})
// Filter entries with an empty value, i.e. "foo="
.filter(splitKeyValuePairs -> splitKeyValuePairs.size() == 2)
.map(
splitKeyValuePairs ->
new AbstractMap.SimpleImmutableEntry<>(
splitKeyValuePairs.get(0), splitKeyValuePairs.get(1)))
// If duplicate keys, prioritize later ones similar to duplicate system properties on a
// Java command line.
.collect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,6 @@ void uncleanMap() {

@Test
void invalidMap() {
assertThatThrownBy(
() ->
DefaultConfigProperties.createFromMap(Collections.singletonMap("map", "a=1,b="))
.getMap("map"))
.isInstanceOf(ConfigurationException.class)
.hasMessage("Invalid map property: map=a=1,b=");
assertThatThrownBy(
() ->
DefaultConfigProperties.createFromMap(Collections.singletonMap("map", "a=1,b"))
Expand Down Expand Up @@ -281,7 +275,7 @@ private static Map<String, String> makeTestProps() {
properties.put("test.double", "5.4");
properties.put("test.boolean", "true");
properties.put("test.list", "cat,dog,bear");
properties.put("test.map", "cat=meow,dog=bark,bear=growl");
properties.put("test.map", "cat=meow,dog=bark,bear=growl,bird=");
properties.put("test.duration", "1s");
return properties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class ResourceConfigurationTest {
void customConfigResource() {
Map<String, String> props = new HashMap<>();
props.put("otel.service.name", "test-service");
props.put("otel.resource.attributes", "food=cheesecake,drink=juice");
props.put(
"otel.resource.attributes", "food=cheesecake,drink=juice,animal= ,color=,shape=square");
props.put("otel.experimental.resource.disabled-keys", "drink");

assertThat(
Expand All @@ -42,6 +43,7 @@ void customConfigResource() {
Resource.getDefault().toBuilder()
.put(ResourceAttributes.SERVICE_NAME, "test-service")
.put("food", "cheesecake")
.put("shape", "square")
.setSchemaUrl(ResourceAttributes.SCHEMA_URL)
.build());
}
Expand Down

0 comments on commit 3f152eb

Please sign in to comment.