dedupe.js 5.2 KB

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