applyScopeDataToEvent.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const utils = require('@sentry/utils');
  3. const dynamicSamplingContext = require('../tracing/dynamicSamplingContext.js');
  4. const getRootSpan = require('./getRootSpan.js');
  5. const spanUtils = require('./spanUtils.js');
  6. /**
  7. * Applies data from the scope to the event and runs all event processors on it.
  8. */
  9. function applyScopeDataToEvent(event, data) {
  10. const { fingerprint, span, breadcrumbs, sdkProcessingMetadata } = data;
  11. // Apply general data
  12. applyDataToEvent(event, data);
  13. // We want to set the trace context for normal events only if there isn't already
  14. // a trace context on the event. There is a product feature in place where we link
  15. // errors with transaction and it relies on that.
  16. if (span) {
  17. applySpanToEvent(event, span);
  18. }
  19. applyFingerprintToEvent(event, fingerprint);
  20. applyBreadcrumbsToEvent(event, breadcrumbs);
  21. applySdkMetadataToEvent(event, sdkProcessingMetadata);
  22. }
  23. /** Merge data of two scopes together. */
  24. function mergeScopeData(data, mergeData) {
  25. const {
  26. extra,
  27. tags,
  28. user,
  29. contexts,
  30. level,
  31. sdkProcessingMetadata,
  32. breadcrumbs,
  33. fingerprint,
  34. eventProcessors,
  35. attachments,
  36. propagationContext,
  37. // eslint-disable-next-line deprecation/deprecation
  38. transactionName,
  39. span,
  40. } = mergeData;
  41. mergeAndOverwriteScopeData(data, 'extra', extra);
  42. mergeAndOverwriteScopeData(data, 'tags', tags);
  43. mergeAndOverwriteScopeData(data, 'user', user);
  44. mergeAndOverwriteScopeData(data, 'contexts', contexts);
  45. mergeAndOverwriteScopeData(data, 'sdkProcessingMetadata', sdkProcessingMetadata);
  46. if (level) {
  47. data.level = level;
  48. }
  49. if (transactionName) {
  50. // eslint-disable-next-line deprecation/deprecation
  51. data.transactionName = transactionName;
  52. }
  53. if (span) {
  54. data.span = span;
  55. }
  56. if (breadcrumbs.length) {
  57. data.breadcrumbs = [...data.breadcrumbs, ...breadcrumbs];
  58. }
  59. if (fingerprint.length) {
  60. data.fingerprint = [...data.fingerprint, ...fingerprint];
  61. }
  62. if (eventProcessors.length) {
  63. data.eventProcessors = [...data.eventProcessors, ...eventProcessors];
  64. }
  65. if (attachments.length) {
  66. data.attachments = [...data.attachments, ...attachments];
  67. }
  68. data.propagationContext = { ...data.propagationContext, ...propagationContext };
  69. }
  70. /**
  71. * Merges certain scope data. Undefined values will overwrite any existing values.
  72. * Exported only for tests.
  73. */
  74. function mergeAndOverwriteScopeData
  75. (data, prop, mergeVal) {
  76. if (mergeVal && Object.keys(mergeVal).length) {
  77. // Clone object
  78. data[prop] = { ...data[prop] };
  79. for (const key in mergeVal) {
  80. if (Object.prototype.hasOwnProperty.call(mergeVal, key)) {
  81. data[prop][key] = mergeVal[key];
  82. }
  83. }
  84. }
  85. }
  86. function applyDataToEvent(event, data) {
  87. const {
  88. extra,
  89. tags,
  90. user,
  91. contexts,
  92. level,
  93. // eslint-disable-next-line deprecation/deprecation
  94. transactionName,
  95. } = data;
  96. const cleanedExtra = utils.dropUndefinedKeys(extra);
  97. if (cleanedExtra && Object.keys(cleanedExtra).length) {
  98. event.extra = { ...cleanedExtra, ...event.extra };
  99. }
  100. const cleanedTags = utils.dropUndefinedKeys(tags);
  101. if (cleanedTags && Object.keys(cleanedTags).length) {
  102. event.tags = { ...cleanedTags, ...event.tags };
  103. }
  104. const cleanedUser = utils.dropUndefinedKeys(user);
  105. if (cleanedUser && Object.keys(cleanedUser).length) {
  106. event.user = { ...cleanedUser, ...event.user };
  107. }
  108. const cleanedContexts = utils.dropUndefinedKeys(contexts);
  109. if (cleanedContexts && Object.keys(cleanedContexts).length) {
  110. event.contexts = { ...cleanedContexts, ...event.contexts };
  111. }
  112. if (level) {
  113. event.level = level;
  114. }
  115. if (transactionName) {
  116. event.transaction = transactionName;
  117. }
  118. }
  119. function applyBreadcrumbsToEvent(event, breadcrumbs) {
  120. const mergedBreadcrumbs = [...(event.breadcrumbs || []), ...breadcrumbs];
  121. event.breadcrumbs = mergedBreadcrumbs.length ? mergedBreadcrumbs : undefined;
  122. }
  123. function applySdkMetadataToEvent(event, sdkProcessingMetadata) {
  124. event.sdkProcessingMetadata = {
  125. ...event.sdkProcessingMetadata,
  126. ...sdkProcessingMetadata,
  127. };
  128. }
  129. function applySpanToEvent(event, span) {
  130. event.contexts = { trace: spanUtils.spanToTraceContext(span), ...event.contexts };
  131. const rootSpan = getRootSpan.getRootSpan(span);
  132. if (rootSpan) {
  133. event.sdkProcessingMetadata = {
  134. dynamicSamplingContext: dynamicSamplingContext.getDynamicSamplingContextFromSpan(span),
  135. ...event.sdkProcessingMetadata,
  136. };
  137. const transactionName = spanUtils.spanToJSON(rootSpan).description;
  138. if (transactionName) {
  139. event.tags = { transaction: transactionName, ...event.tags };
  140. }
  141. }
  142. }
  143. /**
  144. * Applies fingerprint from the scope to the event if there's one,
  145. * uses message if there's one instead or get rid of empty fingerprint
  146. */
  147. function applyFingerprintToEvent(event, fingerprint) {
  148. // Make sure it's an array first and we actually have something in place
  149. event.fingerprint = event.fingerprint ? utils.arrayify(event.fingerprint) : [];
  150. // If we have something on the scope, then merge it with event
  151. if (fingerprint) {
  152. event.fingerprint = event.fingerprint.concat(fingerprint);
  153. }
  154. // If we have no data at all, remove empty array default
  155. if (event.fingerprint && !event.fingerprint.length) {
  156. delete event.fingerprint;
  157. }
  158. }
  159. exports.applyScopeDataToEvent = applyScopeDataToEvent;
  160. exports.mergeAndOverwriteScopeData = mergeAndOverwriteScopeData;
  161. exports.mergeScopeData = mergeScopeData;
  162. //# sourceMappingURL=applyScopeDataToEvent.js.map