@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
75 lines (74 loc) • 3.04 kB
JavaScript
import { debounce } from "../utils/debounce.js";
import { getSegmentSpanCaptureStrategy, setSegmentSpanCaptureStrategy } from "./segmentSpanCaptureStrategy.js";
// src/tracing/deferSegmentSpanCapture.ts
const CAPTURED_SPANS = /* @__PURE__ */ new WeakSet();
const isSpanAlreadyCaptured = (span) => CAPTURED_SPANS.has(span);
const markSpanCaptured = (span) => {
CAPTURED_SPANS.add(span);
};
const CLIENT_QUEUES = /* @__PURE__ */ new WeakMap();
/**
* @private Private API with no semver guarantees!
*
* Enable deferred segment-span transaction capture for a client: create its debounced queue and
* register the strategy (idempotent).
*
* `SentrySpan` otherwise assembles the transaction synchronously the instant a segment span ends, which
* drops children whose async instrumentation closes them later (a diagnostics-channel `asyncEnd`
* callback in the same tick, or engine spans replayed on a later tick). The debounced snapshot delays
* capture just enough for those later span ends to land first; a child that still ends after it is
* emitted as its own orphan transaction. Pending captures drain on the client's `flush` hook, so
* `Sentry.flush()` / `client.close()` cannot resolve before they run.
*/
function _INTERNAL_setDeferSegmentSpanCapture(client) {
if (!getSegmentSpanCaptureStrategy()) setSegmentSpanCaptureStrategy(deferredSegmentSpanCaptureStrategy);
if (CLIENT_QUEUES.has(client)) return;
const pendingCaptures = /* @__PURE__ */ new Set();
const debouncedDrain = debounce(() => {
const captures = [...pendingCaptures];
pendingCaptures.clear();
for (const capture of captures) capture();
}, 1, { maxWait: 100 });
client.on("flush", () => {
debouncedDrain.flush();
});
CLIENT_QUEUES.set(client, (capture) => {
pendingCaptures.add(capture);
debouncedDrain();
});
}
const deferredSegmentSpanCaptureStrategy = {
onSegmentSpanEnded(convert, scope) {
const client = scope.getClient();
const enqueue = client && CLIENT_QUEUES.get(client);
if (!enqueue) {
const transactionEvent = convert();
if (transactionEvent) client?.captureEvent(transactionEvent);
return;
}
enqueue(() => {
const transactionEvent = convert({
isSpanAlreadyCaptured,
onSpanCaptured: markSpanCaptured
});
if (transactionEvent) client.captureEvent(transactionEvent);
});
},
onChildSpanEnded(span, rootSpan, convert, scope) {
if (CAPTURED_SPANS.has(span) || !CAPTURED_SPANS.has(rootSpan)) return;
const client = scope.getClient();
const enqueue = client && CLIENT_QUEUES.get(client);
const captureOrphan = () => {
const transactionEvent = convert({
isSpanAlreadyCaptured,
onSpanCaptured: markSpanCaptured
});
if (transactionEvent?.contexts?.trace?.data) transactionEvent.contexts.trace.data["sentry.parent_span_already_sent"] = true;
if (transactionEvent) client?.captureEvent(transactionEvent);
};
if (enqueue) enqueue(captureOrphan);
else captureOrphan();
}
};
export { _INTERNAL_setDeferSegmentSpanCapture };
//# sourceMappingURL=deferSegmentSpanCapture.js.map