forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateCheckerPlugin.ts
More file actions
130 lines (110 loc) · 3.76 KB
/
DuplicateCheckerPlugin.ts
File metadata and controls
130 lines (110 loc) · 3.76 KB
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
import { InnerErrorInfo } from "../../models/data/ErrorInfo.js";
import { KnownEventDataKeys } from "../../models/Event.js";
import { getHashCode } from "../../Utils.js";
import { EventPluginContext } from "../EventPluginContext.js";
import { IEventPlugin } from "../IEventPlugin.js";
export class DuplicateCheckerPlugin implements IEventPlugin {
public priority = 1010;
public name = "DuplicateCheckerPlugin";
private _mergedEvents: MergedEvent[] = [];
private _processedHashCodes: TimestampedHash[] = [];
private _getCurrentTime: () => number;
private _intervalId = 0;
private _interval: number;
constructor(
getCurrentTime: () => number = () => Date.now(),
interval: number = 30000
) {
this._getCurrentTime = getCurrentTime;
this._interval = interval;
}
public startup(): Promise<void> {
clearInterval(this._intervalId);
this._intervalId = setInterval(() => void this.submitEvents(), this._interval);
return Promise.resolve();
}
public async suspend(): Promise<void> {
clearInterval(this._intervalId);
this._intervalId = 0;
await this.submitEvents();
}
public run(context: EventPluginContext): Promise<void> {
function calculateHashCode(error: InnerErrorInfo | undefined): number {
let hash = 0;
while (error) {
if (error.message && error.message.length) {
hash += (hash * 397) ^ getHashCode(error.message);
}
if (error.stack_trace && error.stack_trace.length) {
hash += (hash * 397) ^ getHashCode(JSON.stringify(error.stack_trace));
}
error = error.inner;
}
return hash;
}
const error = context.event.data?.[KnownEventDataKeys.Error];
const hashCode = calculateHashCode(error);
if (hashCode) {
const count = context.event.count || 1;
const now = this._getCurrentTime();
const 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;
}
if (
!context.cancelled &&
this._processedHashCodes.some((h) =>
h.hash === hashCode && h.timestamp >= (now - this._interval)
)
) {
context.log.trace("Adding event with hash: " + hashCode);
this._mergedEvents.push(new MergedEvent(hashCode, context, count));
context.cancelled = true;
}
if (!context.cancelled) {
context.log.trace(`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();
}
}
}
return Promise.resolve();
}
private async submitEvents(): Promise<void> {
while (this._mergedEvents.length > 0) {
await this._mergedEvents.shift()?.resubmit();
}
}
}
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): void {
this._count += count;
}
public async resubmit(): Promise<void> {
this._context.event.count = this._count;
await this._context.client.config.services.queue.enqueue(this._context.event);
}
public updateDate(date?: Date): void {
const ev = this._context.event;
if (date && ev.date && date > ev.date) {
ev.date = date;
}
}
}