Skip to content

Commit

Permalink
Add support for commas in values for exporter.otlp.headers
Browse files Browse the repository at this point in the history
  • Loading branch information
lesterhaynes committed Dec 6, 2023
1 parent 247ef4d commit 719ff40
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,21 @@ public List<String> getList(String name) {
if (value == null) {
return Collections.emptyList();
}
return filterBlanksAndNulls(value.split(","));

// Support list members containing commas per RFC9110 5.5 suggestion
String[] listMembers;
// check if list members are double-quoted
if (value.startsWith("\"") && value.endsWith("\"")) {
// remove first and last quote and split on '","'
value = value.substring(1, value.length() - 1);
listMembers = value.split("\"\\s?,\\s?\"");
} else if (value.contains(",") && value.contains("\"")) {
throw new ConfigurationException("Invalid list property: " + name + "=" + config.get(name));
} else {
listMembers = value.split(",");
}

return filterBlanksAndNulls(listMembers);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ void allValid() {
assertThat(config.getList("test.list")).containsExactly("cat", "dog", "bear");
assertThat(config.getMap("test.map"))
.containsExactly(entry("cat", "meow"), entry("dog", "bark"), entry("bear", "growl"));
assertThat(config.getMap("test.map.commas"))
.containsExactly(
entry("cat", "meow,hiss"), entry("dog", "bark,growl"), entry("bear", "growl,roar"));
assertThat(config.getDuration("test.duration")).isEqualTo(Duration.ofSeconds(1));
}

Expand All @@ -49,6 +52,9 @@ void allValidUsingHyphens() {
assertThat(config.getList("test-list")).containsExactly("cat", "dog", "bear");
assertThat(config.getMap("test-map"))
.containsExactly(entry("cat", "meow"), entry("dog", "bark"), entry("bear", "growl"));
assertThat(config.getMap("test-map-commas"))
.containsExactly(
entry("cat", "meow,hiss"), entry("dog", "bark,growl"), entry("bear", "growl,roar"));
assertThat(config.getDuration("test-duration")).isEqualTo(Duration.ofSeconds(1));
}

Expand All @@ -61,6 +67,7 @@ void allMissing() {
assertThat(config.getDouble("test.double")).isNull();
assertThat(config.getList("test.list")).isEmpty();
assertThat(config.getMap("test.map")).isEmpty();
assertThat(config.getMap("test.map.commas")).isEmpty();
assertThat(config.getDuration("test.duration")).isNull();
}

Expand All @@ -73,6 +80,7 @@ void allEmpty() {
properties.put("test.double", "");
properties.put("test.list", "");
properties.put("test.map", "");
properties.put("test.map.commas", "");
properties.put("test.duration", "");

ConfigProperties config = DefaultConfigProperties.createFromMap(properties);
Expand All @@ -82,6 +90,7 @@ void allEmpty() {
assertThat(config.getDouble("test.double")).isNull();
assertThat(config.getList("test.list")).isEmpty();
assertThat(config.getMap("test.map")).isEmpty();
assertThat(config.getMap("test.map.commas")).isEmpty();
assertThat(config.getDuration("test.duration")).isNull();
}

Expand Down Expand Up @@ -169,6 +178,17 @@ void invalidMap() {
.hasMessage("Invalid map property: map=a=1,=b");
}

@Test
void invalidList() {
DefaultConfigProperties config =
DefaultConfigProperties.createFromMap(
Collections.singletonMap(
"invalid", "\"cat=meow,hiss\",\"dog=bark,growl\", bear=growl"));
assertThatThrownBy(() -> config.getList("invalid"))
.isInstanceOf(ConfigurationException.class)
.hasMessageContaining("Invalid list property");
}

@Test
void invalidDuration() {
assertThatThrownBy(
Expand Down Expand Up @@ -277,6 +297,7 @@ private static Map<String, String> makeTestProps() {
properties.put("test.list", "cat,dog,bear");
properties.put("test.map", "cat=meow,dog=bark,bear=growl,bird=");
properties.put("test.duration", "1s");
properties.put("test.map.commas", "\"cat=meow,hiss\",\"dog=bark,growl\", \"bear=growl,roar\"");
return properties;
}
}

0 comments on commit 719ff40

Please sign in to comment.