/
dedupe.ts
203 lines (165 loc) · 5.6 KB
/
dedupe.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { Event, EventProcessor, Exception, Hub, Integration, StackFrame } from '@sentry/types';
import { logger } from '@sentry/utils';
/** Deduplication filter */
export class Dedupe implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'Dedupe';
/**
* @inheritDoc
*/
public name: string = Dedupe.id;
/**
* @inheritDoc
*/
private _previousEvent?: Event;
/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
addGlobalEventProcessor((currentEvent: Event) => {
const self = getCurrentHub().getIntegration(Dedupe);
if (self) {
// Juuust in case something goes wrong
try {
if (self._shouldDropEvent(currentEvent, self._previousEvent)) {
logger.warn(`Event dropped due to being a duplicate of previously captured event.`);
return null;
}
} catch (_oO) {
return (self._previousEvent = currentEvent);
}
return (self._previousEvent = currentEvent);
}
return currentEvent;
});
}
/** JSDoc */
private _shouldDropEvent(currentEvent: Event, previousEvent?: Event): boolean {
if (!previousEvent) {
return false;
}
if (this._isSameMessageEvent(currentEvent, previousEvent)) {
return true;
}
if (this._isSameExceptionEvent(currentEvent, previousEvent)) {
return true;
}
return false;
}
/** JSDoc */
private _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 (!this._isSameFingerprint(currentEvent, previousEvent)) {
return false;
}
if (!this._isSameStacktrace(currentEvent, previousEvent)) {
return false;
}
return true;
}
/** JSDoc */
private _getFramesFromEvent(event: Event): StackFrame[] | undefined {
const exception = event.exception;
if (exception) {
try {
// @ts-ignore Object could be undefined
return exception.values[0].stacktrace.frames;
} catch (_oO) {
return undefined;
}
} else if (event.stacktrace) {
return event.stacktrace.frames;
}
return undefined;
}
/** JSDoc */
private _isSameStacktrace(currentEvent: Event, previousEvent: Event): boolean {
let currentFrames = this._getFramesFromEvent(currentEvent);
let previousFrames = this._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++) {
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;
}
/** JSDoc */
private _getExceptionFromEvent(event: Event): Exception | undefined {
return event.exception && event.exception.values && event.exception.values[0];
}
/** JSDoc */
private _isSameExceptionEvent(currentEvent: Event, previousEvent: Event): boolean {
const previousException = this._getExceptionFromEvent(previousEvent);
const currentException = this._getExceptionFromEvent(currentEvent);
if (!previousException || !currentException) {
return false;
}
if (previousException.type !== currentException.type || previousException.value !== currentException.value) {
return false;
}
if (!this._isSameFingerprint(currentEvent, previousEvent)) {
return false;
}
if (!this._isSameStacktrace(currentEvent, previousEvent)) {
return false;
}
return true;
}
/** JSDoc */
private _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;
}
}
}