Skip to content

Commit 7c12490

Browse files
committed
Better hashcode calculation
JSON.stringify shortcut didn't work, resulting JSON stack was [{}, {}, {}] Fallback to running through inner errors, calculating hashcode per error, XORing them to a final hashcode
1 parent 0fc7f0c commit 7c12490

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/plugins/default/DuplicateCheckerPlugin.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { IInnerError } from '../../models/IInnerError';
2-
import { ILog } from '../../logging/ILog';
32
import { IEventPlugin } from '../IEventPlugin';
43
import { EventPluginContext } from '../EventPluginContext';
54
import { Utils } from '../../Utils';
@@ -18,27 +17,44 @@ export class DuplicateCheckerPlugin implements IEventPlugin {
1817
this._interval = interval;
1918

2019
setInterval(() => {
21-
while(this._mergedEvents.length > 0){
20+
while (this._mergedEvents.length > 0) {
2221
this._mergedEvents.shift().resubmit();
2322
}
2423
}, interval);
2524
}
2625

2726
public run(context: EventPluginContext, next?: () => void): void {
28-
let hashCode = Utils.getHashCode(JSON.stringify(context.event.data['@error'], ['stack_trace', 'inner']));
29-
let count = context.event.count || 1;
27+
function getHashCode(error: IInnerError): number {
28+
let hashCode = 0;
29+
30+
while (error) {
31+
if (error.stack_trace && error.stack_trace.length) {
32+
hashCode = (hashCode * 397) ^ Utils.getHashCode(JSON.stringify(error.stack_trace));
33+
}
34+
error = error.inner;
35+
}
36+
37+
return hashCode;
38+
}
39+
40+
let error = context.event.data['@error'];
41+
let hashCode = getHashCode(error);
42+
if (!hashCode) {
43+
return;
44+
}
3045

46+
let count = context.event.count || 1;
3147
let now = this._getCurrentTime();
3248

3349
let merged = this._mergedEvents.filter(s => s.hashCode === hashCode)[0];
34-
if(merged) {
50+
if (merged) {
3551
merged.incrementCount(count);
3652
merged.updateDate(context.event.date);
3753
context.cancelled = true;
3854
return;
3955
}
4056

41-
if(this._processedHashcodes.some(h => h.hash === hashCode && h.timestamp >= (now - this._interval))) {
57+
if (this._processedHashcodes.some(h => h.hash === hashCode && h.timestamp >= (now - this._interval))) {
4258
this._mergedEvents.push(new MergedEvent(hashCode, context, count));
4359
context.cancelled = true;
4460
return;
@@ -61,17 +77,17 @@ interface TimestampedHash {
6177
}
6278

6379
class MergedEvent {
80+
public hashCode: number;
6481
private _count: number;
6582
private _context: EventPluginContext;
66-
public hashCode: number;
6783

68-
constructor(hashCode: number, context: EventPluginContext, count: number){
84+
constructor(hashCode: number, context: EventPluginContext, count: number) {
6985
this.hashCode = hashCode;
7086
this._context = context;
7187
this._count = count;
7288
}
7389

74-
public incrementCount(count: number){
90+
public incrementCount(count: number) {
7591
this._count += count;
7692
}
7793

@@ -81,7 +97,7 @@ class MergedEvent {
8197
}
8298

8399
public updateDate(date) {
84-
if(date > this._context.event.date) {
100+
if (date > this._context.event.date) {
85101
this._context.event.date = date;
86102
}
87103
}

0 commit comments

Comments
 (0)