Skip to content

Commit afc16a0

Browse files
authored
Merge pull request #418 from DataDog/mar-kolya/ignore-empty-tags-in-config
Ignore empty entries in tags config strings
2 parents 590ff0e + db2f3c8 commit afc16a0

3 files changed

Lines changed: 27 additions & 16 deletions

File tree

dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package datadog.opentracing;
22

3+
import static datadog.trace.common.DDTraceConfig.HEADER_TAGS;
4+
import static datadog.trace.common.DDTraceConfig.SERVICE_MAPPING;
5+
import static datadog.trace.common.DDTraceConfig.SERVICE_NAME;
6+
import static datadog.trace.common.DDTraceConfig.SPAN_TAGS;
37
import static datadog.trace.common.util.Config.parseMap;
48

59
import datadog.opentracing.decorators.AbstractDecorator;
@@ -89,12 +93,12 @@ public DDTracer(final String serviceName) {
8993

9094
public DDTracer(final Properties config) {
9195
this(
92-
config.getProperty(DDTraceConfig.SERVICE_NAME),
96+
config.getProperty(SERVICE_NAME),
9397
Writer.Builder.forConfig(config),
9498
Sampler.Builder.forConfig(config),
95-
parseMap(config.getProperty(DDTraceConfig.SPAN_TAGS)),
96-
parseMap(config.getProperty(DDTraceConfig.SERVICE_MAPPING)),
97-
parseMap(config.getProperty(DDTraceConfig.HEADER_TAGS)));
99+
parseMap(config.getProperty(SPAN_TAGS), SPAN_TAGS),
100+
parseMap(config.getProperty(SERVICE_MAPPING), SERVICE_MAPPING),
101+
parseMap(config.getProperty(HEADER_TAGS), HEADER_TAGS));
98102
log.debug("Using config: {}", config);
99103
}
100104

@@ -167,9 +171,9 @@ public DDTracer(final Writer writer) {
167171
UNASSIGNED_DEFAULT_SERVICE_NAME,
168172
writer,
169173
new AllSampler(),
170-
parseMap(new DDTraceConfig().getProperty(DDTraceConfig.SPAN_TAGS)),
171-
parseMap(new DDTraceConfig().getProperty(DDTraceConfig.SERVICE_MAPPING)),
172-
parseMap(new DDTraceConfig().getProperty(DDTraceConfig.HEADER_TAGS)));
174+
parseMap(new DDTraceConfig().getProperty(SPAN_TAGS), SPAN_TAGS),
175+
parseMap(new DDTraceConfig().getProperty(SERVICE_MAPPING), SPAN_TAGS),
176+
parseMap(new DDTraceConfig().getProperty(HEADER_TAGS), SPAN_TAGS));
173177
}
174178

175179
/**

dd-trace-ot/src/main/java/datadog/trace/common/util/Config.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ public static String propToEnvName(final String name) {
1616
return name.toUpperCase().replace(".", "_");
1717
}
1818

19-
public static Map<String, String> parseMap(final String str) {
19+
public static Map<String, String> parseMap(final String str, final String settingName) {
2020
if (str == null || str.trim().isEmpty()) {
2121
return Collections.emptyMap();
2222
}
23-
if (!str.matches("(([^,:]+:[^,:]+,)*([^,:]+:[^,:]+),?)?")) {
24-
log.warn("Invalid config '{}'. Must match 'key1:value1,key2:value2'.", str);
23+
if (!str.matches("(([^,:]+:[^,:]*,)*([^,:]+:[^,:]*),?)?")) {
24+
log.warn(
25+
"Invalid config for {}: '{}'. Must match 'key1:value1,key2:value2'.", settingName, str);
2526
return Collections.emptyMap();
2627
}
2728

@@ -31,7 +32,13 @@ public static Map<String, String> parseMap(final String str) {
3132
for (final String token : tokens) {
3233
final String[] keyValue = token.split(":", -1);
3334
if (keyValue.length == 2) {
34-
map.put(keyValue[0].trim(), keyValue[1].trim());
35+
final String key = keyValue[0].trim();
36+
final String value = keyValue[1].trim();
37+
if (value.length() <= 0) {
38+
log.warn("Ignoring empty value for key '{}' in config for {}", key, settingName);
39+
continue;
40+
}
41+
map.put(key, value);
3542
}
3643
}
3744
return Collections.unmodifiableMap(map);

dd-trace-ot/src/test/groovy/datadog/trace/DDTraceConfigTest.groovy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class DDTraceConfigTest extends Specification {
129129
where:
130130
mapString | map
131131
"a:1, a:2, a:3" | [a: "3"]
132-
"a:b,c:d" | [a: "b", c: "d"]
132+
"a:b,c:d,e:" | [a: "b", c: "d"]
133133
}
134134

135135
def "verify single override on #source for #key"() {
@@ -151,28 +151,28 @@ class DDTraceConfigTest extends Specification {
151151

152152
def "parsing valid string returns a map"() {
153153
expect:
154-
parseMap(str) == map
154+
parseMap(str, "test") == map
155155

156156
where:
157157
str | map
158+
"a:" | [:]
158159
"a:a;" | [a: "a;"]
159160
"a:1, a:2, a:3" | [a: "3"]
160-
"a:b,c:d" | [a: "b", c: "d"]
161+
"a:b,c:d,e:" | [a: "b", c: "d"]
161162
"key 1!:va|ue_1," | ["key 1!": "va|ue_1"]
162163
" key1 :value1 ,\t key2: value2" | [key1: "value1", key2: "value2"]
163164
}
164165

165166
def "parsing an invalid string returns an empty map"() {
166167
expect:
167-
parseMap(str) == [:]
168+
parseMap(str, "test") == [:]
168169

169170
where:
170171
str | _
171172
null | _
172173
"" | _
173174
"1" | _
174175
"a" | _
175-
"a:" | _
176176
"a,1" | _
177177
"in:val:id" | _
178178
"a:b:c:d" | _

0 commit comments

Comments
 (0)