parse static method
Reads a traceparent header, or null when it is absent or malformed.
A malformed header is dropped rather than rejected: an unreadable trace id is an observability problem, and failing the request over it would turn a broken collector into an outage.
Implementation
static TraceContext? parse(String? traceparent) {
if (traceparent == null) return null;
final parts = traceparent.trim().split('-');
if (parts.length < 4) return null;
if (parts[0] != '00') return null;
if (!_isHex(parts[1], 32) || !_isHex(parts[2], 16)) return null;
if (!_isHex(parts[3], 2)) return null;
// All-zero ids are reserved as "invalid" by the specification.
if (parts[1] == '0' * 32 || parts[2] == '0' * 16) return null;
return TraceContext(
traceId: parts[1],
spanId: parts[2],
sampled: int.parse(parts[3], radix: 16) & 0x01 == 0x01,
);
}