UNPKG

@sentry/core

Version:
99 lines (95 loc) • 5.25 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const semanticAttributes = require('../../semanticAttributes.js'); const scopeData = require('../../utils/scopeData.js'); const spanUtils = require('../../utils/spanUtils.js'); const utils = require('../utils.js'); const beforeSendSpan = require('./beforeSendSpan.js'); const spanJsonToStreamedSpan = require('./spanJsonToStreamedSpan.js'); const scopeContextAttributes = require('./scopeContextAttributes.js'); const constants = require('../../constants.js'); const attributes = require('@sentry/conventions/attributes'); function captureSpan(span, client) { const spanJSON = spanUtils.spanToJSON(span); const segmentSpan = spanUtils.INTERNAL_getSegmentSpan(span); const serializedSegmentSpan = spanUtils.spanToJSON(segmentSpan); const { isolationScope: spanIsolationScope, scope: spanScope } = utils.getCapturedScopesOnSpan(span); const finalScopeData = scopeData.getCombinedScopeData(spanIsolationScope, spanScope); applyCommonSpanAttributes(spanJSON, serializedSegmentSpan, client, finalScopeData); client.emit("preprocessSpan", spanJSON); if (spanJSON.is_segment) { applyScopeToSegmentSpan(spanJSON, finalScopeData); applySdkMetadataToSegmentSpan(spanJSON, client); client.emit("processSegmentSpan", spanJSON); } client.emit("processSpan", spanJSON); const { beforeSendSpan: beforeSendSpan$1, traceLifecycle } = client.getOptions(); const processedSpan = ( // check for traceLifecycle here because in static lifecycle, // captureSpan is called for INP spans. If an unmigrated beforeSendSpan // callback is run on these spans, it will throw an error. traceLifecycle !== "static" && beforeSendSpan$1 && !beforeSendSpan.isStaticBeforeSendSpanCallback(beforeSendSpan$1) ? beforeSendSpan.applyBeforeSendSpanCallback(spanJSON, beforeSendSpan$1) : spanJSON ); return { ...spanUtils.streamedSpanJsonToSerializedSpan(processedSpan), _segmentSpan: segmentSpan }; } function applyScopeToSegmentSpan(segmentSpanJSON, scopeData) { const contextAttributes = scopeContextAttributes.scopeContextsToSpanAttributes(scopeData.contexts); safeSetSpanJSONAttributes(segmentSpanJSON, contextAttributes); } 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, { [semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_SDK_INTEGRATIONS]: integrationNames }); } function commonSpanAttributes(serializedSegmentSpan, client, scopeData, includeScopeAttributes = true) { const sdk = client.getSdkMetadata(); const { release, environment } = client.getOptions(); return { [attributes.SENTRY_TRACE_LIFECYCLE]: "stream", [attributes.SENTRY_SEGMENT_NAME]: serializedSegmentSpan.name, [attributes.SENTRY_SEGMENT_ID]: serializedSegmentSpan.span_id, [attributes.SENTRY_SDK_NAME]: sdk?.sdk?.name, [attributes.SENTRY_SDK_VERSION]: sdk?.sdk?.version, [semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_RELEASE]: release, [semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT]: environment || constants.DEFAULT_ENVIRONMENT, [semanticAttributes.SEMANTIC_ATTRIBUTE_USER_ID]: scopeData.user?.id, [semanticAttributes.SEMANTIC_ATTRIBUTE_USER_EMAIL]: scopeData.user?.email, [semanticAttributes.SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS]: scopeData.user?.ip_address, [semanticAttributes.SEMANTIC_ATTRIBUTE_USER_USERNAME]: scopeData.user?.username, ...includeScopeAttributes ? scopeData.attributes : void 0 }; } function applyCommonSpanAttributes(spanJSON, serializedSegmentSpan, client, scopeData) { safeSetSpanJSONAttributes(spanJSON, commonSpanAttributes(serializedSegmentSpan, client, scopeData)); } function captureStandaloneSpanWithStaticCallback(span, client, beforeSendSpan$1) { const spanJSON = spanUtils.spanToStaticSpanJSON(span); const segmentSpan = spanUtils.INTERNAL_getSegmentSpan(span); const serializedSegmentSpan = spanUtils.spanToJSON(segmentSpan); const { isolationScope: spanIsolationScope, scope: spanScope } = utils.getCapturedScopesOnSpan(span); const finalScopeData = scopeData.getCombinedScopeData(spanIsolationScope, spanScope); const commonAttributes = commonSpanAttributes(serializedSegmentSpan, client, finalScopeData, false); Object.entries(commonAttributes).forEach(([key, value]) => { if (value != null && !(key in spanJSON.data)) { spanJSON.data[key] = value; } }); const processedSpan = beforeSendSpan.applyBeforeSendSpanCallback(spanJSON, beforeSendSpan$1); return spanJsonToStreamedSpan.spanJsonToSerializedStreamedSpan(processedSpan); } exports.captureSpan = captureSpan; exports.captureStandaloneSpanWithStaticCallback = captureStandaloneSpanWithStaticCallback; exports.safeSetSpanJSONAttributes = safeSetSpanJSONAttributes; //# sourceMappingURL=captureSpan.js.map