|
| 1 | +import type { Span as SentrySpan } from '@sentry/types'; |
| 2 | + |
| 3 | +interface SpanMapEntry { |
| 4 | + sentrySpan: SentrySpan; |
| 5 | + ref: SpanRefType; |
| 6 | + // These are not direct children, but all spans under the tree of a root span. |
| 7 | + subSpans: string[]; |
| 8 | +} |
| 9 | + |
| 10 | +const SPAN_REF_ROOT = Symbol('root'); |
| 11 | +const SPAN_REF_CHILD = Symbol('child'); |
| 12 | +const SPAN_REF_CHILD_ENDED = Symbol('child_ended'); |
| 13 | +type SpanRefType = typeof SPAN_REF_ROOT | typeof SPAN_REF_CHILD | typeof SPAN_REF_CHILD_ENDED; |
| 14 | + |
| 15 | +/** Exported only for tests. */ |
| 16 | +export const SPAN_MAP = new Map<string, SpanMapEntry>(); |
| 17 | + |
| 18 | +/** |
| 19 | + * Get a Sentry span for a given span ID. |
| 20 | + */ |
| 21 | +export function getSentrySpan(spanId: string): SentrySpan | undefined { |
| 22 | + const entry = SPAN_MAP.get(spanId); |
| 23 | + return entry ? entry.sentrySpan : undefined; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Set a Sentry span for a given span ID. |
| 28 | + * This is necessary so we can lookup parent spans later. |
| 29 | + * We also keep a list of children for root spans only, in order to be able to clean them up together. |
| 30 | + */ |
| 31 | +export function setSentrySpan(spanId: string, sentrySpan: SentrySpan): void { |
| 32 | + let ref: SpanRefType = SPAN_REF_ROOT; |
| 33 | + |
| 34 | + const rootSpanId = sentrySpan.transaction?.spanId; |
| 35 | + |
| 36 | + if (rootSpanId && rootSpanId !== spanId) { |
| 37 | + const root = SPAN_MAP.get(rootSpanId); |
| 38 | + if (root) { |
| 39 | + root.subSpans.push(spanId); |
| 40 | + ref = SPAN_REF_CHILD; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + SPAN_MAP.set(spanId, { |
| 45 | + sentrySpan, |
| 46 | + ref, |
| 47 | + subSpans: [], |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Clear references of the given span ID. |
| 53 | + */ |
| 54 | +export function clearSpan(spanId: string): void { |
| 55 | + const entry = SPAN_MAP.get(spanId); |
| 56 | + if (!entry) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const { ref, subSpans } = entry; |
| 61 | + |
| 62 | + // If this is a child, mark it as ended. |
| 63 | + if (ref === SPAN_REF_CHILD) { |
| 64 | + entry.ref = SPAN_REF_CHILD_ENDED; |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // If this is a root span, clear all (ended) children |
| 69 | + if (ref === SPAN_REF_ROOT) { |
| 70 | + for (const childId of subSpans) { |
| 71 | + const child = SPAN_MAP.get(childId); |
| 72 | + if (!child) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + if (child.ref === SPAN_REF_CHILD_ENDED) { |
| 77 | + // if the child has already ended, just clear it |
| 78 | + SPAN_MAP.delete(childId); |
| 79 | + } else if (child.ref === SPAN_REF_CHILD) { |
| 80 | + // If the child has not ended yet, mark it as a root span so it is cleared when it ends. |
| 81 | + child.ref = SPAN_REF_ROOT; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + SPAN_MAP.delete(spanId); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // Generally, `clearSpan` should never be called for ref === SPAN_REF_CHILD_ENDED |
| 90 | + // But if it does, just clear the span |
| 91 | + SPAN_MAP.delete(spanId); |
| 92 | +} |
0 commit comments