Skip to content

Commit d678bc2

Browse files
committed
Added support for cancelling any event where the user agent matches a known bot pattern :)
1 parent 44d5e8b commit d678bc2

12 files changed

+111
-24
lines changed

dist/exceptionless.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,11 @@ export declare class Configuration implements IConfigurationSettings {
198198
private _serverUrl;
199199
serverUrl: string;
200200
private _dataExclusions;
201+
private _userAgentBotPatterns;
201202
dataExclusions: string[];
202203
addDataExclusions(...exclusions: string[]): void;
204+
userAgentBotPatterns: string[];
205+
addUserAgentBotPatterns(...userAgentBotPatterns: string[]): void;
203206
plugins: IEventPlugin[];
204207
addPlugin(plugin: IEventPlugin): void;
205208
addPlugin(name: string, priority: number, pluginAction: (context: EventPluginContext, next?: () => void) => void): void;

dist/exceptionless.js

Lines changed: 28 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.node.js

Lines changed: 30 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.node.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/submitSync.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/submitSync.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
"url": "git://github.com/exceptionless/Exceptionless.JavaScript.git"
2727
},
2828
"devDependencies": {
29-
"chai": "3.4.1",
29+
"chai": "3.5.0",
3030
"del": "2.2.0",
31-
"es5-shim": "4.5.0",
31+
"es5-shim": "4.5.2",
3232
"es6-shim": "0.34.2",
3333
"gulp": "3.9.0",
3434
"gulp-concat": "2.6.0",
@@ -41,9 +41,9 @@
4141
"gulp-wrap-umd": "0.2.1",
4242
"rimraf": "2.5.1",
4343
"source-map-support": "0.4.0",
44-
"tracekit": "0.3.1",
44+
"tracekit": "0.3.2",
4545
"tslint": "3.3.0",
46-
"tsproject": "1.2.0-rc.4",
46+
"tsproject": "1.2.0-rc.8",
4747
"typescript": "1.7.5",
4848
"typescript-formatter": "1.2.0"
4949
},

src/configuration/Configuration.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ export class Configuration implements IConfigurationSettings {
178178
*/
179179
private _dataExclusions: string[] = [];
180180

181+
/**
182+
* A list of user agent patterns.
183+
* @type {Array}
184+
* @private
185+
*/
186+
private _userAgentBotPatterns: string[] = [];
187+
181188
/**
182189
* A list of exclusion patterns that will automatically remove any data that
183190
* matches them from any data submitted to the server.
@@ -205,6 +212,29 @@ export class Configuration implements IConfigurationSettings {
205212
this._dataExclusions = Utils.addRange<string>(this._dataExclusions, ...exclusions);
206213
}
207214

215+
/**
216+
* A list of user agent patterns that will cause any event with a matching user agent to not be submitted.
217+
*
218+
* For example, entering *Bot* will cause any events that contains a user agent of Bot will not be submitted.
219+
*
220+
* @returns {string[]}
221+
*/
222+
public get userAgentBotPatterns(): string[] {
223+
let patterns: string = this.settings['@@UserAgentBotPatterns'];
224+
return this._userAgentBotPatterns.concat(patterns && patterns.split(',') || []);
225+
}
226+
227+
/**
228+
* Add items to the list of user agent patterns that will cause any event with a matching user agent to not be submitted.
229+
*
230+
* For example, entering *Bot* will cause any events that contains a user agent of Bot will not be submitted.
231+
*
232+
* @param userAgentBotPatterns
233+
*/
234+
public addUserAgentBotPatterns(...userAgentBotPatterns: string[]) {
235+
this._userAgentBotPatterns = Utils.addRange<string>(this._userAgentBotPatterns, ...userAgentBotPatterns);
236+
}
237+
208238
/**
209239
* The list of plugins that will be used in this configuration.
210240
* @returns {IEventPlugin[]}

src/plugins/default/RequestInfoPlugin.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { IEventPlugin } from '../IEventPlugin';
22
import { EventPluginContext } from '../EventPluginContext';
33
import { IRequestInfo } from '../../models/IRequestInfo';
4+
import { Utils } from '../../Utils';
45

56
export class RequestInfoPlugin implements IEventPlugin {
67
public priority: number = 70;
@@ -9,11 +10,17 @@ export class RequestInfoPlugin implements IEventPlugin {
910
public run(context: EventPluginContext, next?: () => void): void {
1011
const REQUEST_KEY: string = '@request'; // optimization for minifier.
1112

12-
let collector = context.client.config.requestInfoCollector;
13+
let config = context.client.config;
14+
let collector = config.requestInfoCollector;
1315
if (!context.event.data[REQUEST_KEY] && !!collector) {
1416
let requestInfo: IRequestInfo = collector.getRequestInfo(context);
1517
if (!!requestInfo) {
16-
context.event.data[REQUEST_KEY] = requestInfo;
18+
if (Utils.isMatch(requestInfo.user_agent, config.userAgentBotPatterns)) {
19+
context.log.info('Cancelling event as the request user agent matches a known bot pattern');
20+
context.cancelled = true;
21+
} else {
22+
context.event.data[REQUEST_KEY] = requestInfo;
23+
}
1724
}
1825
}
1926

0 commit comments

Comments
 (0)