UNPKG

@sentry/core

Version:
107 lines (106 loc) • 5.97 kB
import { SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME } from "../../semanticAttributes.js"; import { getCombinedScopeData } from "../../utils/scopeData.js"; import { INTERNAL_getSegmentSpan, spanToJSON, spanToStaticSpanJSON, streamedSpanJsonToSerializedSpan } from "../../utils/spanUtils.js"; import { freezeDscOnSpan, getDynamicSamplingContextFromSpan } from "../dynamicSamplingContext.js"; import { getCapturedScopesOnSpan } from "../utils.js"; import { applyBeforeSendSpanCallback, isStaticBeforeSendSpanCallback } from "./beforeSendSpan.js"; import { spanJsonToSerializedStreamedSpan } from "./spanJsonToStreamedSpan.js"; import { scopeContextsToSpanAttributes } from "./scopeContextAttributes.js"; import "../../constants.js"; import { SENTRY_ENVIRONMENT, SENTRY_RELEASE, SENTRY_SDK_INTEGRATIONS, SENTRY_SDK_NAME, SENTRY_SDK_VERSION, SENTRY_SEGMENT_ID, SENTRY_SEGMENT_NAME, SENTRY_TRACE_LIFECYCLE, USER_EMAIL, USER_ID, USER_IP_ADDRESS, USER_NAME } from "@sentry/conventions/attributes"; // src/tracing/spans/captureSpan.ts /** * Captures a span and returns a JSON representation to be enqueued for sending. * * IMPORTANT: This function converts the span to JSON immediately to avoid writing * to an already-ended OTel span instance (which is blocked by the OTel Span class). * * @returns the final serialized span with a reference to its segment span. This reference * is needed later on to compute the DSC and the final segment name for the span envelope. */ function captureSpan(span, client) { const spanJSON = spanToJSON(span); if (spanJSON.attributes) delete spanJSON.attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME]; const segmentSpan = INTERNAL_getSegmentSpan(span); const serializedSegmentSpan = spanToJSON(segmentSpan); const { isolationScope: spanIsolationScope, scope: spanScope } = getCapturedScopesOnSpan(span); const finalScopeData = getCombinedScopeData(spanIsolationScope, spanScope); applyCommonSpanAttributes(spanJSON, serializedSegmentSpan, client, finalScopeData); client.emit("preprocessSpan", spanJSON); if (spanJSON.is_segment) { freezeDscOnSpan(segmentSpan, getDynamicSamplingContextFromSpan(segmentSpan)); applyScopeToSegmentSpan(spanJSON, finalScopeData); applySdkMetadataToSegmentSpan(spanJSON, client); client.emit("processSegmentSpan", spanJSON); } client.emit("processSpan", spanJSON); const { beforeSendSpan, traceLifecycle } = client.getOptions(); const processedSpan = traceLifecycle !== "static" && beforeSendSpan && !isStaticBeforeSendSpanCallback(beforeSendSpan) ? applyBeforeSendSpanCallback(spanJSON, beforeSendSpan) : spanJSON; return { ...streamedSpanJsonToSerializedSpan(processedSpan), _segmentSpan: segmentSpan, _segmentNameAtCapture: serializedSegmentSpan.name }; } function applyScopeToSegmentSpan(segmentSpanJSON, scopeData) { safeSetSpanJSONAttributes(segmentSpanJSON, scopeContextsToSpanAttributes(scopeData.contexts)); } /** * Safely set attributes on a span JSON. * If an attribute already exists, it will not be overwritten. */ function safeSetSpanJSONAttributes(spanJSON, newAttributes) { const originalAttributes = spanJSON.attributes ?? (spanJSON.attributes = {}); Object.entries(newAttributes).forEach(([key, value]) => { if (value != null && !(key in originalAttributes)) originalAttributes[key] = value; }); } function applySdkMetadataToSegmentSpan(segmentSpanJSON, client) { const integrationNames = client.getIntegrationNames(); if (!integrationNames.length) return; safeSetSpanJSONAttributes(segmentSpanJSON, { [SENTRY_SDK_INTEGRATIONS]: integrationNames }); } function commonSpanAttributes(serializedSegmentSpan, client, scopeData, includeScopeAttributes = true) { const sdk = client.getSdkMetadata(); const { release, environment } = client.getOptions(); return { [SENTRY_TRACE_LIFECYCLE]: "stream", [SENTRY_SEGMENT_NAME]: serializedSegmentSpan.name, [SENTRY_SEGMENT_ID]: serializedSegmentSpan.span_id, [SENTRY_SDK_NAME]: sdk?.sdk?.name, [SENTRY_SDK_VERSION]: sdk?.sdk?.version, [SENTRY_RELEASE]: release, [SENTRY_ENVIRONMENT]: environment || "production", [USER_ID]: scopeData.user?.id, [USER_EMAIL]: scopeData.user?.email, [USER_IP_ADDRESS]: scopeData.user?.ip_address, [USER_NAME]: scopeData.user?.username, ...includeScopeAttributes ? scopeData.attributes : void 0 }; } function applyCommonSpanAttributes(spanJSON, serializedSegmentSpan, client, scopeData) { safeSetSpanJSONAttributes(spanJSON, commonSpanAttributes(serializedSegmentSpan, client, scopeData)); } /** * Captures a standalone span whose `beforeSendSpan` callback expects the v1 {@link SpanJSON} format * (i.e. the user opted out of span streaming). The span is serialized to v1, the common attributes are * applied, the callback runs in its native format, and the result is converted forward to a serialized * v2 span. This mirrors how gen_ai spans reach the v2 span path from a static transaction (a plain * conversion, no `processSpan` hooks), so there is never a reverse v2 -> v1 conversion. * * TODO(standalone): remove once the static (transaction) trace lifecycle is dropped. */ function captureStandaloneSpanWithStaticCallback(span, client, beforeSendSpan) { const spanJSON = spanToStaticSpanJSON(span); const segmentSpan = INTERNAL_getSegmentSpan(span); const serializedSegmentSpan = spanToJSON(segmentSpan); const { isolationScope: spanIsolationScope, scope: spanScope } = getCapturedScopesOnSpan(span); const commonAttributes = commonSpanAttributes(serializedSegmentSpan, client, getCombinedScopeData(spanIsolationScope, spanScope), false); Object.entries(commonAttributes).forEach(([key, value]) => { if (value != null && !(key in spanJSON.data)) spanJSON.data[key] = value; }); const processedSpan = applyBeforeSendSpanCallback(spanJSON, beforeSendSpan); return spanJsonToSerializedStreamedSpan(processedSpan); } export { captureSpan, captureStandaloneSpanWithStaticCallback, safeSetSpanJSONAttributes }; //# sourceMappingURL=captureSpan.js.map