@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
85 lines (84 loc) • 3.66 kB
JavaScript
import { DEBUG_BUILD } from "../debug-build.js";
import { defineIntegration } from "../integration.js";
import { debug } from "../utils/debug-logger.js";
import { getFramesFromEvent } from "../utils/stacktrace.js";
// src/integrations/dedupe.ts
const INTEGRATION_NAME = "Dedupe";
const _dedupeIntegration = (() => {
let previousEvent;
return {
name: INTEGRATION_NAME,
processEvent(currentEvent) {
if (currentEvent.type) return currentEvent;
try {
if (_shouldDropEvent(currentEvent, previousEvent)) {
DEBUG_BUILD && debug.warn("Event dropped due to being a duplicate of previously captured event.");
return null;
}
} catch {}
return previousEvent = currentEvent;
}
};
});
/**
* Drops an error or message event if it is equal to the previous error or message event.
* Two events are equal if they have the same message (or the same exception type and value),
* the same stack trace and the same fingerprint.
*
* A repeated `captureException` of the same error object is dropped by the client, also without
* this integration (see `checkOrSetAlreadyCaught`).
*/
const dedupeIntegration = defineIntegration(_dedupeIntegration);
/** only exported for tests. */
function _shouldDropEvent(currentEvent, previousEvent) {
if (!previousEvent) return false;
if (_isSameMessageEvent(currentEvent, previousEvent)) return true;
if (_isSameExceptionEvent(currentEvent, previousEvent)) return true;
return false;
}
function _isSameMessageEvent(currentEvent, previousEvent) {
const currentMessage = currentEvent.message;
const previousMessage = previousEvent.message;
if (!currentMessage && !previousMessage) return false;
if (currentMessage && !previousMessage || !currentMessage && previousMessage) return false;
if (currentMessage !== previousMessage) return false;
if (!_isSameFingerprint(currentEvent, previousEvent)) return false;
if (!_isSameStacktrace(currentEvent, previousEvent)) return false;
return true;
}
function _isSameExceptionEvent(currentEvent, previousEvent) {
const previousException = _getExceptionFromEvent(previousEvent);
const currentException = _getExceptionFromEvent(currentEvent);
if (!previousException || !currentException) return false;
if (previousException.type !== currentException.type || previousException.value !== currentException.value) return false;
if (!_isSameFingerprint(currentEvent, previousEvent)) return false;
if (!_isSameStacktrace(currentEvent, previousEvent)) return false;
return true;
}
function _isSameStacktrace(currentEvent, previousEvent) {
const currentFrames = getFramesFromEvent(currentEvent);
const previousFrames = getFramesFromEvent(previousEvent);
if (!currentFrames || !previousFrames) return !currentFrames && !previousFrames;
if (previousFrames.length !== currentFrames.length) return false;
for (let i = 0; i < previousFrames.length; i++) {
const frameA = previousFrames[i];
const frameB = currentFrames[i];
if (frameA.filename !== frameB.filename || frameA.lineno !== frameB.lineno || frameA.colno !== frameB.colno || frameA.function !== frameB.function) return false;
}
return true;
}
function _isSameFingerprint(currentEvent, previousEvent) {
const currentFingerprint = currentEvent.fingerprint;
const previousFingerprint = previousEvent.fingerprint;
if (!currentFingerprint || !previousFingerprint) return !currentFingerprint && !previousFingerprint;
try {
return !!(currentFingerprint.join("") === previousFingerprint.join(""));
} catch {
return false;
}
}
function _getExceptionFromEvent(event) {
return event.exception?.values?.[0];
}
export { _shouldDropEvent, dedupeIntegration };
//# sourceMappingURL=dedupe.js.map