Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultConfigProperties#getMap filters entries with blank values intead of throwing #5784

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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