forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.ts
More file actions
702 lines (627 loc) · 20.3 KB
/
Configuration.ts
File metadata and controls
702 lines (627 loc) · 20.3 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
import { DefaultLastReferenceIdManager } from "../lastReferenceIdManager/DefaultLastReferenceIdManager.js";
import { ILastReferenceIdManager } from "../lastReferenceIdManager/ILastReferenceIdManager.js";
import { ILog } from "../logging/ILog.js";
import { ConsoleLog } from "../logging/ConsoleLog.js";
import { NullLog } from "../logging/NullLog.js";
import { UserInfo } from "../models/data/UserInfo.js";
import { HeartbeatPlugin } from "../plugins/default/HeartbeatPlugin.js";
import { ReferenceIdPlugin } from "../plugins/default/ReferenceIdPlugin.js";
import { EventPluginContext } from "../plugins/EventPluginContext.js";
import { IEventPlugin } from "../plugins/IEventPlugin.js";
import { DefaultEventQueue } from "../queue/DefaultEventQueue.js";
import { IEventQueue } from "../queue/IEventQueue.js";
import { IEnvironmentInfoCollector } from "../services/IEnvironmentInfoCollector.js";
import { IErrorParser } from "../services/IErrorParser.js";
import { IModuleCollector } from "../services/IModuleCollector.js";
import { IRequestInfoCollector } from "../services/IRequestInfoCollector.js";
import { InMemoryStorageProvider } from "../storage/InMemoryStorageProvider.js";
import { IStorageProvider } from "../storage/IStorageProvider.js";
import { ISubmissionClient } from "../submission/ISubmissionClient.js";
import { IConfigurationSettings } from "./IConfigurationSettings.js";
import { guid, merge } from "../Utils.js";
import { KnownEventDataKeys } from "../models/Event.js";
export class Configuration {
/**
* The default configuration settings that are applied to new configuration instances.
* @type {IConfigurationSettings}
* @private
*/
private static _defaultSettings: IConfigurationSettings = null;
/**
* A default list of tags that will automatically be added to every
* report submitted to the server.
*
* @type {Array}
*/
public defaultTags: string[] = [];
/**
* A default list of of extended data objects that will automatically
* be added to every report submitted to the server.
*
* @type {{}}
*/
public defaultData: Record<string, unknown> = {};
/**
* Whether the client is currently enabled or not. If it is disabled,
* submitted errors will be discarded and no data will be sent to the server.
*
* @returns {boolean}
*/
public enabled: boolean = true;
public environmentInfoCollector: IEnvironmentInfoCollector;
public errorParser: IErrorParser;
public lastReferenceIdManager: ILastReferenceIdManager = new DefaultLastReferenceIdManager();
public log: ILog = new NullLog();
public moduleCollector: IModuleCollector;
public requestInfoCollector: IRequestInfoCollector;
/**
* Maximum number of events that should be sent to the server together in a batch. (Defaults to 50)
*/
public submissionBatchSize: number = 50;
public submissionClient: ISubmissionClient;
/**
* Contains a dictionary of custom settings that can be used to control
* the client and will be automatically updated from the server.
*/
public settings: Record<string, string> = {};
public storage: IStorageProvider = new InMemoryStorageProvider();
public queue: IEventQueue = new DefaultEventQueue(this);
/**
* The API key that will be used when sending events to the server.
* @type {string}
* @private
*/
private _apiKey: string;
/**
* The server url that all events will be sent to.
* @type {string}
* @private
*/
private _serverUrl: string = "https://collector.exceptionless.io";
/**
* The config server url that all configuration will be retrieved from.
* @type {string}
* @private
*/
private _configServerUrl: string = "https://config.exceptionless.io";
/**
* The heartbeat server url that all heartbeats will be sent to.
* @type {string}
* @private
*/
private _heartbeatServerUrl: string = "https://heartbeat.exceptionless.io";
/**
* How often the client should check for updated server settings when idle. The default is every 2 minutes.
* @type {number}
* @private
*/
private _updateSettingsWhenIdleInterval: number = 120000;
/**
* A list of exclusion patterns.
* @type {Array}
* @private
*/
private _dataExclusions: string[] = [];
private _includePrivateInformation: boolean;
private _includeUserName: boolean;
private _includeMachineName: boolean;
private _includeIpAddress: boolean;
private _includeCookies: boolean;
private _includePostData: boolean;
private _includeQueryString: boolean;
/**
* A list of user agent patterns.
* @type {Array}
* @private
*/
private _userAgentBotPatterns: string[] = [];
/**
* The list of plugins that will be used in this configuration.
* @type {Array}
* @private
*/
private _plugins: IEventPlugin[] = [];
/**
* A list of handlers that will be fired when configuration changes.
* @type {Array}
* @private
*/
private _handlers: Array<(config: Configuration) => void> = [];
public apply(configSettings?: IConfigurationSettings): void {
function inject<T>(functionOrValue: T | ((config: Configuration) => T)) {
return functionOrValue instanceof Function ? functionOrValue(this) : functionOrValue;
}
// TODO: Handle this being called multiple times.
configSettings = merge(Configuration.defaults, configSettings);
if (configSettings.apiKey) {
this.apiKey = configSettings.apiKey;
}
if (configSettings.serverUrl) {
this.serverUrl = configSettings.serverUrl;
}
if (configSettings.configServerUrl) {
this.configServerUrl = configSettings.configServerUrl;
}
if (configSettings.heartbeatServerUrl) {
this.heartbeatServerUrl = configSettings.heartbeatServerUrl;
}
if (configSettings.updateSettingsWhenIdleInterval !== undefined) {
this.updateSettingsWhenIdleInterval = configSettings.updateSettingsWhenIdleInterval;
}
if (configSettings.includePrivateInformation !== undefined) {
this.includePrivateInformation = configSettings.includePrivateInformation;
}
if (configSettings.submissionBatchSize !== undefined) {
this.submissionBatchSize = configSettings.submissionBatchSize;
}
if (configSettings.log) {
this.log = inject(configSettings.log);
}
if (configSettings.environmentInfoCollector) {
this.environmentInfoCollector = inject(configSettings.environmentInfoCollector);
}
if (configSettings.errorParser) {
this.errorParser = inject(configSettings.errorParser);
}
if (configSettings.lastReferenceIdManager) {
this.lastReferenceIdManager = inject(configSettings.lastReferenceIdManager);
}
if (configSettings.moduleCollector) {
this.moduleCollector = inject(configSettings.moduleCollector);
}
if (configSettings.requestInfoCollector) {
this.requestInfoCollector = inject(configSettings.requestInfoCollector);
}
if (configSettings.submissionClient) {
this.submissionClient = inject(configSettings.submissionClient);
}
if (configSettings.storage) {
this.storage = inject(configSettings.storage);
}
if (configSettings.queue) {
this.queue = inject(configSettings.queue);
}
}
/**
* The API key that will be used when sending events to the server.
* @returns {string}
*/
public get apiKey(): string {
return this._apiKey;
}
/**
* The API key that will be used when sending events to the server.
* @param value
*/
public set apiKey(value: string) {
this._apiKey = value || null;
this.log.info(`apiKey: ${this._apiKey}`);
this.changed();
}
/**
* Returns true if the apiKey is valid.
* @returns {boolean}
*/
public get isValid(): boolean {
return this.apiKey && this.apiKey.length >= 10;
}
/**
* The server url that all events will be sent to.
* @returns {string}
*/
public get serverUrl(): string {
return this._serverUrl;
}
/**
* The server url that all events will be sent to.
* @param value
*/
public set serverUrl(value: string) {
if (value) {
this._serverUrl = value;
this._configServerUrl = value;
this._heartbeatServerUrl = value;
this.log.info(`serverUrl: ${value}`);
this.changed();
}
}
/**
* The config server url that all configuration will be retrieved from.
* @returns {string}
*/
public get configServerUrl(): string {
return this._configServerUrl;
}
/**
* The config server url that all configuration will be retrieved from.
* @param value
*/
public set configServerUrl(value: string) {
if (value) {
this._configServerUrl = value;
this.log.info(`configServerUrl: ${value}`);
this.changed();
}
}
/**
* The heartbeat server url that all heartbeats will be sent to.
* @returns {string}
*/
public get heartbeatServerUrl(): string {
return this._heartbeatServerUrl;
}
/**
* The heartbeat server url that all heartbeats will be sent to.
* @param value
*/
public set heartbeatServerUrl(value: string) {
if (value) {
this._heartbeatServerUrl = value;
this.log.info(`heartbeatServerUrl: ${value}`);
this.changed();
}
}
/**
* How often the client should check for updated server settings when idle. The default is every 2 minutes.
* @returns {number}
*/
public get updateSettingsWhenIdleInterval(): number {
return this._updateSettingsWhenIdleInterval;
}
/**
* How often the client should check for updated server settings when idle. The default is every 2 minutes.
* @param value
*/
public set updateSettingsWhenIdleInterval(value: number) {
if (typeof value !== "number") {
return;
}
if (value <= 0) {
value = -1;
} else if (value > 0 && value < 120000) {
value = 120000;
}
this._updateSettingsWhenIdleInterval = value;
this.log.info(`updateSettingsWhenIdleInterval: ${value}`);
this.changed();
}
/**
* A list of exclusion patterns that will automatically remove any data that
* matches them from any data submitted to the server.
*
* For example, entering CreditCard will remove any extended data properties,
* form fields, cookies and query parameters from the report.
*
* @returns {string[]}
*/
public get dataExclusions(): string[] {
// TODO: Known settings keys.
const exclusions: string = this.settings["@@DataExclusions"];
return this._dataExclusions.concat(
exclusions && exclusions.split(",") || [],
);
}
/**
* Add items to the list of exclusion patterns that will automatically remove any
* data that matches them from any data submitted to the server.
*
* For example, entering CreditCard will remove any extended data properties, form
* fields, cookies and query parameters from the report.
*
* @param exclusions
*/
public addDataExclusions(...exclusions: string[]) {
this._dataExclusions = [...this._dataExclusions, ...exclusions];
}
/**
* Gets a value indicating whether to include private information about the local machine.
* @returns {boolean}
*/
public get includePrivateInformation(): boolean {
return this._includePrivateInformation;
}
/**
* Sets a value indicating whether to include private information about the local machine
* @param value
*/
public set includePrivateInformation(value: boolean) {
const val = value || false;
this._includePrivateInformation = val;
this._includeUserName = val;
this._includeMachineName = val;
this._includeIpAddress = val;
this._includeCookies = val;
this._includePostData = val;
this._includeQueryString = val;
this.log.info(`includePrivateInformation: ${val}`);
this.changed();
}
/**
* Gets a value indicating whether to include User Name.
* @returns {boolean}
*/
public get includeUserName(): boolean {
return this._includeUserName;
}
/**
* Sets a value indicating whether to include User Name.
* @param value
*/
public set includeUserName(value: boolean) {
this._includeUserName = value || false;
this.changed();
}
/**
* Gets a value indicating whether to include MachineName in MachineInfo.
* @returns {boolean}
*/
public get includeMachineName(): boolean {
return this._includeMachineName;
}
/**
* Sets a value indicating whether to include MachineName in MachineInfo.
* @param value
*/
public set includeMachineName(value: boolean) {
this._includeMachineName = value || false;
this.changed();
}
/**
* Gets a value indicating whether to include Ip Addresses in MachineInfo and RequestInfo.
* @returns {boolean}
*/
public get includeIpAddress(): boolean {
return this._includeIpAddress;
}
/**
* Sets a value indicating whether to include Ip Addresses in MachineInfo and RequestInfo.
* @param value
*/
public set includeIpAddress(value: boolean) {
this._includeIpAddress = value || false;
this.changed();
}
/**
* Gets a value indicating whether to include Cookies.
* NOTE: DataExclusions are applied to all Cookie keys when enabled.
* @returns {boolean}
*/
public get includeCookies(): boolean {
return this._includeCookies;
}
/**
* Sets a value indicating whether to include Cookies.
* NOTE: DataExclusions are applied to all Cookie keys when enabled.
* @param value
*/
public set includeCookies(value: boolean) {
this._includeCookies = value || false;
this.changed();
}
/**
* Gets a value indicating whether to include Form/POST Data.
* NOTE: DataExclusions are only applied to Form data keys when enabled.
* @returns {boolean}
*/
public get includePostData(): boolean {
return this._includePostData;
}
/**
* Sets a value indicating whether to include Form/POST Data.
* NOTE: DataExclusions are only applied to Form data keys when enabled.
* @param value
*/
public set includePostData(value: boolean) {
this._includePostData = value || false;
this.changed();
}
/**
* Gets a value indicating whether to include query string information.
* NOTE: DataExclusions are applied to all Query String keys when enabled.
* @returns {boolean}
*/
public get includeQueryString(): boolean {
return this._includeQueryString;
}
/**
* Sets a value indicating whether to include query string information.
* NOTE: DataExclusions are applied to all Query String keys when enabled.
* @param value
*/
public set includeQueryString(value: boolean) {
this._includeQueryString = value || false;
this.changed();
}
/**
* A list of user agent patterns that will cause any event with a matching user agent to not be submitted.
*
* For example, entering *Bot* will cause any events that contains a user agent of Bot will not be submitted.
*
* @returns {string[]}
*/
public get userAgentBotPatterns(): string[] {
// TODO: Known settings keys.
const patterns: string = this.settings["@@UserAgentBotPatterns"];
return this._userAgentBotPatterns.concat(
patterns && patterns.split(",") || [],
);
}
/**
* Add items to the list of user agent patterns that will cause any event with a matching user agent to not be submitted.
*
* For example, entering *Bot* will cause any events that contains a user agent of Bot will not be submitted.
*
* @param userAgentBotPatterns
*/
public addUserAgentBotPatterns(...userAgentBotPatterns: string[]) {
this._userAgentBotPatterns = [...this._userAgentBotPatterns, ...userAgentBotPatterns];
}
/**
* The list of plugins that will be used in this configuration.
* @returns {IEventPlugin[]}
*/
public get plugins(): IEventPlugin[] {
return this._plugins.sort((p1: IEventPlugin, p2: IEventPlugin) => {
return (p1.priority < p2.priority)
? -1
: (p1.priority > p2.priority)
? 1
: 0;
});
}
/**
* Register an plugin to be used in this configuration.
* @param plugin
*/
public addPlugin(plugin: IEventPlugin): void;
/**
* Register an plugin to be used in this configuration.
* @param name The name used to identify the plugin.
* @param priority Used to determine plugins priority.
* @param pluginAction A function that is run.
*/
public addPlugin(
name: string,
priority: number,
pluginAction: (context: EventPluginContext) => Promise<void>,
): void;
public addPlugin(
pluginOrName: IEventPlugin | string,
priority?: number,
pluginAction?: (context: EventPluginContext) => Promise<void>,
): void {
const plugin: IEventPlugin = pluginAction
? { name: pluginOrName as string, priority, run: pluginAction }
: pluginOrName as IEventPlugin;
if (!plugin || !plugin.run) {
this.log.error("Add plugin failed: Run method not defined");
return;
}
if (!plugin.name) {
plugin.name = guid();
}
if (!plugin.priority) {
plugin.priority = 0;
}
let pluginExists: boolean = false;
const plugins = this._plugins; // optimization for minifier.
for (const p of plugins) {
if (p.name === plugin.name) {
pluginExists = true;
break;
}
}
if (!pluginExists) {
plugins.push(plugin);
}
}
/**
* Remove the plugin from this configuration.
* @param plugin
*/
public removePlugin(plugin: IEventPlugin): void;
/**
* Remove an plugin by key from this configuration.
* @param name
*/
public removePlugin(pluginOrName: IEventPlugin | string): void {
const name: string = typeof pluginOrName === "string"
? pluginOrName
: pluginOrName.name;
if (!name) {
this.log.error("Remove plugin failed: Plugin name not defined");
return;
}
const plugins = this._plugins; // optimization for minifier.
for (let index = 0; index < plugins.length; index++) {
if (plugins[index].name === name) {
plugins.splice(index, 1);
break;
}
}
}
/**
* Automatically set the application version for events.
* @param version
*/
public setVersion(version: string): void {
if (version) {
this.defaultData[KnownEventDataKeys.Version] = version;
}
}
public setUserIdentity(userInfo: UserInfo): void;
public setUserIdentity(identity: string): void;
public setUserIdentity(identity: string, name: string): void;
public setUserIdentity(
userInfoOrIdentity: UserInfo | string,
name?: string,
): void {
const userInfo: UserInfo = typeof userInfoOrIdentity !== "string"
? userInfoOrIdentity
: { identity: userInfoOrIdentity, name };
const shouldRemove: boolean = !userInfo ||
(!userInfo.identity && !userInfo.name);
if (shouldRemove) {
delete this.defaultData[KnownEventDataKeys.UserInfo];
} else {
this.defaultData[KnownEventDataKeys.UserInfo] = userInfo;
}
this.log.info(
`user identity: ${shouldRemove ? "null" : userInfo.identity}`,
);
}
/**
* Used to identify the client that sent the events to the server.
* @returns {string}
*/
public get userAgent(): string {
return "exceptionless-js/1.0.0.0";
}
/**
* Automatically send a heartbeat to keep the session alive.
*/
public useSessions(
sendHeartbeats: boolean = true,
heartbeatInterval: number = 30000,
): void {
if (sendHeartbeats) {
this.addPlugin(new HeartbeatPlugin(heartbeatInterval));
}
}
/**
* Automatically set a reference id for error events.
*/
public useReferenceIds(): void {
this.addPlugin(new ReferenceIdPlugin());
}
public useLocalStorage(): void {
// This method will be injected via the prototype.
}
// TODO: Support a min log level.
public useDebugLogger(): void {
this.log = new ConsoleLog();
}
public onChanged(handler: (config: Configuration) => void): void {
handler && this._handlers.push(handler);
}
private changed() {
const handlers = this._handlers; // optimization for minifier.
for (const handler of handlers) {
try {
handler(this);
} catch (ex) {
this.log.error(`Error calling onChanged handler: ${ex}`);
}
}
}
/**
* The default configuration settings that are applied to new configuration instances.
* @returns {IConfigurationSettings}
*/
public static get defaults(): IConfigurationSettings {
if (Configuration._defaultSettings === null) {
Configuration._defaultSettings = { includePrivateInformation: true };
}
return Configuration._defaultSettings;
}
}