All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.build.esm.utils.traceData.js.map Maven / Gradle / Ivy

There is a newer version: 8.39.0
Show newest version
{"version":3,"file":"traceData.js","sources":["../../../src/utils/traceData.ts"],"sourcesContent":["import type { Client, Scope, Span } from '@sentry/types';\nimport {\n  TRACEPARENT_REGEXP,\n  dynamicSamplingContextToSentryBaggageHeader,\n  generateSentryTraceHeader,\n  logger,\n} from '@sentry/utils';\nimport { getClient, getCurrentScope } from '../currentScopes';\nimport { getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan } from '../tracing';\nimport { getActiveSpan, getRootSpan, spanToTraceHeader } from './spanUtils';\n\ntype TraceData = {\n  'sentry-trace'?: string;\n  baggage?: string;\n};\n\n/**\n * Extracts trace propagation data from the current span or from the client's scope (via transaction or propagation\n * context) and serializes it to `sentry-trace` and `baggage` values to strings. These values can be used to propagate\n * a trace via our tracing Http headers or Html `` tags.\n *\n * This function also applies some validation to the generated sentry-trace and baggage values to ensure that\n * only valid strings are returned.\n *\n * @param span a span to take the trace data from. By default, the currently active span is used.\n * @param scope the scope to take trace data from By default, the active current scope is used.\n * @param client the SDK's client to take trace data from. By default, the current client is used.\n *\n * @returns an object with the tracing data values. The object keys are the name of the tracing key to be used as header\n * or meta tag name.\n */\nexport function getTraceData(span?: Span, scope?: Scope, client?: Client): TraceData {\n  const clientToUse = client || getClient();\n  const scopeToUse = scope || getCurrentScope();\n  const spanToUse = span || getActiveSpan();\n\n  const { dsc, sampled, traceId } = scopeToUse.getPropagationContext();\n  const rootSpan = spanToUse && getRootSpan(spanToUse);\n\n  const sentryTrace = spanToUse ? spanToTraceHeader(spanToUse) : generateSentryTraceHeader(traceId, undefined, sampled);\n\n  const dynamicSamplingContext = rootSpan\n    ? getDynamicSamplingContextFromSpan(rootSpan)\n    : dsc\n      ? dsc\n      : clientToUse\n        ? getDynamicSamplingContextFromClient(traceId, clientToUse)\n        : undefined;\n\n  const baggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);\n\n  const isValidSentryTraceHeader = TRACEPARENT_REGEXP.test(sentryTrace);\n  if (!isValidSentryTraceHeader) {\n    logger.warn('Invalid sentry-trace data. Cannot generate trace data');\n    return {};\n  }\n\n  const validBaggage = isValidBaggageString(baggage);\n  if (!validBaggage) {\n    logger.warn('Invalid baggage data. Not returning \"baggage\" value');\n  }\n\n  return {\n    'sentry-trace': sentryTrace,\n    ...(validBaggage && { baggage }),\n  };\n}\n\n/**\n * Tests string against baggage spec as defined in:\n *\n * - W3C Baggage grammar: https://www.w3.org/TR/baggage/#definition\n * - RFC7230 token definition: https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.6\n *\n * exported for testing\n */\nexport function isValidBaggageString(baggage?: string): boolean {\n  if (!baggage || !baggage.length) {\n    return false;\n  }\n  const keyRegex = \"[-!#$%&'*+.^_`|~A-Za-z0-9]+\";\n  const valueRegex = '[!#-+-./0-9:<=>?@A-Z\\\\[\\\\]a-z{-}]+';\n  const spaces = '\\\\s*';\n  // eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- RegExp for readability, no user input\n  const baggageRegex = new RegExp(\n    `^${keyRegex}${spaces}=${spaces}${valueRegex}(${spaces},${spaces}${keyRegex}${spaces}=${spaces}${valueRegex})*$`,\n  );\n  return baggageRegex.test(baggage);\n}\n"],"names":[],"mappings":";;;;;;;AAgBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,YAAY,CAAC,IAAI,EAAS,KAAK,EAAU,MAAM,EAAsB;AACrF,EAAE,MAAM,WAAY,GAAE,UAAU,SAAS,EAAE,CAAA;AAC3C,EAAE,MAAM,UAAW,GAAE,SAAS,eAAe,EAAE,CAAA;AAC/C,EAAE,MAAM,SAAU,GAAE,QAAQ,aAAa,EAAE,CAAA;AAC3C;AACA,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,OAAQ,EAAA,GAAI,UAAU,CAAC,qBAAq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