dedupe.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { defineIntegration, convertIntegrationFnToClass } from '@sentry/core';
  2. import { logger } from '@sentry/utils';
  3. import { DEBUG_BUILD } from './debug-build.js';
  4. const INTEGRATION_NAME = 'Dedupe';
  5. const _dedupeIntegration = (() => {
  6. let previousEvent;
  7. return {
  8. name: INTEGRATION_NAME,
  9. // TODO v8: Remove this
  10. setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
  11. processEvent(currentEvent) {
  12. // We want to ignore any non-error type events, e.g. transactions or replays
  13. // These should never be deduped, and also not be compared against as _previousEvent.
  14. if (currentEvent.type) {
  15. return currentEvent;
  16. }
  17. // Juuust in case something goes wrong
  18. try {
  19. if (_shouldDropEvent(currentEvent, previousEvent)) {
  20. DEBUG_BUILD && logger.warn('Event dropped due to being a duplicate of previously captured event.');
  21. return null;
  22. }
  23. } catch (_oO) {} // eslint-disable-line no-empty
  24. return (previousEvent = currentEvent);
  25. },
  26. };
  27. }) ;
  28. const dedupeIntegration = defineIntegration(_dedupeIntegration);
  29. /**
  30. * Deduplication filter.
  31. * @deprecated Use `dedupeIntegration()` instead.
  32. */
  33. // eslint-disable-next-line deprecation/deprecation
  34. const Dedupe = convertIntegrationFnToClass(INTEGRATION_NAME, dedupeIntegration)
  35. ;
  36. /** only exported for tests. */
  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. export { Dedupe, _shouldDropEvent, dedupeIntegration };
  156. //# sourceMappingURL=dedupe.js.map