getTracingHeaders function

Map<String, String> getTracingHeaders(
  1. TracingContext context,
  2. TracingHeaderType headersType, {
  3. TraceContextInjection contextInjection = TraceContextInjection.all,
})

Implementation

Map<String, String> getTracingHeaders(
  TracingContext context,
  TracingHeaderType headersType, {
  TraceContextInjection contextInjection = TraceContextInjection.all,
}) {
  var headers = <String, String>{};

  final sampledString = context.sampled ? '1' : '0';
  bool shouldInjectHeaders =
      context.sampled || contextInjection == TraceContextInjection.all;

  switch (headersType) {
    case TracingHeaderType.datadog:
      if (shouldInjectHeaders) {
        headers[DatadogHttpTracingHeaders.traceId] =
            context.traceId.asString(TracingIdRepresentation.lowDecimal);
        headers[DatadogHttpTracingHeaders.tags] =
            '${DatadogHttpTracingHeaders.traceIdTag}=${context.traceId.asString(TracingIdRepresentation.highHex16Chars)}';
        headers[DatadogHttpTracingHeaders.parentId] =
            context.spanId.asString(TracingIdRepresentation.decimal);
        headers[DatadogHttpTracingHeaders.origin] = 'rum';
        headers[DatadogHttpTracingHeaders.samplingPriority] = sampledString;
      }
      break;
    case TracingHeaderType.b3:
      if (context.sampled) {
        final headerValue = [
          context.traceId.asString(TracingIdRepresentation.hex32Chars),
          context.spanId.asString(TracingIdRepresentation.hex16Chars),
          sampledString,
          context.parentSpanId?.asString(TracingIdRepresentation.hex16Chars),
        ].whereType<String>().join('-');
        headers[OTelHttpTracingHeaders.singleB3] = headerValue;
      } else if (contextInjection == TraceContextInjection.all) {
        headers[OTelHttpTracingHeaders.singleB3] = sampledString;
      }
      break;
    case TracingHeaderType.b3multi:
      if (shouldInjectHeaders) {
        headers[OTelHttpTracingHeaders.multipleSampled] = sampledString;
      }

      if (context.sampled) {
        headers[OTelHttpTracingHeaders.multipleTraceId] =
            context.traceId.asString(TracingIdRepresentation.hex32Chars);
        headers[OTelHttpTracingHeaders.multipleSpanId] =
            context.spanId.asString(TracingIdRepresentation.hex16Chars);
        if (context.parentSpanId != null) {
          headers[OTelHttpTracingHeaders.multipleParentId] = context
              .parentSpanId!
              .asString(TracingIdRepresentation.hex16Chars);
        }
      }
      break;
    case TracingHeaderType.tracecontext:
      if (shouldInjectHeaders) {
        final spanString =
            context.spanId.asString(TracingIdRepresentation.hex16Chars);
        final parentHeaderValue = [
          '00', // Version Code
          context.traceId.asString(TracingIdRepresentation.hex32Chars),
          spanString,
          context.sampled ? '01' : '00'
        ].join('-');
        final stateHeaderValue = [
          's:$sampledString',
          'o:rum',
          'p:$spanString',
        ].join(';');
        headers[W3CTracingHeaders.traceparent] = parentHeaderValue;
        headers[W3CTracingHeaders.tracestate] = 'dd=$stateHeaderValue';
      }
      break;
  }

  return headers;
}