Skip to content

Commit 241ba69

Browse files
committed
Use InvalidBooleanValueException; return false when encountered
1 parent f0c322e commit 241ba69

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ final class ConfigConverter {
2121

2222
private static final Logger log = LoggerFactory.getLogger(ConfigConverter.class);
2323

24+
/**
25+
* Custom exception for invalid boolean values to maintain backward compatibility. When caught in
26+
* ConfigProvider, boolean methods should return false instead of default value.
27+
*/
28+
static class InvalidBooleanValueException extends IllegalArgumentException {
29+
public InvalidBooleanValueException(String message) {
30+
super(message);
31+
}
32+
}
33+
2434
private static final ValueOfLookup LOOKUP = new ValueOfLookup();
2535

2636
/**
@@ -422,8 +432,8 @@ public static Boolean booleanValueOf(String value) {
422432
} else if ("false".equalsIgnoreCase(value)) {
423433
return Boolean.FALSE;
424434
} else {
425-
// Throw exception for invalid boolean values
426-
throw new IllegalArgumentException("Invalid boolean value: " + value);
435+
// Throw custom exception for invalid boolean values to maintain backward compatibility
436+
throw new InvalidBooleanValueException("Invalid boolean value: " + value);
427437
}
428438
}
429439

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,27 @@ public double getDouble(String key, double defaultValue) {
193193

194194
private <T> T get(String key, T defaultValue, Class<T> type, String... aliases) {
195195
for (ConfigProvider.Source source : sources) {
196+
String sourceValue = source.get(key, aliases);
196197
try {
197-
String sourceValue = source.get(key, aliases);
198198
T value = ConfigConverter.valueOf(sourceValue, type);
199199
if (value != null) {
200200
if (collectConfig) {
201201
ConfigCollector.get().put(key, sourceValue, source.origin());
202202
}
203203
return value;
204204
}
205+
} catch (ConfigConverter.InvalidBooleanValueException ex) {
206+
// For backward compatibility: invalid boolean values should return false, not default
207+
// Store the invalid sourceValue for telemetry, but return false for the application
208+
if (Boolean.class.equals(type)) {
209+
if (collectConfig) {
210+
ConfigCollector.get().put(key, sourceValue, source.origin());
211+
}
212+
return (T) Boolean.FALSE;
213+
}
214+
// For non-boolean types, continue to next source
205215
} catch (IllegalArgumentException ex) {
206-
// continue - covers both NumberFormatException and IllegalArgumentException
216+
// continue - covers both NumberFormatException and other IllegalArgumentException
207217
}
208218
}
209219
if (collectConfig) {

internal-api/src/test/groovy/datadog/trace/api/ConfigTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ class ConfigTest extends DDSpecification {
657657
config.agentHost == " "
658658
config.agentPort == 8126
659659
config.agentUrl == "http:// :8126"
660-
config.prioritySamplingEnabled == true
660+
config.prioritySamplingEnabled == false
661661
config.traceResolverEnabled == true
662662
config.serviceMapping == [:]
663663
config.mergedSpanTags == [:]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ConfigConverterTest extends DDSpecification {
2828
ConfigConverter.valueOf(invalidValue, Boolean)
2929

3030
then:
31-
def exception = thrown(IllegalArgumentException)
31+
def exception = thrown(ConfigConverter.InvalidBooleanValueException)
3232
exception.message.contains("Invalid boolean value:")
3333

3434
where:

0 commit comments

Comments
 (0)