Skip to content

Feature/excludes #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Simplified the duplicate checker plugin
  • Loading branch information
niemyjski committed May 4, 2016
commit 63b05b9283b7b091d11bc04419ad1a935cf6699d
11 changes: 4 additions & 7 deletions src/plugins/default/DuplicateCheckerPlugin-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import { createFixture } from './EventPluginTestFixture';
import { expect } from 'chai';

describe('DuplicateCheckerPlugin', () => {

let target: DuplicateCheckerPlugin;
let now: number;
let now: number = 0;
let plugin: DuplicateCheckerPlugin;

beforeEach(() => {
target = new DuplicateCheckerPlugin();
(<any>target).getNow = () => now;
now = 0;
plugin = new DuplicateCheckerPlugin(() => now);
});

function run(exception: Error) {
Expand All @@ -28,7 +25,7 @@ describe('DuplicateCheckerPlugin', () => {

let errorPlugin = new ErrorPlugin();
errorPlugin.run(context);
target.run(context);
plugin.run(context);

return context;
}
Expand Down
71 changes: 27 additions & 44 deletions src/plugins/default/DuplicateCheckerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,50 @@ import { IEventPlugin } from '../IEventPlugin';
import { EventPluginContext } from '../EventPluginContext';
import { Utils } from '../../Utils';

const ERROR_KEY: string = '@error';
const WINDOW_MILLISECONDS = 2000;
const MAX_QUEUE_LENGTH = 10;

export class DuplicateCheckerPlugin implements IEventPlugin {
public priority: number = 40;
public name: string = 'DuplicateCheckerPlugin';

private recentlyProcessedErrors: TimestampedHash[] = [];

public run(context: EventPluginContext, next?: () => void): void {
if (context.event.type === 'error') {
let error = context.event.data[ERROR_KEY];
let isDuplicate = this.checkDuplicate(error, context.log);
if (isDuplicate) {
context.cancelled = true;
return;
}
}
private _processedHashcodes: TimestampedHash[] = [];
private _getCurrentTime: () => number;

next && next();
constructor(getCurrentTime:() => number = () => Date.now()) {
this._getCurrentTime = getCurrentTime;
}

private getNow() {
return Date.now();
}
public run(context: EventPluginContext, next?: () => void): void {
function isDuplicate(error: IInnerError, processedHashcodes, now, log: ILog): boolean {
while (error) {
let hashCode = Utils.getHashCode(error.stack_trace && JSON.stringify(error.stack_trace));

private checkDuplicate(error: IInnerError, log: ILog): boolean {
function getHashCodeForError(err: IInnerError): number {
if (!err.stack_trace) {
return null;
}
// Only process the unique errors times within a 2 second window.
if (hashCode && processedHashcodes.some(h => h.hash === hashCode && h.timestamp >= (now - 2000))) {
log.info(`Ignoring duplicate error event hash: ${hashCode}`);
return true;
}

return Utils.getHashCode(JSON.stringify(err.stack_trace));
}
// Add this exception to our list of recent processed errors.
processedHashcodes.push({ hash: hashCode, timestamp: now });

let now = this.getNow();
let repeatWindow = now - WINDOW_MILLISECONDS;
let hashCode: number;
while (error) {
hashCode = getHashCodeForError(error);
// Only keep the last 20 recent errors.
while (processedHashcodes.length > 20) {
processedHashcodes.shift();
}

// make sure that we don't process the same error multiple times within the repeat window
if (hashCode && this.recentlyProcessedErrors.some(h =>
h.hash === hashCode && h.timestamp >= repeatWindow)) {
log.info(`Ignoring duplicate error event: hash=${hashCode}`);
return true;
error = error.inner;
}

// add this exception to our list of recent errors that we have processed
this.recentlyProcessedErrors.push({ hash: hashCode, timestamp: now });
return false;
}

// only keep the last 10 recent errors
while (this.recentlyProcessedErrors.length > MAX_QUEUE_LENGTH) {
this.recentlyProcessedErrors.shift();
if (context.event.type === 'error') {
if (isDuplicate(context.event.data['@error'], this._processedHashcodes, this._getCurrentTime(), context.log)) {
context.cancelled = true;
return;
}

error = error.inner;
}

return false;
next && next();
}
}

Expand Down