dedupe.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const utils = require('@sentry/utils');
  4. const debugBuild = require('./debug-build.js');
  5. const INTEGRATION_NAME = 'Dedupe';
  6. const _dedupeIntegration = (() => {
  7. let previousEvent;
  8. return {
  9. name: INTEGRATION_NAME,
  10. // TODO v8: Remove this
  11. setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
  12. processEvent(currentEvent) {
  13. // We want to ignore any non-error type events, e.g. transactions or replays
  14. // These should never be deduped, and also not be compared against as _previousEvent.
  15. if (currentEvent.type) {
  16. return currentEvent;
  17. }
  18. // Juuust in case something goes wrong
  19. try {
  20. if (_shouldDropEvent(currentEvent, previousEvent)) {
  21. debugBuild.DEBUG_BUILD && utils.logger.warn('Event dropped due to being a duplicate of previously captured event.');
  22. return null;
  23. }
  24. } catch (_oO) {} // eslint-disable-line no-empty
  25. return (previousEvent = currentEvent);
  26. },
  27. };
  28. }) ;
  29. const dedupeIntegration = core.defineIntegration(_dedupeIntegration);
  30. /**
  31. * Deduplication filter.
  32. * @deprecated Use `dedupeIntegration()` instead.
  33. */
  34. // eslint-disable-next-line deprecation/deprecation
  35. const Dedupe = core.convertIntegrationFnToClass(INTEGRATION_NAME, dedupeIntegration)
  36. ;
  37. /** only exported for tests. */
  38. function _shouldDropEvent(currentEvent, previousEvent) {
  39. if (!previousEvent) {
  40. return false;
  41. }
  42. if (_isSameMessageEvent(currentEvent, previousEvent)) {
  43. return true;
  44. }
  45. if (_isSameExceptionEvent(currentEvent, previousEvent)) {
  46. return true;
  47. }
  48. return false;
  49. }
  50. function _isSameMessageEvent(currentEvent, previousEvent) {
  51. const currentMessage = currentEvent.message;
  52. const previousMessage = previousEvent.message;
  53. // If neither event has a message property, they were both exceptions, so bail out
  54. if (!currentMessage && !previousMessage) {
  55. return false;
  56. }
  57. // If only one event has a stacktrace, but not the other one, they are not the same
  58. if ((currentMessage && !previousMessage) || (!currentMessage && previousMessage)) {
  59. return false;
  60. }
  61. if (currentMessage !== previousMessage) {
  62. return false;
  63. }
  64. if (!_isSameFingerprint(currentEvent, previousEvent)) {
  65. return false;
  66. }
  67. if (!_isSameStacktrace(currentEvent, previousEvent)) {
  68. return false;
  69. }
  70. return true;
  71. }
  72. function _isSameExceptionEvent(currentEvent, previousEvent) {
  73. const previousException = _getExceptionFromEvent(previousEvent);
  74. const currentException = _getExceptionFromEvent(currentEvent);
  75. if (!previousException || !currentException) {
  76. return false;
  77. }
  78. if (previousException.type !== currentException.type || previousException.value !== currentException.value) {
  79. return false;
  80. }
  81. if (!_isSameFingerprint(currentEvent, previousEvent)) {
  82. return false;
  83. }
  84. if (!_isSameStacktrace(currentEvent, previousEvent)) {
  85. return false;
  86. }
  87. return true;
  88. }
  89. function _isSameStacktrace(currentEvent, previousEvent) {
  90. let currentFrames = _getFramesFromEvent(currentEvent);
  91. let previousFrames = _getFramesFromEvent(previousEvent);
  92. // If neither event has a stacktrace, they are assumed to be the same
  93. if (!currentFrames && !previousFrames) {
  94. return true;
  95. }
  96. // If only one event has a stacktrace, but not the other one, they are not the same
  97. if ((currentFrames && !previousFrames) || (!currentFrames && previousFrames)) {
  98. return false;
  99. }
  100. currentFrames = currentFrames ;
  101. previousFrames = previousFrames ;
  102. // If number of frames differ, they are not the same
  103. if (previousFrames.length !== currentFrames.length) {
  104. return false;
  105. }
  106. // Otherwise, compare the two
  107. for (let i = 0; i < previousFrames.length; i++) {
  108. const frameA = previousFrames[i];
  109. const frameB = currentFrames[i];
  110. if (
  111. frameA.filename !== frameB.filename ||
  112. frameA.lineno !== frameB.lineno ||
  113. frameA.colno !== frameB.colno ||
  114. frameA.function !== frameB.function
  115. ) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. function _isSameFingerprint(currentEvent, previousEvent) {
  122. let currentFingerprint = currentEvent.fingerprint;
  123. let previousFingerprint = previousEvent.fingerprint;
  124. // If neither event has a fingerprint, they are assumed to be the same
  125. if (!currentFingerprint && !previousFingerprint) {
  126. return true;
  127. }
  128. // If only one event has a fingerprint, but not the other one, they are not the same
  129. if ((currentFingerprint && !previousFingerprint) || (!currentFingerprint && previousFingerprint)) {
  130. return false;
  131. }
  132. currentFingerprint = currentFingerprint ;
  133. previousFingerprint = previousFingerprint ;
  134. // Otherwise, compare the two
  135. try {
  136. return !!(currentFingerprint.join('') === previousFingerprint.join(''));
  137. } catch (_oO) {
  138. return false;
  139. }
  140. }
  141. function _getExceptionFromEvent(event) {
  142. return event.exception && event.exception.values && event.exception.values[0];
  143. }
  144. function _getFramesFromEvent(event) {
  145. const exception = event.exception;
  146. if (exception) {
  147. try {
  148. // @ts-expect-error Object could be undefined
  149. return exception.values[0].stacktrace.frames;
  150. } catch (_oO) {
  151. return undefined;
  152. }
  153. }
  154. return undefined;
  155. }
  156. exports.Dedupe = Dedupe;
  157. exports._shouldDropEvent = _shouldDropEvent;
  158. exports.dedupeIntegration = dedupeIntegration;
  159. //# sourceMappingURL=dedupe.js.map