123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- Object.defineProperty(exports, '__esModule', { value: true });
- const utils = require('@sentry/utils');
- const dynamicSamplingContext = require('../tracing/dynamicSamplingContext.js');
- const getRootSpan = require('./getRootSpan.js');
- const spanUtils = require('./spanUtils.js');
- /**
- * Applies data from the scope to the event and runs all event processors on it.
- */
- function applyScopeDataToEvent(event, data) {
- const { fingerprint, span, breadcrumbs, sdkProcessingMetadata } = data;
- // Apply general data
- applyDataToEvent(event, data);
- // We want to set the trace context for normal events only if there isn't already
- // a trace context on the event. There is a product feature in place where we link
- // errors with transaction and it relies on that.
- if (span) {
- applySpanToEvent(event, span);
- }
- applyFingerprintToEvent(event, fingerprint);
- applyBreadcrumbsToEvent(event, breadcrumbs);
- applySdkMetadataToEvent(event, sdkProcessingMetadata);
- }
- /** Merge data of two scopes together. */
- function mergeScopeData(data, mergeData) {
- const {
- extra,
- tags,
- user,
- contexts,
- level,
- sdkProcessingMetadata,
- breadcrumbs,
- fingerprint,
- eventProcessors,
- attachments,
- propagationContext,
- // eslint-disable-next-line deprecation/deprecation
- transactionName,
- span,
- } = mergeData;
- mergeAndOverwriteScopeData(data, 'extra', extra);
- mergeAndOverwriteScopeData(data, 'tags', tags);
- mergeAndOverwriteScopeData(data, 'user', user);
- mergeAndOverwriteScopeData(data, 'contexts', contexts);
- mergeAndOverwriteScopeData(data, 'sdkProcessingMetadata', sdkProcessingMetadata);
- if (level) {
- data.level = level;
- }
- if (transactionName) {
- // eslint-disable-next-line deprecation/deprecation
- data.transactionName = transactionName;
- }
- if (span) {
- data.span = span;
- }
- if (breadcrumbs.length) {
- data.breadcrumbs = [...data.breadcrumbs, ...breadcrumbs];
- }
- if (fingerprint.length) {
- data.fingerprint = [...data.fingerprint, ...fingerprint];
- }
- if (eventProcessors.length) {
- data.eventProcessors = [...data.eventProcessors, ...eventProcessors];
- }
- if (attachments.length) {
- data.attachments = [...data.attachments, ...attachments];
- }
- data.propagationContext = { ...data.propagationContext, ...propagationContext };
- }
- /**
- * Merges certain scope data. Undefined values will overwrite any existing values.
- * Exported only for tests.
- */
- function mergeAndOverwriteScopeData
- (data, prop, mergeVal) {
- if (mergeVal && Object.keys(mergeVal).length) {
- // Clone object
- data[prop] = { ...data[prop] };
- for (const key in mergeVal) {
- if (Object.prototype.hasOwnProperty.call(mergeVal, key)) {
- data[prop][key] = mergeVal[key];
- }
- }
- }
- }
- function applyDataToEvent(event, data) {
- const {
- extra,
- tags,
- user,
- contexts,
- level,
- // eslint-disable-next-line deprecation/deprecation
- transactionName,
- } = data;
- const cleanedExtra = utils.dropUndefinedKeys(extra);
- if (cleanedExtra && Object.keys(cleanedExtra).length) {
- event.extra = { ...cleanedExtra, ...event.extra };
- }
- const cleanedTags = utils.dropUndefinedKeys(tags);
- if (cleanedTags && Object.keys(cleanedTags).length) {
- event.tags = { ...cleanedTags, ...event.tags };
- }
- const cleanedUser = utils.dropUndefinedKeys(user);
- if (cleanedUser && Object.keys(cleanedUser).length) {
- event.user = { ...cleanedUser, ...event.user };
- }
- const cleanedContexts = utils.dropUndefinedKeys(contexts);
- if (cleanedContexts && Object.keys(cleanedContexts).length) {
- event.contexts = { ...cleanedContexts, ...event.contexts };
- }
- if (level) {
- event.level = level;
- }
- if (transactionName) {
- event.transaction = transactionName;
- }
- }
- function applyBreadcrumbsToEvent(event, breadcrumbs) {
- const mergedBreadcrumbs = [...(event.breadcrumbs || []), ...breadcrumbs];
- event.breadcrumbs = mergedBreadcrumbs.length ? mergedBreadcrumbs : undefined;
- }
- function applySdkMetadataToEvent(event, sdkProcessingMetadata) {
- event.sdkProcessingMetadata = {
- ...event.sdkProcessingMetadata,
- ...sdkProcessingMetadata,
- };
- }
- function applySpanToEvent(event, span) {
- event.contexts = { trace: spanUtils.spanToTraceContext(span), ...event.contexts };
- const rootSpan = getRootSpan.getRootSpan(span);
- if (rootSpan) {
- event.sdkProcessingMetadata = {
- dynamicSamplingContext: dynamicSamplingContext.getDynamicSamplingContextFromSpan(span),
- ...event.sdkProcessingMetadata,
- };
- const transactionName = spanUtils.spanToJSON(rootSpan).description;
- if (transactionName) {
- event.tags = { transaction: transactionName, ...event.tags };
- }
- }
- }
- /**
- * Applies fingerprint from the scope to the event if there's one,
- * uses message if there's one instead or get rid of empty fingerprint
- */
- function applyFingerprintToEvent(event, fingerprint) {
- // Make sure it's an array first and we actually have something in place
- event.fingerprint = event.fingerprint ? utils.arrayify(event.fingerprint) : [];
- // If we have something on the scope, then merge it with event
- if (fingerprint) {
- event.fingerprint = event.fingerprint.concat(fingerprint);
- }
- // If we have no data at all, remove empty array default
- if (event.fingerprint && !event.fingerprint.length) {
- delete event.fingerprint;
- }
- }
- exports.applyScopeDataToEvent = applyScopeDataToEvent;
- exports.mergeAndOverwriteScopeData = mergeAndOverwriteScopeData;
- exports.mergeScopeData = mergeScopeData;
- //# sourceMappingURL=applyScopeDataToEvent.js.map
|