|
| 1 | +import type { DurableObjectStorage } from '@cloudflare/workers-types'; |
| 2 | +import { TraceFlags } from '@opentelemetry/api'; |
| 3 | +import { getActiveSpan } from '@sentry/core'; |
| 4 | + |
| 5 | +/** Storage key prefix for the span context that links consecutive method invocations */ |
| 6 | +const SENTRY_TRACE_LINK_KEY_PREFIX = '__SENTRY_TRACE_LINK__'; |
| 7 | + |
| 8 | +/** Stored span context for creating span links */ |
| 9 | +export interface StoredSpanContext { |
| 10 | + traceId: string; |
| 11 | + spanId: string; |
| 12 | + sampled: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +/** Span link structure for connecting traces */ |
| 16 | +export interface SpanLink { |
| 17 | + context: { |
| 18 | + traceId: string; |
| 19 | + spanId: string; |
| 20 | + traceFlags: number; |
| 21 | + }; |
| 22 | + attributes?: Record<string, string>; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Gets the storage key for a specific method's trace link. |
| 27 | + */ |
| 28 | +export function getTraceLinkKey(methodName: string): string { |
| 29 | + return `${SENTRY_TRACE_LINK_KEY_PREFIX}${methodName}`; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Stores the current span context in Durable Object storage for trace linking. |
| 34 | + * Uses the original uninstrumented storage to avoid creating spans for internal operations. |
| 35 | + */ |
| 36 | +export async function storeSpanContext(originalStorage: DurableObjectStorage, methodName: string): Promise<void> { |
| 37 | + const activeSpan = getActiveSpan(); |
| 38 | + if (activeSpan) { |
| 39 | + const spanContext = activeSpan.spanContext(); |
| 40 | + const storedContext: StoredSpanContext = { |
| 41 | + traceId: spanContext.traceId, |
| 42 | + spanId: spanContext.spanId, |
| 43 | + sampled: spanContext.traceFlags === TraceFlags.SAMPLED, |
| 44 | + }; |
| 45 | + await originalStorage.put(getTraceLinkKey(methodName), storedContext); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Retrieves a stored span context from Durable Object storage. |
| 51 | + */ |
| 52 | +export async function getStoredSpanContext( |
| 53 | + originalStorage: DurableObjectStorage, |
| 54 | + methodName: string, |
| 55 | +): Promise<StoredSpanContext | undefined> { |
| 56 | + try { |
| 57 | + return await originalStorage.get<StoredSpanContext>(getTraceLinkKey(methodName)); |
| 58 | + } catch { |
| 59 | + return undefined; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Builds span links from a stored span context. |
| 65 | + */ |
| 66 | +export function buildSpanLinks(storedContext: StoredSpanContext): SpanLink[] { |
| 67 | + return [ |
| 68 | + { |
| 69 | + context: { |
| 70 | + traceId: storedContext.traceId, |
| 71 | + spanId: storedContext.spanId, |
| 72 | + traceFlags: storedContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE, |
| 73 | + }, |
| 74 | + attributes: { |
| 75 | + 'sentry.link.type': 'previous_trace', |
| 76 | + }, |
| 77 | + }, |
| 78 | + ]; |
| 79 | +} |
0 commit comments