@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
27 lines (26 loc) • 1.28 kB
JavaScript
import { createSpanContainerItem } from "./envelope.js";
import { hasSpanStreamingEnabled } from "./hasSpanStreamingEnabled.js";
import { spanJsonToSerializedStreamedSpan } from "./spanJsonToStreamedSpan.js";
// src/tracing/spans/extractGenAiSpans.ts
/**
* Extracts gen_ai spans from a transaction event, converts them to span v2 format,
* and returns them as a SpanContainerItem.
*
* Only applies to static mode (non-streaming) transactions.
*
* WARNING: This function mutates `event.spans` by removing the extracted gen_ai spans
* from the array. Call this before creating the event envelope so the transaction
* item does not include the extracted spans.
*/
function extractGenAiSpansFromEvent(event, client) {
if (event.type !== "transaction" || !event.spans?.length || !event.sdkProcessingMetadata?.hasGenAiSpans || hasSpanStreamingEnabled(client)) return;
const genAiSpans = [];
const remainingSpans = [];
for (const span of event.spans) if (span.op?.startsWith("gen_ai.")) genAiSpans.push(spanJsonToSerializedStreamedSpan(span));
else remainingSpans.push(span);
if (genAiSpans.length === 0) return;
event.spans = remainingSpans;
return createSpanContainerItem(genAiSpans, client);
}
export { extractGenAiSpansFromEvent };
//# sourceMappingURL=extractGenAiSpans.js.map