All files / src/utils baggage.ts

97.97% Statements 97/99
97.14% Branches 34/35
100% Functions 3/3
97.97% Lines 97/99

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1001x 1x 1x 1x 1x 1x 15x 15x 15x 38x 36x 38x 38x 38x 15x 15x 15x 1x 1x 1x     1x 15x 15x 15x 1x 1x 8x 8x 8x 7x 7x 8x 20x 20x 19x 19x 19x 19x 19x 20x 16x 16x 19x 19x 19x 19x 20x 7x 7x 7x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 3x 3x 16x 16x 5x 5x 16x 16x 2x 2x 16x 16x 3x 3x 3x 3x 2x 2x 3x 16x 16x 1x 1x 16x 16x 1x 1x 16x 16x 16x  
import type { BaggageFields } from '../types';
 
const SENTRY_PREFIX = 'sentry-';
const MAX_BAGGAGE_LENGTH = 8192;
 
export function serializeBaggage(fields: BaggageFields): string {
  const parts: string[] = [];
 
  for (const [key, value] of Object.entries(fields)) {
    if (value === undefined || value === null || value === '') continue;
 
    const prefixedKey = key.startsWith(SENTRY_PREFIX) ? key : `${SENTRY_PREFIX}${key}`;
    parts.push(`${prefixedKey}=${encodeURIComponent(String(value))}`);
  }
 
  let result = parts.join(',');
  if (result.length > MAX_BAGGAGE_LENGTH) {
    result = result.substring(0, MAX_BAGGAGE_LENGTH);
    const lastComma = result.lastIndexOf(',');
    if (lastComma > 0) {
      result = result.substring(0, lastComma);
    }
  }
 
  return result;
}
 
export function parseBaggage(header: string): BaggageFields {
  const fields: BaggageFields = {};
 
  if (!header) return fields;
 
  const parts = header.split(',');
  for (const part of parts) {
    const eqIdx = part.indexOf('=');
    if (eqIdx === -1) continue;
 
    const rawKey = part.substring(0, eqIdx).trim();
    const value = decodeURIComponent(part.substring(eqIdx + 1).trim());
 
    let key = rawKey;
    if (rawKey.startsWith(SENTRY_PREFIX)) {
      key = rawKey.substring(SENTRY_PREFIX.length);
    }
 
    if (key) {
      fields[key] = value;
    }
  }
 
  return fields;
}
 
export function createBaggageFromTransaction(
  traceId: string,
  options?: {
    dsnPublicKey?: string;
    sampled?: boolean;
    sampleRate?: number;
    user?: { id?: string; segment?: string };
    release?: string;
    environment?: string;
  }
): BaggageFields {
  const fields: BaggageFields = {
    trace_id: traceId,
  };
 
  if (options?.dsnPublicKey) {
    fields.public_key = options.dsnPublicKey;
  }
 
  if (options?.sampled !== undefined) {
    fields.sampled = options.sampled ? 'true' : 'false';
  }
 
  if (options?.sampleRate !== undefined) {
    fields.sample_rate = String(options.sampleRate);
  }
 
  if (options?.user) {
    if (options.user.id) {
      fields.user_id = options.user.id;
    }
    if (options.user.segment) {
      fields.user_segment = options.user.segment;
    }
  }
 
  if (options?.release) {
    fields.release = options.release;
  }
 
  if (options?.environment) {
    fields.environment = options.environment;
  }
 
  return fields;
}