dedupe.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. function _shouldDropEvent(currentEvent, previousEvent) {
  38. if (!previousEvent) {
  39. return false;
  40. }
  41. if (_isSameMessageEvent(currentEvent, previousEvent)) {
  42. return true;
  43. }
  44. if (_isSameExceptionEvent(currentEvent, previousEvent)) {
  45. return true;
  46. }
  47. return false;
  48. }
  49. function _isSameMessageEvent(currentEvent, previousEvent) {
  50. const currentMessage = currentEvent.message;
  51. const previousMessage = previousEvent.message;
  52. // If neither event has a message property, they were both exceptions, so bail out
  53. if (!currentMessage && !previousMessage) {
  54. return false;
  55. }
  56. // If only one event has a stacktrace, but not the other one, they are not the same
  57. if ((currentMessage && !previousMessage) || (!currentMessage && previousMessage)) {
  58. return false;
  59. }
  60. if (currentMessage !== previousMessage) {
  61. return false;
  62. }
  63. if (!_isSameFingerprint(currentEvent, previousEvent)) {
  64. return false;
  65. }
  66. if (!_isSameStacktrace(currentEvent, previousEvent)) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. function _isSameExceptionEvent(currentEvent, previousEvent) {
  72. const previousException = _getExceptionFromEvent(previousEvent);
  73. const currentException = _getExceptionFromEvent(currentEvent);
  74. if (!previousException || !currentException) {
  75. return false;
  76. }
  77. if (previousException.type !== currentException.type || previousException.value !== currentException.value) {
  78. return false;
  79. }
  80. if (!_isSameFingerprint(currentEvent, previousEvent)) {
  81. return false;
  82. }
  83. if (!_isSameStacktrace(currentEvent, previousEvent)) {
  84. return false;
  85. }
  86. return true;
  87. }
  88. function _isSameStacktrace(currentEvent, previousEvent) {
  89. let currentFrames = _getFramesFromEvent(currentEvent);
  90. let previousFrames = _getFramesFromEvent(previousEvent);
  91. // If neither event has a stacktrace, they are assumed to be the same
  92. if (!currentFrames && !previousFrames) {
  93. return true;
  94. }
  95. // If only one event has a stacktrace, but not the other one, they are not the same
  96. if ((currentFrames && !previousFrames) || (!currentFrames && previousFrames)) {
  97. return false;
  98. }
  99. currentFrames = currentFrames ;
  100. previousFrames = previousFrames ;
  101. // If number of frames differ, they are not the same
  102. if (previousFrames.length !== currentFrames.length) {
  103. return false;
  104. }
  105. // Otherwise, compare the two
  106. for (let i = 0; i < previousFrames.length; i++) {
  107. const frameA = previousFrames[i];
  108. const frameB = currentFrames[i];
  109. if (
  110. frameA.filename !== frameB.filename ||
  111. frameA.lineno !== frameB.lineno ||
  112. frameA.colno !== frameB.colno ||
  113. frameA.function !== frameB.function
  114. ) {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. function _isSameFingerprint(currentEvent, previousEvent) {
  121. let currentFingerprint = currentEvent.fingerprint;
  122. let previousFingerprint = previousEvent.fingerprint;
  123. // If neither event has a fingerprint, they are assumed to be the same
  124. if (!currentFingerprint && !previousFingerprint) {
  125. return true;
  126. }
  127. // If only one event has a fingerprint, but not the other one, they are not the same
  128. if ((currentFingerprint && !previousFingerprint) || (!currentFingerprint && previousFingerprint)) {
  129. return false;
  130. }
  131. currentFingerprint = currentFingerprint ;
  132. previousFingerprint = previousFingerprint ;
  133. // Otherwise, compare the two
  134. try {
  135. return !!(currentFingerprint.join('') === previousFingerprint.join(''));
  136. } catch (_oO) {
  137. return false;
  138. }
  139. }
  140. function _getExceptionFromEvent(event) {
  141. return event.exception && event.exception.values && event.exception.values[0];
  142. }
  143. function _getFramesFromEvent(event) {
  144. const exception = event.exception;
  145. if (exception) {
  146. try {
  147. // @ts-expect-error Object could be undefined
  148. return exception.values[0].stacktrace.frames;
  149. } catch (_oO) {
  150. return undefined;
  151. }
  152. }
  153. return undefined;
  154. }
  155. exports.Dedupe = Dedupe;
  156. exports.dedupeIntegration = dedupeIntegration;
  157. //# sourceMappingURL=dedupe.js.map