|
| 1 | +import { configureScope } from '@sentry/minimal'; |
| 2 | +import { Integration, SentryEvent, SentryException, StackFrame } from '@sentry/types'; |
| 3 | +import { getEventDescription } from '@sentry/utils/misc'; |
| 4 | +import { logger } from '../logger'; |
| 5 | + |
| 6 | +/** Deduplication filter */ |
| 7 | +export class Dedupe implements Integration { |
| 8 | + /** |
| 9 | + * @inheritDoc |
| 10 | + */ |
| 11 | + private previousEvent?: SentryEvent; |
| 12 | + |
| 13 | + /** |
| 14 | + * @inheritDoc |
| 15 | + */ |
| 16 | + public name: string = 'Dedupe'; |
| 17 | + |
| 18 | + /** |
| 19 | + * @inheritDoc |
| 20 | + */ |
| 21 | + public install(): void { |
| 22 | + configureScope(scope => { |
| 23 | + scope.addEventProcessor(async (currentEvent: SentryEvent) => { |
| 24 | + // Juuust in case something goes wrong |
| 25 | + try { |
| 26 | + if (this.shouldDropEvent(currentEvent, this.previousEvent)) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + } catch (_oO) { |
| 30 | + return (this.previousEvent = currentEvent); |
| 31 | + } |
| 32 | + |
| 33 | + return (this.previousEvent = currentEvent); |
| 34 | + }); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + /** JSDoc */ |
| 39 | + public shouldDropEvent(currentEvent: SentryEvent, previousEvent?: SentryEvent): boolean { |
| 40 | + if (!previousEvent) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + if (this.isSameMessageEvent(currentEvent, previousEvent)) { |
| 45 | + logger.warn( |
| 46 | + `Event dropped due to being a duplicate of previous event (same message).\nEvent: ${getEventDescription( |
| 47 | + currentEvent, |
| 48 | + )}`, |
| 49 | + ); |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + if (this.isSameExceptionEvent(currentEvent, previousEvent)) { |
| 54 | + logger.warn( |
| 55 | + `Event dropped due to being a duplicate of previous event (same exception).\nEvent: ${getEventDescription( |
| 56 | + currentEvent, |
| 57 | + )}`, |
| 58 | + ); |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + /** JSDoc */ |
| 66 | + private isSameMessageEvent(currentEvent: SentryEvent, previousEvent: SentryEvent): boolean { |
| 67 | + const currentMessage = currentEvent.message; |
| 68 | + const previousMessage = previousEvent.message; |
| 69 | + |
| 70 | + // If no event has a message, they were both exceptions, so bail out |
| 71 | + if (!currentMessage && !previousMessage) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + // If only one event has a stacktrace, but not the other one, they are not the same |
| 76 | + if ((currentMessage && !previousMessage) || (!currentMessage && previousMessage)) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + if (currentMessage !== previousMessage) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + if (!this.isSameFingerprint(currentEvent, previousEvent)) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + if (!this.isSameStacktrace(currentEvent, previousEvent)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + /** JSDoc */ |
| 96 | + private getFramesFromEvent(event: SentryEvent): StackFrame[] | undefined { |
| 97 | + const exception = event.exception; |
| 98 | + |
| 99 | + if (exception) { |
| 100 | + try { |
| 101 | + // @ts-ignore |
| 102 | + return exception.values[0].stacktrace.frames; |
| 103 | + } catch (_oO) { |
| 104 | + return undefined; |
| 105 | + } |
| 106 | + } else if (event.stacktrace) { |
| 107 | + return event.stacktrace.frames; |
| 108 | + } else { |
| 109 | + return undefined; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** JSDoc */ |
| 114 | + private isSameStacktrace(currentEvent: SentryEvent, previousEvent: SentryEvent): boolean { |
| 115 | + let currentFrames = this.getFramesFromEvent(currentEvent); |
| 116 | + let previousFrames = this.getFramesFromEvent(previousEvent); |
| 117 | + |
| 118 | + // If no event has a fingerprint, they are assumed to be the same |
| 119 | + if (!currentFrames && !previousFrames) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + // If only one event has a stacktrace, but not the other one, they are not the same |
| 124 | + if ((currentFrames && !previousFrames) || (!currentFrames && previousFrames)) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + currentFrames = currentFrames as StackFrame[]; |
| 129 | + previousFrames = previousFrames as StackFrame[]; |
| 130 | + |
| 131 | + // If number of frames differ, they are not the same |
| 132 | + if (previousFrames.length !== currentFrames.length) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + // Otherwise, compare the two |
| 137 | + for (let i = 0; i < previousFrames.length; i++) { |
| 138 | + const frameA = previousFrames[i]; |
| 139 | + const frameB = currentFrames[i]; |
| 140 | + |
| 141 | + if ( |
| 142 | + frameA.filename !== frameB.filename || |
| 143 | + frameA.lineno !== frameB.lineno || |
| 144 | + frameA.colno !== frameB.colno || |
| 145 | + frameA.function !== frameB.function |
| 146 | + ) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return true; |
| 152 | + } |
| 153 | + |
| 154 | + /** JSDoc */ |
| 155 | + private getExceptionFromEvent(event: SentryEvent): SentryException | undefined { |
| 156 | + return event.exception && event.exception.values && event.exception.values[0]; |
| 157 | + } |
| 158 | + |
| 159 | + /** JSDoc */ |
| 160 | + private isSameExceptionEvent(currentEvent: SentryEvent, previousEvent: SentryEvent): boolean { |
| 161 | + const previousException = this.getExceptionFromEvent(previousEvent); |
| 162 | + const currentException = this.getExceptionFromEvent(currentEvent); |
| 163 | + |
| 164 | + if (!previousException || !currentException) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + if (previousException.type !== currentException.type || previousException.value !== currentException.value) { |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + if (!this.isSameFingerprint(currentEvent, previousEvent)) { |
| 173 | + return false; |
| 174 | + } |
| 175 | + |
| 176 | + if (!this.isSameStacktrace(currentEvent, previousEvent)) { |
| 177 | + return false; |
| 178 | + } |
| 179 | + |
| 180 | + return true; |
| 181 | + } |
| 182 | + |
| 183 | + /** JSDoc */ |
| 184 | + private isSameFingerprint(currentEvent: SentryEvent, previousEvent: SentryEvent): boolean { |
| 185 | + let currentFingerprint = currentEvent.fingerprint; |
| 186 | + let previousFingerprint = previousEvent.fingerprint; |
| 187 | + |
| 188 | + // If no event has a fingerprint, they are assumed to be the same |
| 189 | + if (!currentFingerprint && !previousFingerprint) { |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
| 193 | + // If only one event has a fingerprint, but not the other one, they are not the same |
| 194 | + if ((currentFingerprint && !previousFingerprint) || (!currentFingerprint && previousFingerprint)) { |
| 195 | + return false; |
| 196 | + } |
| 197 | + |
| 198 | + currentFingerprint = currentFingerprint as string[]; |
| 199 | + previousFingerprint = previousFingerprint as string[]; |
| 200 | + |
| 201 | + // Otherwise, compare the two |
| 202 | + try { |
| 203 | + return !!(currentFingerprint.join('') === previousFingerprint.join('')); |
| 204 | + } catch (_oO) { |
| 205 | + return false; |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments