Skip to content

Commit 9845f22

Browse files
committed
Added new EventExclusionPlugin
1 parent 63b05b9 commit 9845f22

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

src/Utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ export class Utils {
149149
}
150150

151151
if (startsWithWildcard) {
152-
let lastIndexOf = input.lastIndexOf(pattern);
153-
return lastIndexOf !== -1 && lastIndexOf === (input.length - pattern.length);
152+
return Utils.startsWith(input, pattern);
154153
}
155154

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

167+
public static startsWith(input: string, pattern: string): boolean {
168+
let lastIndexOf = input.lastIndexOf(pattern);
169+
return lastIndexOf !== -1 && lastIndexOf === (input.length - pattern.length);
170+
}
171+
168172
/**
169173
* Stringifys an object with optional exclusions and max depth.
170174
* @param data The data object to add.

src/plugins/EventPluginManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { RequestInfoPlugin } from './default/RequestInfoPlugin';
88
import { EnvironmentInfoPlugin } from './default/EnvironmentInfoPlugin';
99
import { SubmissionMethodPlugin } from './default/SubmissionMethodPlugin';
1010
import { DuplicateCheckerPlugin } from './default/DuplicateCheckerPlugin';
11+
import { EventExclusionPlugin } from './default/EventExclusionPlugin';
1112

1213
export class EventPluginManager {
1314
public static run(context: EventPluginContext, callback: (context?: EventPluginContext) => void): void {
@@ -45,6 +46,7 @@ export class EventPluginManager {
4546
config.addPlugin(new ConfigurationDefaultsPlugin());
4647
config.addPlugin(new ErrorPlugin());
4748
config.addPlugin(new DuplicateCheckerPlugin());
49+
config.addPlugin(new EventExclusionPlugin());
4850
config.addPlugin(new ModuleInfoPlugin());
4951
config.addPlugin(new RequestInfoPlugin());
5052
config.addPlugin(new EnvironmentInfoPlugin());

src/plugins/default/DuplicateCheckerPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class DuplicateCheckerPlugin implements IEventPlugin {
1111
private _processedHashcodes: TimestampedHash[] = [];
1212
private _getCurrentTime: () => number;
1313

14-
constructor(getCurrentTime:() => number = () => Date.now()) {
14+
constructor(getCurrentTime: () => number = () => Date.now()) {
1515
this._getCurrentTime = getCurrentTime;
1616
}
1717

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { IInnerError } from '../../models/IInnerError';
2+
import { IEventPlugin } from '../IEventPlugin';
3+
import { EventPluginContext } from '../EventPluginContext';
4+
import { Utils } from '../../Utils';
5+
6+
export class EventExclusionPlugin implements IEventPlugin {
7+
public priority: number = 45;
8+
public name: string = 'EventExclusionPlugin';
9+
10+
public run(context: EventPluginContext, next?: () => void): void {
11+
function getLogLevel(level: string): number {
12+
switch (level.toLowerCase()) {
13+
case 'trace':
14+
return 0;
15+
case 'debug':
16+
return 1;
17+
case 'info':
18+
return 2;
19+
case 'warn':
20+
return 3;
21+
case 'error':
22+
return 4;
23+
case 'fatal':
24+
return 5;
25+
case 'off':
26+
return 6;
27+
default:
28+
return -1;
29+
}
30+
}
31+
32+
function getMinLogLevel(settings: Object, loggerName: string = '*'): number {
33+
return getLogLevel(getTypeAndSourceSetting(settings, 'log', loggerName, 'Trace'));
34+
}
35+
36+
function getTypeAndSourceSetting(settings: Object = {}, type: string, source: string, defaultValue: string|boolean = undefined): string|boolean {
37+
if (!type) {
38+
return defaultValue;
39+
}
40+
41+
let sourcePrefix = `@@${type}:`;
42+
if (settings[sourcePrefix + source]) {
43+
return settings[sourcePrefix + source];
44+
}
45+
46+
// check for wildcard match
47+
for (let key in settings) {
48+
if (Utils.startsWith(key.toLowerCase(), sourcePrefix.toLowerCase()) && Utils.isMatch(source, [key.substring(sourcePrefix.length)])) {
49+
return settings[key];
50+
}
51+
}
52+
53+
return defaultValue;
54+
}
55+
56+
let ev = context.event;
57+
let settings = context.client.config.settings;
58+
59+
if (ev.type === 'log') {
60+
let minLogLevel = getMinLogLevel(settings, ev.source);
61+
let logLevel = getLogLevel(ev.data['@level'] || 'Trace');
62+
63+
if (logLevel >= 0 && (logLevel > 5 || logLevel < minLogLevel)) {
64+
context.log.info('Cancelling log event due to minimum log level.');
65+
context.cancelled = true;
66+
}
67+
} else if (ev.type === 'error') {
68+
let error: IInnerError = ev.data['@error'];
69+
while (!context.cancelled && error) {
70+
if (getTypeAndSourceSetting(settings, ev.type, error.type, true) === true) {
71+
context.log.info(`Cancelling error from excluded exception type: ${error.type}`);
72+
context.cancelled = true;
73+
}
74+
75+
error = error.inner;
76+
}
77+
}
78+
79+
if (!context.cancelled && getTypeAndSourceSetting(settings, ev.type, ev.source, true) === true) {
80+
context.log.info(`Cancelling event from excluded type: ${ev.type} and source: ${ev.source}`);
81+
context.cancelled = true;
82+
}
83+
84+
next && next();
85+
}
86+
}

0 commit comments

Comments
 (0)