TraceContext.from constructor

TraceContext.from({
  1. String? requestId,
  2. String? traceparent,
  3. String? tracestate,
  4. Map<String, String>? baggage,
})

Builds a context from whatever the caller sent, generating what is missing.

A blank or absent request id becomes a fresh one, so every request has an id whether or not the caller supplied one. traceparent is carried verbatim when present and not invented when absent — a malformed or fabricated one is worse than none, because a collector will happily stitch it into the wrong trace.

Implementation

factory TraceContext.from({
  String? requestId,
  String? traceparent,
  String? tracestate,
  Map<String, String>? baggage,
}) {
  return TraceContext(
    requestId: switch (requestId) {
      final String id when id.trim().isNotEmpty => id,
      _ => newRequestId(),
    },
    traceparent: _orNull(traceparent),
    tracestate: _orNull(tracestate),
    baggage: baggage,
  );
}