{"version":3,"file":"should-ignore-span.js","sources":["../../../src/utils/should-ignore-span.ts"],"sourcesContent":["import { DEBUG_BUILD } from '../debug-build';\nimport type { ClientOptions, IgnoreSpanAttributeValue } from '../types-hoist/options';\nimport type { SpanJSON } from '../types-hoist/span';\nimport { debug } from './debug-logger';\nimport { isMatchingPattern } from './string';\n\nfunction logIgnoredSpan(droppedSpan: Pick<SpanJSON, 'description' | 'op'>): void {\n  debug.log(`Ignoring span ${droppedSpan.op} - ${droppedSpan.description} because it matches \\`ignoreSpans\\`.`);\n}\n\n/**\n * Check if a span should be ignored based on the ignoreSpans configuration.\n */\nexport function shouldIgnoreSpan(\n  span: Pick<SpanJSON, 'description' | 'op'> & { attributes?: Record<string, unknown> },\n  ignoreSpans: Required<ClientOptions>['ignoreSpans'],\n): boolean {\n  if (!ignoreSpans?.length) {\n    return false;\n  }\n\n  for (const pattern of ignoreSpans) {\n    if (isStringOrRegExp(pattern)) {\n      if (span.description && isMatchingPattern(span.description, pattern)) {\n        DEBUG_BUILD && logIgnoredSpan(span);\n        return true;\n      }\n      continue;\n    }\n\n    const hasAttributes = !!pattern.attributes && Object.keys(pattern.attributes).length > 0;\n    if (!pattern.name && !pattern.op && !hasAttributes) {\n      continue;\n    }\n\n    const nameMatches = pattern.name ? span.description && isMatchingPattern(span.description, pattern.name) : true;\n    const opMatches = pattern.op ? span.op && isMatchingPattern(span.op, pattern.op) : true;\n    const attrsMatch = pattern.attributes\n      ? Object.entries(pattern.attributes).every(([key, valuePattern]) =>\n          _matchesAttributeValue(span.attributes?.[key], valuePattern),\n        )\n      : true;\n\n    // This check here is only correct because we can guarantee that we ran `isMatchingPattern`\n    // for at least one of `nameMatches`, `opMatches`, or `attrsMatch`. So in contrary to how this looks,\n    // not all of op, name, and attributes actually have to match. This is the most efficient way to check\n    // for all combinations of name, op, and attribute patterns.\n    if (nameMatches && opMatches && attrsMatch) {\n      DEBUG_BUILD && logIgnoredSpan(span);\n      return true;\n    }\n  }\n\n  return false;\n}\n\nfunction _matchesAttributeValue(actual: unknown, pat: IgnoreSpanAttributeValue): boolean {\n  // String values support pattern matching\n  if (typeof actual === 'string' && (typeof pat === 'string' || pat instanceof RegExp)) {\n    return isMatchingPattern(actual, pat);\n  }\n  // Arrays: element-wise strict equality\n  if (Array.isArray(actual) && Array.isArray(pat)) {\n    return actual.length === pat.length && actual.every((v, i) => v === pat[i]);\n  }\n  // Primitives: strict equality\n  return actual === pat;\n}\n\n/**\n * Takes a list of spans, and a span that was dropped, and re-parents the child spans of the dropped span to the parent of the dropped span, if possible.\n * This mutates the spans array in place!\n */\nexport function reparentChildSpans(spans: SpanJSON[], dropSpan: SpanJSON): void {\n  const droppedSpanParentId = dropSpan.parent_span_id;\n  const droppedSpanId = dropSpan.span_id;\n\n  // This should generally not happen, as we do not apply this on root spans\n  // but to be safe, we just bail in this case\n  if (!droppedSpanParentId) {\n    return;\n  }\n\n  for (const span of spans) {\n    if (span.parent_span_id === droppedSpanId) {\n      span.parent_span_id = droppedSpanParentId;\n    }\n  }\n}\n\nfunction isStringOrRegExp(value: unknown): value is string | RegExp {\n  return typeof value === 'string' || value instanceof RegExp;\n}\n"],"names":["debug","isMatchingPattern","DEBUG_BUILD"],"mappings":";;;;;;AAMA,SAAS,cAAc,CAAC,WAAW,EAA8C;AACjF,EAAEA,iBAAK,CAAC,GAAG,CAAC,CAAC,cAAc,EAAE,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,WAAW,CAAC,WAAW,CAAC,oCAAoC,CAAC,CAAC;AAC/G;;AAEA;AACA;AACA;AACO,SAAS,gBAAgB;AAChC,EAAE,IAAI;AACN,EAAE,WAAW;AACb,EAAW;AACX,EAAE,IAAI,CAAC,WAAW,EAAE,MAAM,EAAE;AAC5B,IAAI,OAAO,KAAK;AAChB,EAAE;;AAEF,EAAE,KAAK,MAAM,OAAA,IAAW,WAAW,EAAE;AACrC,IAAI,IAAI,gBAAgB,CAAC,OAAO,CAAC,EAAE;AACnC,MAAM,IAAI,IAAI,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