Skip to content

Commit 337fe3d

Browse files
committed
initial working parsing for DD_TAGS
1 parent c23a2c9 commit 337fe3d

File tree

2 files changed

+34
-39
lines changed

2 files changed

+34
-39
lines changed

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/ConfigConverter.java

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,6 @@ private static void loadMap(
138138
try {
139139
int start = 0;
140140
int splitter = str.indexOf(keyValueSeparator, start);
141-
if (splitter == -1){
142-
return;
143-
}
144141
char argSeparator = '\u0000';
145142
int argSeparatorInd = -1;
146143
for (Character sep : argSeparators){ //find the first instance of the first possible separator
@@ -150,45 +147,43 @@ private static void loadMap(
150147
break;
151148
}
152149
}
153-
if (argSeparator == '\u0000'){ //no argSeparator found
154-
return;
155-
}
156-
while (splitter != -1) {
157-
int nextSplitter = str.indexOf(keyValueSeparator, splitter + 1);
158-
int nextArgSeparator = -1;
159-
nextArgSeparator = str.indexOf(argSeparator, splitter + 1);
160-
nextArgSeparator = nextArgSeparator == -1 ? str.length() : nextArgSeparator;
161-
// if we have a delimiter after this splitter, then try to move the splitter forward to
162-
// allow for tags with ':' in them
163-
int end = nextArgSeparator;
164-
while (nextSplitter != -1 && nextSplitter < end) { //skips all following splitters before the nextArgSeparator
165-
nextSplitter = str.indexOf(keyValueSeparator, nextSplitter + 1);
150+
while (splitter != -1 || start < str.length()) {
151+
int nextSplitter = argSeparatorInd == -1 ? -1 : str.indexOf(keyValueSeparator, argSeparatorInd + 1);
152+
int nextArgSeparator = argSeparatorInd == -1 ? -1 : str.indexOf(argSeparator, argSeparatorInd + 1);
153+
int end = argSeparatorInd == -1 ? str.length() : argSeparatorInd;
154+
155+
if(start >= end){ //the character is only the delimiter
156+
start = end + 1;
157+
splitter = nextSplitter;
158+
argSeparatorInd = nextArgSeparator;
159+
continue;
166160
}
167-
if (nextSplitter == -1) {
168-
// this is either the end of the string or the next position where the value should be
169-
// trimmed
170-
if (end < str.length() - 1) {
171-
// there are characters after the argSeparator
172-
throw new BadFormatException(String.format("Non white space characters after trailing '%c'", nextArgSeparator));
161+
162+
if(splitter >= end || splitter == -1){ //only key, no value
163+
String key = str.substring(start, end).trim();
164+
if(!key.isEmpty()){
165+
map.put(key, "");
173166
}
174-
} else {
175-
if (end >= str.length()) {
176-
// this should not happen
177-
throw new BadFormatException("Illegal position of split character ':'");
167+
}else{
168+
String key = str.substring(start, splitter).trim();
169+
if (key.indexOf(argSeparator) != -1) {
170+
throw new BadFormatException("Illegal '" + argSeparator + "' character in key '" + key + "'");
171+
}
172+
String value;
173+
if (splitter + 1 >= end){ // no splitter in this string: only key, no value
174+
value = "";
175+
}else{
176+
value = str.substring(splitter + 1, end).trim();
177+
}
178+
if (value.indexOf(argSeparator) != -1) {
179+
throw new BadFormatException("Illegal '" + argSeparator + "' character in value for key '" + key + "'");
180+
}
181+
if (!key.isEmpty()) {
182+
map.put(key, value);
178183
}
179-
}
180-
String key = str.substring(start, splitter).trim();
181-
if (key.indexOf(argSeparator) != -1) {
182-
throw new BadFormatException("Illegal '" + argSeparator + "' character in key '" + key + "'");
183-
}
184-
String value = str.substring(splitter + 1, end).trim();
185-
if (value.indexOf(argSeparator) != -1) {
186-
throw new BadFormatException("Illegal '" + argSeparator + "' character in value for key '" + key + "'");
187-
}
188-
if (!key.isEmpty()) {
189-
map.put(key, value);
190184
}
191185
splitter = nextSplitter;
186+
argSeparatorInd = nextArgSeparator;
192187
start = end + 1;
193188
}
194189
} catch (Throwable t) {

internal-api/src/test/groovy/datadog/trace/bootstrap/config/provider/ConfigConverterTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ class ConfigConverterTest extends DDSpecification {
117117
"env:test aKey:aVal bKey:bVal cKey:" | ':' | [] | [env: "test", aKey: "aVal", bKey: "bVal", cKey:""]
118118
"env:test,aKey:aVal,bKey:bVal,cKey:" | ':' | [] | [env: "test", aKey: "aVal", bKey: "bVal", cKey:""]
119119
"env:test,aKey:aVal bKey:bVal cKey:" | ':' | [] | [env: "test", aKey: "aVal bKey:bVal cKey:"]
120-
"env:test bKey :bVal dKey: dVal cKey:" | ':' | [] | [env: "test", bKey: "", dKey: "", cKey: ""]
120+
"env:test bKey :bVal dKey: dVal cKey:" | ':' | [] | [env: "test", bKey: "", dKey: "", dVal: "", cKey: ""] //[bKey: "", cKey: "", dVal: "", dKey: "", env:"test"]
121121
'env :test, aKey : aVal bKey:bVal cKey:' | ':' | [] | [env: "test", aKey : "aVal bKey:bVal cKey:"]
122122
"env:keyWithA:Semicolon bKey:bVal cKey" | ':' | [] | [env: "keyWithA:Semicolon", bKey: "bVal", cKey: ""]
123123
"env:keyWith: , , Lots:Of:Semicolons " | ':' | [] | [env: "keyWith:", Lots: "Of:Semicolons"]
124124
"a:b,c,d" | ':' | [] | [a: "b", c: "", d: ""]
125-
"a,1" | ':' | [] | [a: "", (1): ""]
125+
"a,1" | ':' | [] | [a: "", "1": ""]
126126
"a:b:c:d" | ':' | [] | [a: "b:c:d"]
127127
// spotless:on
128128
}

0 commit comments

Comments
 (0)