extract method

  1. @override
Context extract(
  1. Context context,
  2. dynamic carrier,
  3. TextMapGetter getter
)
override

Implementation

@override
api.Context extract(
    api.Context context, dynamic carrier, api.TextMapGetter getter) {
  final traceParentHeader = getter.get(carrier, _traceParentHeaderKey);
  if (traceParentHeader == null) {
    // Carrier did not contain a trace header.  Do nothing.
    return context;
  }

  final parentHeaderMatch =
      traceParentHeaderRegEx.firstMatch(traceParentHeader);

  if (parentHeaderMatch == null) {
    // Encountered a malformed or unknown trace header.  Do nothing.
    return context;
  }

  final parentHeaderFields = Map<String, String>.fromIterable(
      parentHeaderMatch.groupNames,
      key: (element) => element.toString(),
      value: (element) => parentHeaderMatch.namedGroup(element)!);

  final traceIdHeader = parentHeaderFields[_traceIdFieldKey];
  final traceId = (traceIdHeader != null)
      ? api.TraceId.fromString(traceIdHeader)
      : api.TraceId.invalid();
  final parentIdHeader = parentHeaderFields[_parentIdFieldKey];
  final parentId = (parentIdHeader != null)
      ? api.SpanId.fromString(parentIdHeader)
      : api.SpanId.invalid();
  final traceFlagsHeader = parentHeaderFields[_traceFlagsFieldKey];
  final traceFlags = (traceFlagsHeader != null)
      ? int.parse(traceFlagsHeader, radix: 16)
      : api.TraceFlags.none;
  final traceStateHeader = getter.get(carrier, _traceStateHeaderKey);
  final traceState = (traceStateHeader != null)
      ? api.TraceState.fromString(traceStateHeader)
      : api.TraceState.empty();

  return api.contextWithSpan(
      context,
      NonRecordingSpan(
          api.SpanContext.remote(traceId, parentId, traceFlags, traceState)));
}