extract method
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;
}
if (!traceParentHeaderRegEx.hasMatch(traceParentHeader)) {
// Encountered a malformed or unknown trace header. Do nothing.
return context;
}
final parentHeaderMatch =
traceParentHeaderRegEx.firstMatch(traceParentHeader)!;
final parentHeaderFields = Map<String, String?>.fromIterable(
parentHeaderMatch.groupNames,
key: (element) => element.toString(),
value: (element) => parentHeaderMatch.namedGroup(element));
final traceId =
api.TraceId.fromString(parentHeaderFields[_traceIdFieldKey]!);
final parentId =
api.SpanId.fromString(parentHeaderFields[_parentIdFieldKey]!);
final traceFlags =
int.parse(parentHeaderFields[_traceFlagsFieldKey]!, radix: 16);
final traceStateHeader = getter.get(carrier, _traceStateHeaderKey);
final traceState = (traceStateHeader != null)
? sdk.TraceState.fromString(traceStateHeader)
: sdk.TraceState.empty();
return context.withSpan(api.NonRecordingSpan(
sdk.SpanContext.remote(traceId, parentId, traceFlags, traceState)));
}