Skip to content
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
Prev Previous commit
Next Next commit
Added new EventExclusionPlugin
  • Loading branch information
niemyjski committed May 4, 2016
commit 9845f22af2d97b3531831d52b19c34dce55935fc
8 changes: 6 additions & 2 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ export class Utils {
}

if (startsWithWildcard) {
let lastIndexOf = input.lastIndexOf(pattern);
return lastIndexOf !== -1 && lastIndexOf === (input.length - pattern.length);
return Utils.startsWith(input, pattern);
}

if (endsWithWildcard) {
Expand All @@ -165,6 +164,11 @@ export class Utils {
return input === null || (typeof (input) === 'object' && Object.keys(input).length === 0);
}

public static startsWith(input: string, pattern: string): boolean {
let lastIndexOf = input.lastIndexOf(pattern);
return lastIndexOf !== -1 && lastIndexOf === (input.length - pattern.length);
}

/**
* Stringifys an object with optional exclusions and max depth.
* @param data The data object to add.
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/EventPluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RequestInfoPlugin } from './default/RequestInfoPlugin';
import { EnvironmentInfoPlugin } from './default/EnvironmentInfoPlugin';
import { SubmissionMethodPlugin } from './default/SubmissionMethodPlugin';
import { DuplicateCheckerPlugin } from './default/DuplicateCheckerPlugin';
import { EventExclusionPlugin } from './default/EventExclusionPlugin';

export class EventPluginManager {
public static run(context: EventPluginContext, callback: (context?: EventPluginContext) => void): void {
Expand Down Expand Up @@ -45,6 +46,7 @@ export class EventPluginManager {
config.addPlugin(new ConfigurationDefaultsPlugin());
config.addPlugin(new ErrorPlugin());
config.addPlugin(new DuplicateCheckerPlugin());
config.addPlugin(new EventExclusionPlugin());
config.addPlugin(new ModuleInfoPlugin());
config.addPlugin(new RequestInfoPlugin());
config.addPlugin(new EnvironmentInfoPlugin());
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/default/DuplicateCheckerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class DuplicateCheckerPlugin implements IEventPlugin {
private _processedHashcodes: TimestampedHash[] = [];
private _getCurrentTime: () => number;

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

Expand Down
86 changes: 86 additions & 0 deletions src/plugins/default/EventExclusionPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { IInnerError } from '../../models/IInnerError';
import { IEventPlugin } from '../IEventPlugin';
import { EventPluginContext } from '../EventPluginContext';
import { Utils } from '../../Utils';

export class EventExclusionPlugin implements IEventPlugin {
public priority: number = 45;
public name: string = 'EventExclusionPlugin';

public run(context: EventPluginContext, next?: () => void): void {
function getLogLevel(level: string): number {
switch (level.toLowerCase()) {
case 'trace':
return 0;
case 'debug':
return 1;
case 'info':
return 2;
case 'warn':
return 3;
case 'error':
return 4;
case 'fatal':
return 5;
case 'off':
return 6;
default:
return -1;
}
}

function getMinLogLevel(settings: Object, loggerName: string = '*'): number {
return getLogLevel(getTypeAndSourceSetting(settings, 'log', loggerName, 'Trace'));
}

function getTypeAndSourceSetting(settings: Object = {}, type: string, source: string, defaultValue: string|boolean = undefined): string|boolean {
if (!type) {
return defaultValue;
}

let sourcePrefix = `@@${type}:`;
if (settings[sourcePrefix + source]) {
return settings[sourcePrefix + source];
}

// check for wildcard match
for (let key in settings) {
if (Utils.startsWith(key.toLowerCase(), sourcePrefix.toLowerCase()) && Utils.isMatch(source, [key.substring(sourcePrefix.length)])) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem like this would handle the same wildcard patterns as the .NET client. In the .NET client the pattern can start with an * which will match based on suffix, end with a * which will match based on prefix, start and end with a * which will do a substring match or it can just be a * which will match everything.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any unit tests that make sure this is working?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Utils.IsMatch does the same thing and we have unit tests for it.

return settings[key];
}
}

return defaultValue;
}

let ev = context.event;
let settings = context.client.config.settings;

if (ev.type === 'log') {
let minLogLevel = getMinLogLevel(settings, ev.source);
let logLevel = getLogLevel(ev.data['@level'] || 'Trace');

if (logLevel >= 0 && (logLevel > 5 || logLevel < minLogLevel)) {
context.log.info('Cancelling log event due to minimum log level.');
context.cancelled = true;
}
} else if (ev.type === 'error') {
let error: IInnerError = ev.data['@error'];
while (!context.cancelled && error) {
if (getTypeAndSourceSetting(settings, ev.type, error.type, true) === true) {
context.log.info(`Cancelling error from excluded exception type: ${error.type}`);
context.cancelled = true;
}

error = error.inner;
}
}

if (!context.cancelled && getTypeAndSourceSetting(settings, ev.type, ev.source, true) === true) {
context.log.info(`Cancelling event from excluded type: ${ev.type} and source: ${ev.source}`);
context.cancelled = true;
}

next && next();
}
}