addBaggageHeader function

void addBaggageHeader(
  1. ISentrySpan span,
  2. Map<String, dynamic> headers, {
  3. SentryLogger? logger,
})

Implementation

void addBaggageHeader(
  ISentrySpan span,
  Map<String, dynamic> headers, {
  SentryLogger? logger,
}) {
  final baggage = span.toBaggageHeader();
  if (baggage != null) {
    final currentValue = headers[baggage.name] as String? ?? '';

    final currentBaggage = SentryBaggage.fromHeader(
      currentValue,
      logger: logger,
    );
    final sentryBaggage = SentryBaggage.fromHeader(
      baggage.value,
      logger: logger,
    );

    // overwrite sentry's keys https://develop.sentry.dev/sdk/performance/dynamic-sampling-context/#baggage
    final filteredBaggageHeader = Map.from(currentBaggage.keyValues);
    filteredBaggageHeader
        .removeWhere((key, value) => key.startsWith('sentry-'));

    final mergedBaggage = <String, String>{
      ...filteredBaggageHeader,
      ...sentryBaggage.keyValues,
    };

    final newBaggage = SentryBaggage(mergedBaggage, logger: logger);

    headers[baggage.name] = newBaggage.toHeaderString();
  }
}