Skip to content

Commit b006773

Browse files
bm1549claude
andcommitted
Fix forbidden API usage in formatKnuthSamplingRate
Replace String#replaceAll (a forbidden API in this codebase) with manual character-based trailing-zero stripping logic that has the same semantics but avoids the regex-based method. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 457c471 commit b006773

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

  • dd-trace-core/src/main/java/datadog/trace/core

dd-trace-core/src/main/java/datadog/trace/core/DDSpan.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,16 @@ static String formatKnuthSamplingRate(double rate) {
664664
// Use %.6g format: up to 6 significant digits, no trailing zeros
665665
// This matches Go's strconv.FormatFloat(rate, 'g', 6, 64) and Python's f"{rate:.6g}"
666666
String formatted = String.format("%.6g", rate);
667-
// Remove trailing zeros after decimal point
667+
// Remove trailing zeros after decimal point (avoid forbidden String#replaceAll)
668668
if (formatted.contains(".")) {
669-
formatted = formatted.replaceAll("0+$", "");
670-
formatted = formatted.replaceAll("\\.$", "");
669+
int end = formatted.length();
670+
while (end > 0 && formatted.charAt(end - 1) == '0') {
671+
end--;
672+
}
673+
if (end > 0 && formatted.charAt(end - 1) == '.') {
674+
end--;
675+
}
676+
formatted = formatted.substring(0, end);
671677
}
672678
return formatted;
673679
}

0 commit comments

Comments
 (0)