Repository navigation
Expand file tree
/
Copy pathdedupe.ts
More file actions
178 lines (140 loc) · 5.1 KB
/
Copy pathdedupe.ts
File metadata and controls
178 lines (140 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { defineIntegration } from '../integration';
import type { Event, Exception, IntegrationFn, StackFrame } from '../types-hoist';
import { DEBUG_BUILD } from '../debug-build';
import { logger } from '../utils-hoist/logger';
import { getFramesFromEvent } from '../utils-hoist/stacktrace';
const INTEGRATION_NAME = 'Dedupe';
const _dedupeIntegration = (() => {
let previousEvent: Event | undefined;
return {
name: INTEGRATION_NAME,
processEvent(currentEvent) {
// We want to ignore any non-error type events, e.g. transactions or replays
// These should never be deduped, and also not be compared against as _previousEvent.
if (currentEvent.type) {
return currentEvent;
}
// Juuust in case something goes wrong
try {
if (_shouldDropEvent(currentEvent, previousEvent)) {
DEBUG_BUILD && logger.warn('Event dropped due to being a duplicate of previously captured event.');
return null;
}
} catch (_oO) {} // eslint-disable-line no-empty
return (previousEvent = currentEvent);
},
};
}) satisfies IntegrationFn;
/**
* Deduplication filter.
*/
export const dedupeIntegration = defineIntegration(_dedupeIntegration);
/** only exported for tests. */
export function _shouldDropEvent(currentEvent: Event, previousEvent?: Event): boolean {
if (!previousEvent) {
return false;
}
if (_isSameMessageEvent(currentEvent, previousEvent)) {
return true;
}
if (_isSameExceptionEvent(currentEvent, previousEvent)) {
return true;
}
return false;
}
function _isSameMessageEvent(currentEvent: Event, previousEvent: Event): boolean {
const currentMessage = currentEvent.message;
const previousMessage = previousEvent.message;
// If neither event has a message property, they were both exceptions, so bail out
if (!currentMessage && !previousMessage) {
return false;
}
// If only one event has a stacktrace, but not the other one, they are not the same
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: Event, previousEvent: Event): boolean {
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: Event, previousEvent: Event): boolean {
let currentFrames = getFramesFromEvent(currentEvent);
let previousFrames = getFramesFromEvent(previousEvent);
// If neither event has a stacktrace, they are assumed to be the same
if (!currentFrames && !previousFrames) {
return true;
}
// If only one event has a stacktrace, but not the other one, they are not the same
if ((currentFrames && !previousFrames) || (!currentFrames && previousFrames)) {
return false;
}
currentFrames = currentFrames as StackFrame[];
previousFrames = previousFrames as StackFrame[];
// If number of frames differ, they are not the same
if (previousFrames.length !== currentFrames.length) {
return false;
}
// Otherwise, compare the two
for (let i = 0; i < previousFrames.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const frameA = previousFrames[i]!;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
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: Event, previousEvent: Event): boolean {
let currentFingerprint = currentEvent.fingerprint;
let previousFingerprint = previousEvent.fingerprint;
// If neither event has a fingerprint, they are assumed to be the same
if (!currentFingerprint && !previousFingerprint) {
return true;
}
// If only one event has a fingerprint, but not the other one, they are not the same
if ((currentFingerprint && !previousFingerprint) || (!currentFingerprint && previousFingerprint)) {
return false;
}
currentFingerprint = currentFingerprint as string[];
previousFingerprint = previousFingerprint as string[];
// Otherwise, compare the two
try {
return !!(currentFingerprint.join('') === previousFingerprint.join(''));
} catch (_oO) {
return false;
}
}
function _getExceptionFromEvent(event: Event): Exception | undefined {
return event.exception?.values && event.exception.values[0];
}