{"version":3,"file":"traceLinks.js","sources":["../../../src/utils/traceLinks.ts"],"sourcesContent":["import type { DurableObjectStorage } from '@cloudflare/workers-types';\nimport { TraceFlags } from '@opentelemetry/api';\nimport type { SpanLink } from '@sentry/core';\nimport { debug, getActiveSpan, SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE, spanIsSampled } from '@sentry/core';\nimport { DEBUG_BUILD } from '../debug-build';\n\n/** Storage key prefix for the span context that links consecutive method invocations */\nconst SENTRY_TRACE_LINK_KEY_PREFIX = '__SENTRY_TRACE_LINK__';\n\n/** Stored span context for creating span links */\nexport interface StoredSpanContext {\n traceId: string;\n spanId: string;\n sampled: boolean;\n}\n\n/**\n * Gets the storage key for a specific method's trace link.\n */\nexport function getTraceLinkKey(methodName: string): string {\n return `${SENTRY_TRACE_LINK_KEY_PREFIX}${methodName}`;\n}\n\n/**\n * Stores the current span context in Durable Object storage for trace linking.\n * Uses the original uninstrumented storage to avoid creating spans for internal operations.\n * Errors are silently ignored to prevent internal storage failures from propagating to user code.\n */\nexport function storeSpanContext(originalStorage: DurableObjectStorage, methodName: string): void {\n try {\n const activeSpan = getActiveSpan();\n if (activeSpan) {\n const spanContext = activeSpan.spanContext();\n const storedContext: StoredSpanContext = {\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n sampled: spanIsSampled(activeSpan),\n };\n\n originalStorage.kv.put(getTraceLinkKey(methodName), storedContext);\n }\n } catch (error) {\n // Silently ignore storage errors to prevent internal failures from affecting user code\n DEBUG_BUILD && debug.log(`[CloudflareClient] Error storing span context for method ${methodName}`, error);\n }\n}\n\n/**\n * Retrieves a stored span context from Durable Object storage.\n * Errors are silently ignored to prevent internal storage failures from propagating to user code.\n */\nexport function getStoredSpanContext(\n originalStorage: DurableObjectStorage,\n methodName: string,\n): StoredSpanContext | undefined {\n try {\n return originalStorage.kv.get(getTraceLinkKey(methodName));\n } catch (error) {\n // Silently ignore storage errors to prevent internal failures from affecting user code\n DEBUG_BUILD && debug.log(`[CloudflareClient] Error retrieving span context for method ${methodName}`, error);\n return undefined;\n }\n}\n\n/**\n * Builds span links from a stored span context.\n */\nexport function buildSpanLinks(storedContext: StoredSpanContext): SpanLink[] {\n return [\n {\n context: {\n traceId: storedContext.traceId,\n spanId: storedContext.spanId,\n traceFlags: storedContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,\n },\n attributes: {\n [SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE]: 'previous_trace',\n },\n },\n ];\n}\n"],"names":[],"mappings":";;;;AAOA,MAAM,4BAAA,GAA+B,uBAAA;AAY9B,SAAS,gBAAgB,UAAA,EAA4B;AAC1D,EAAA,OAAO,CAAA,EAAG,4BAA4B,CAAA,EAAG,UAAU,CAAA,CAAA;AACrD;AAOO,SAAS,gBAAA,CAAiB,iBAAuC,UAAA,EAA0B;AAChG,EAAA,IAAI;AACF,IAAA,MAAM,aAAa,aAAA,EAAc;AACjC,IAAA,IAAI,UAAA,EAAY;AACd,MAAA,MAAM,WAAA,GAAc,WAAW,WAAA,EAAY;AAC3C,MAAA,MAAM,aAAA,GAAmC;AAAA,QACvC,SAAS,WAAA,CAAY,OAAA;AAAA,QACrB,QAAQ,WAAA,CAAY,MAAA;AAAA,QACpB,OAAA,EAAS,cAAc,UAAU;AAAA,OACnC;AAEA,MAAA,eAAA,CAAgB,EAAA,CAAG,GAAA,CAAI,eAAA,CAAgB,UAAU,GAAG,aAAa,CAAA;AAAA,IACnE;AAAA,EACF,SAAS,KAAA,EAAO;AAEd,IAAA,WAAA,IAAe,KAAA,CAAM,GAAA,CAAI,CAAA,yDAAA,EAA4D,UAAU,IAAI,KAAK,CAAA;AAAA,EAC1G;AACF;AAMO,SAAS,oBAAA,CACd,iBACA,UAAA,EAC+B;AAC/B,EAAA,IAAI;AACF,IAAA,OAAO,eAAA,CAAgB,EAAA,CAAG,GAAA,CAAuB,eAAA,CAAgB,UAAU,CAAC,CAAA;AAAA,EAC9E,SAAS,KAAA,EAAO;AAEd,IAAA,WAAA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