dvRedactTelemetryText function
text as it may leave the process in telemetry: resolved DV.Secrets
values and credential shapes (bearer and basic headers, JWTs, the password
in a URL) struck out, exactly as a log line is.
One function for both, because a span attribute and a log line go to the same collector, and a redaction rule that only one of them follows is a credential that leaves through the other.
Implementation
String dvRedactTelemetryText(String text) {
String redacted = dvRedactSecrets(text);
// Every shape needs at least a dozen characters and a telltale piece --
// `eyJ`, `://`, or Bearer/Basic -- so the usual attribute (a route, a
// method, a table name) skips the regular expressions altogether: this
// runs for every string attribute of every span.
if (redacted.length < 12) return redacted;
final bool jwt = redacted.contains('eyJ');
final bool url = redacted.contains('://');
// Plain substring tests: a case-insensitive alternation costs microseconds
// a string on the Dart VM, which is more than the rest of a span.
final bool scheme = redacted.contains('earer') ||
redacted.contains('EARER') ||
redacted.contains('asic') ||
redacted.contains('ASIC');
if (!jwt && !url && !scheme) return redacted;
for (final (RegExp shape, String Function(Match) replace)
in DVLogger._credentialShapes) {
redacted = redacted.replaceAllMapped(shape, replace);
}
return redacted;
}