Skip to content

Commit 2df85ac

Browse files
committed
feat: match stable config java properties on other operator than 'contains'
1 parent 887ea39 commit 2df85ac

File tree

1 file changed

+28
-57
lines changed

1 file changed

+28
-57
lines changed

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

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -104,35 +104,27 @@ private static boolean doesRuleMatch(Rule rule) {
104104
return true; // Return true if all selectors match
105105
}
106106

107-
private static boolean validOperatorForLanguageOrigin(String operator) {
108-
operator = operator.toLowerCase();
109-
// "exists" is not valid
110-
switch (operator) {
111-
case "equals":
112-
case "starts_with":
113-
case "ends_with":
114-
case "contains":
115-
return true;
116-
default:
117-
return false;
118-
}
119-
}
120-
121-
private static boolean checkEnvMatches(
122-
List<String> values, List<String> matches, BiPredicate<String, String> compareFunc) {
123-
// envValue shouldn't be null, but doing an extra check to avoid NullPointerException on
124-
// compareFunc.test
125-
if (values == null) {
107+
private static boolean matchOperator(String value, String operator, List<String> matches, String match) {
108+
// not sure if these are nullable, but the semantics make sense
109+
// and that will save us from a NPE
110+
if (value == null || matches == null || operator == null) {
126111
return false;
127112
}
113+
value = value.toLowerCase();
128114
for (String match : matches) {
129-
if (match == null) {
130-
continue;
131-
}
132-
for (String value : values) {
133-
if (compareFunc.test(value, match.toLowerCase())) {
115+
switch (operator) {
116+
case "equals":
117+
return value == match;
118+
case "starts_with":
119+
return value.startsWith(match);
120+
case "ends_with":
121+
return value.endsWith(match);
122+
case "contains":
123+
return value.contains(match);
124+
case "exists":
134125
return true;
135-
}
126+
default:
127+
return false;
136128
}
137129
}
138130
return false;
@@ -141,53 +133,32 @@ private static boolean checkEnvMatches(
141133
// We do all of the case insensitivity modifications in this function, because each selector will
142134
// be viewed just once
143135
static boolean selectorMatch(String origin, List<String> matches, String operator, String key) {
136+
operator = operator.toLowerCase();
144137
switch (origin.toLowerCase()) {
145138
case "language":
146-
if (!validOperatorForLanguageOrigin(operator)) {
147-
return false;
148-
}
149-
for (String entry : matches) {
150-
// loose match on any reference to "*java*"
151-
if (entry.toLowerCase().contains("java")) {
152-
return true;
153-
}
154-
}
139+
return matchOperator("java", operator, matches);
155140
case "environment_variables":
156141
if (key == null) {
157-
return false;
142+
return false;
158143
}
159-
String envValue = System.getenv(key.toUpperCase());
160-
if (envValue == null) {
144+
String value = System.getenv(key.toUpperCase());
145+
if (value == null) {
161146
return false;
162147
}
163-
envValue = envValue.toLowerCase();
164-
switch (operator.toLowerCase()) {
165-
case "exists":
166-
// We don't care about the value
167-
return true;
168-
case "equals":
169-
return checkEnvMatches(
170-
Collections.singletonList(envValue), matches, String::equalsIgnoreCase);
171-
case "starts_with":
172-
return checkEnvMatches(
173-
Collections.singletonList(envValue), matches, String::startsWith);
174-
case "ends_with":
175-
return checkEnvMatches(Collections.singletonList(envValue), matches, String::endsWith);
176-
case "contains":
177-
return checkEnvMatches(Collections.singletonList(envValue), matches, String::contains);
178-
default:
179-
return false;
180-
}
148+
return matchOperator(value, operator, matches, key);
181149
case "process_arguments":
150+
if (key == null) {
151+
return false;
152+
}
182153
// TODO: flesh out the meaning of each operator for process_arguments
183154
if (!key.startsWith("-D")) {
184155
log.warn(
185156
"Ignoring unsupported process_arguments entry in selector match, '{}'. Only system properties specified with the '-D' prefix are supported.",
186157
key);
187158
return false;
188159
}
189-
// Cut the -D prefix
190-
return System.getProperty(key.substring(2)) != null;
160+
String value = System.getProperty(key.substring(2));
161+
return matchOperator(value, operator, matches, key);
191162
case "tags":
192163
// TODO: Support this down the line (Must define the source of "tags" first)
193164
return false;

0 commit comments

Comments
 (0)