-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathDuplicateCheckerPlugin.ts
110 lines (88 loc) · 3.06 KB
/
DuplicateCheckerPlugin.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
import { IInnerError } from '../../models/IInnerError';
import { IEventPlugin } from '../IEventPlugin';
import { EventPluginContext } from '../EventPluginContext';
import { Utils } from '../../Utils';
export class DuplicateCheckerPlugin implements IEventPlugin {
public priority: number = 1010;
public name: string = 'DuplicateCheckerPlugin';
private _mergedEvents: MergedEvent[] = [];
private _processedHashcodes: TimestampedHash[] = [];
private _getCurrentTime: () => number;
private _interval: number;
constructor(getCurrentTime: () => number = () => Date.now(), interval: number = 60000) {
this._getCurrentTime = getCurrentTime;
this._interval = interval;
setInterval(() => {
while (this._mergedEvents.length > 0) {
this._mergedEvents.shift().resubmit();
}
}, interval);
}
public run(context: EventPluginContext, next?: () => void): void {
function getHashCode(error: IInnerError): number {
let hashCode = 0;
while (error) {
hashCode += (hashCode * 397) ^ Utils.getHashCode(error.message);
if (error.stack_trace && error.stack_trace.length) {
hashCode += (hashCode * 397) ^ Utils.getHashCode(JSON.stringify(error.stack_trace));
}
error = error.inner;
}
return hashCode;
}
let error = context.event.data['@error'];
let hashCode = getHashCode(error);
if (!hashCode) {
return;
}
let count = context.event.count || 1;
let now = this._getCurrentTime();
let merged = this._mergedEvents.filter(s => s.hashCode === hashCode)[0];
if (merged) {
merged.incrementCount(count);
merged.updateDate(context.event.date);
context.log.info("Ignoring duplicate event with hash: " + hashCode);
context.cancelled = true;
return;
}
if (this._processedHashcodes.some(h => h.hash === hashCode && h.timestamp >= (now - this._interval))) {
context.log.info("Adding event with hash: " + hashCode);
this._mergedEvents.push(new MergedEvent(hashCode, context, count));
context.cancelled = true;
return;
}
context.log.info("Enqueueing event with hash: " + hashCode + "to cache.");
this._processedHashcodes.push({ hash: hashCode, timestamp: now });
// Only keep the last 50 recent errors.
while (this._processedHashcodes.length > 50) {
this._processedHashcodes.shift();
}
next && next();
}
}
interface TimestampedHash {
hash: number;
timestamp: number;
}
class MergedEvent {
public hashCode: number;
private _count: number;
private _context: EventPluginContext;
constructor(hashCode: number, context: EventPluginContext, count: number) {
this.hashCode = hashCode;
this._context = context;
this._count = count;
}
public incrementCount(count: number) {
this._count += count;
}
public resubmit() {
this._context.event.count = this._count;
this._context.client.config.queue.enqueue(this._context.event);
}
public updateDate(date) {
if (date > this._context.event.date) {
this._context.event.date = date;
}
}
}