|
| 1 | +import { IInnerError } from '../../models/IInnerError'; |
| 2 | +import { IStackFrame } from '../../models/IStackFrame'; |
| 3 | + |
| 4 | +import { ILog } from '../../logging/ILog'; |
| 5 | + |
| 6 | +import { IEventPlugin } from '../IEventPlugin'; |
| 7 | +import { EventPluginContext } from '../EventPluginContext'; |
| 8 | + |
| 9 | +const ERROR_KEY: string = '@error'; |
| 10 | +const WINDOW_MILLISECONDS = 2000; |
| 11 | +const MAX_QUEUE_LENGTH = 10; |
| 12 | + |
| 13 | +export class DuplicateCheckerPlugin implements IEventPlugin { |
| 14 | + public priority: number = 40; |
| 15 | + public name: string = 'DuplicateCheckerPlugin'; |
| 16 | + |
| 17 | + private recentlyProcessedErrors: TimestampedHash[] = []; |
| 18 | + |
| 19 | + public run(context: EventPluginContext, next?: () => void): void { |
| 20 | + if (context.event.type === 'error') { |
| 21 | + let error = context.event.data[ERROR_KEY]; |
| 22 | + let isDuplicate = this.checkDuplicate(error, context.log); |
| 23 | + if (isDuplicate) { |
| 24 | + context.cancelled = true; |
| 25 | + return; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + next && next(); |
| 30 | + } |
| 31 | + |
| 32 | + private getNow() { |
| 33 | + return Date.now(); |
| 34 | + } |
| 35 | + |
| 36 | + private checkDuplicate(error: IInnerError, log: ILog): boolean { |
| 37 | + let now = this.getNow(); |
| 38 | + let repeatWindow = now - WINDOW_MILLISECONDS; |
| 39 | + let hashCode: number; |
| 40 | + while (error) { |
| 41 | + hashCode = getHashCodeForError(error); |
| 42 | + |
| 43 | + // make sure that we don't process the same error multiple times within the repeat window |
| 44 | + if (hashCode && this.recentlyProcessedErrors.some(h => |
| 45 | + h.hash == hashCode && h.timestamp >= repeatWindow)) { |
| 46 | + log.info(`Ignoring duplicate error event: hash=${hashCode}`); |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + // add this exception to our list of recent errors that we have processed |
| 51 | + this.recentlyProcessedErrors.push({ hash: hashCode, timestamp: now }); |
| 52 | + |
| 53 | + // only keep the last 10 recent errors |
| 54 | + while (this.recentlyProcessedErrors.length > MAX_QUEUE_LENGTH) { |
| 55 | + this.recentlyProcessedErrors.shift(); |
| 56 | + } |
| 57 | + |
| 58 | + error = error.inner; |
| 59 | + } |
| 60 | + |
| 61 | + return false; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +interface TimestampedHash { |
| 66 | + hash: number; |
| 67 | + timestamp: number |
| 68 | +} |
| 69 | + |
| 70 | +function getHashCodeForError(error: IInnerError): number { |
| 71 | + if (!error.stack_trace) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + let stack = JSON.stringify(error.stack_trace); |
| 76 | + return getHashCode(stack); |
| 77 | +} |
| 78 | + |
| 79 | +function getHashCode(s: string): number { |
| 80 | + let hash = 0, length = s.length, char; |
| 81 | + for (let i = 0; i < length; i++) { |
| 82 | + char = s.charCodeAt(i); |
| 83 | + hash = ((hash << 5) - hash) + char; |
| 84 | + hash |= 0; |
| 85 | + } |
| 86 | + return hash; |
| 87 | +} |
0 commit comments