Skip to content

Commit 91569d5

Browse files
committed
Added Last Reference Id support.
1 parent 78cb9f6 commit 91569d5

File tree

2 files changed

+98
-8
lines changed

2 files changed

+98
-8
lines changed

src/exceptionless-spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33

44
//import { Configuration, ExceptionlessClient } from 'Exceptionless';
55
module Exceptionless {
6+
describe('ExceptionlessClient', () => {
7+
it('should use event reference ids', () => {
8+
var error = new Error('From Unit Test');
9+
10+
var client = new ExceptionlessClient('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
11+
expect(client.config.lastReferenceIdManager.getLast()).toBe(null);
12+
client.submitException(error);
13+
expect(client.config.lastReferenceIdManager.getLast()).toBe(null);
14+
15+
var numberOfPlugins = client.config.plugins.length;
16+
client.config.useReferenceIds();
17+
expect(client.config.plugins.length).toBe(numberOfPlugins + 1);
18+
19+
client.submitException(error);
20+
expect(client.config.lastReferenceIdManager.getLast()).not.toBe(null);
21+
});
22+
});
23+
624
describe('Configuration', () => {
725
it('should set the api key to null and enabled to false', () => {
826
var config = new Configuration(null);

src/exceptionless.ts

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,43 @@ module Exceptionless {
9393
}
9494

9595
public submitEvent(event:IEvent, pluginContextData?:ContextData) {
96+
if (!event) {
97+
return;
98+
}
99+
96100
if (!this.config.enabled) {
97101
this.config.log.info('Event submission is currently disabled');
98102
return;
99103
}
100104

105+
var context = new EventPluginContext(this, event, pluginContextData);
106+
EventPluginManager.run(context);
107+
if (context.cancel) {
108+
return;
109+
}
110+
111+
// ensure all required data
112+
if (!event.type || event.type.length === 0) {
113+
event.type = 'log';
114+
}
115+
116+
if (!event.date) {
117+
event.date = new Date();
118+
}
119+
120+
this.config.log.info('Submitting event: type=' + event.type + !!event.reference_id ? ' refid=' + event.reference_id : '');
101121
this.config.queue.enqueue(event);
122+
123+
if (!event.reference_id || event.reference_id.length === 0) {
124+
return;
125+
}
126+
127+
this.config.log.info('Setting last reference id "' + event.reference_id + '"');
128+
this.config.lastReferenceIdManager.setLast(event.reference_id);
129+
}
130+
131+
public getLastReferenceId(): string {
132+
return this.config.lastReferenceIdManager.getLast();
102133
}
103134
}
104135

@@ -108,6 +139,7 @@ module Exceptionless {
108139
private _serverUrl:string = 'https://collector.exceptionless.io';
109140
private _plugins:IEventPlugin[] = [];
110141

142+
public lastReferenceIdManager:ILastReferenceIdManager = new InMemoryLastReferenceIdManager();
111143
public log:ILog = new NullLog();
112144
public submissionBatchSize = 50;
113145
public submissionClient:ISubmissionClient = new SubmissionClient();
@@ -154,7 +186,7 @@ module Exceptionless {
154186
public addPlugin(plugin:IEventPlugin): void;
155187
public addPlugin(name:string, priority:number, pluginAction:(context:EventPluginContext) => void): void;
156188
public addPlugin(pluginOrName:IEventPlugin|string, priority?:number, pluginAction?:(context:EventPluginContext) => void): void {
157-
var plugin = !!pluginAction ? { name: pluginOrName, priority: priority, run: pluginAction } : plugin;
189+
var plugin:IEventPlugin = !!pluginAction ? { name: <string>pluginOrName, priority: priority, run: pluginAction } : <IEventPlugin>pluginOrName;
158190
if (!plugin || !plugin.run) {
159191
this.log.error('Unable to add plugin: No run method was found.');
160192
return;
@@ -197,6 +229,10 @@ module Exceptionless {
197229
}
198230
}
199231
}
232+
233+
public useReferenceIds(): void {
234+
this.addPlugin(new ReferenceIdPlugin());
235+
}
200236
}
201237

202238
export interface ILog {
@@ -360,7 +396,7 @@ module Exceptionless {
360396
durationInMinutes = 5;
361397
}
362398

363-
this._config.log.info('Suspending processing for ' + durationInMinutes + 'minutes.');
399+
this._config.log.info('Suspending processing for ' + durationInMinutes + ' minutes.');
364400
this._suspendProcessingUntil = new Date(new Date().getTime() + (durationInMinutes * 60000));
365401

366402
if (discardFutureQueuedItems) {
@@ -380,8 +416,8 @@ module Exceptionless {
380416
private requeueEvents(events:IEvent[]) {
381417
this._config.log.info('Requeuing ' + events.length + ' events.');
382418

383-
for (var event of events) {
384-
this.enqueue(event);
419+
for (var index = 0; index < events.length; index++) {
420+
this.enqueue(events[index]);
385421
}
386422
}
387423

@@ -627,6 +663,28 @@ module Exceptionless {
627663
}
628664
}
629665

666+
export interface ILastReferenceIdManager {
667+
getLast(): string;
668+
clearLast(): void;
669+
setLast(eventId:string): void;
670+
}
671+
672+
export class InMemoryLastReferenceIdManager implements ILastReferenceIdManager {
673+
private _lastReferenceId:string = null;
674+
675+
getLast(): string {
676+
return this._lastReferenceId;
677+
}
678+
679+
clearLast():void {
680+
this._lastReferenceId = null;
681+
}
682+
683+
setLast(eventId:string):void {
684+
this._lastReferenceId = eventId;
685+
}
686+
}
687+
630688
export interface IEvent {
631689
type?:string;
632690
source?:string;
@@ -720,9 +778,9 @@ module Exceptionless {
720778
this.target.tags = [];
721779
}
722780

723-
for(var tag of tags) {
724-
if (tag && this.target.tags.indexOf(tag) < 0) {
725-
this.target.tags.push(tag);
781+
for (var index = 0; index < tags.length; index++) {
782+
if (tags[index] && this.target.tags.indexOf(tags[index]) < 0) {
783+
this.target.tags.push(tags[index]);
726784
}
727785
}
728786

@@ -837,8 +895,9 @@ module Exceptionless {
837895

838896
class EventPluginManager {
839897
public static run(context:EventPluginContext): void {
840-
for (var plugin of context.client.config.plugins) {
898+
for (var index = 0; index < context.client.config.plugins.length; index++) {
841899
try {
900+
var plugin = context.client.config.plugins[index];
842901
plugin.run(context);
843902
if (context.cancel) {
844903
context.log.info('Event submission cancelled by plugin "' + plugin.name + '": id=' + context.event.reference_id + ' type=' + context.event.type);
@@ -859,6 +918,19 @@ module Exceptionless {
859918
}
860919
}
861920

921+
class ReferenceIdPlugin implements IEventPlugin {
922+
public priority:number = 20;
923+
public name:string = 'ReferenceIdPlugin';
924+
925+
run(context:Exceptionless.EventPluginContext): void {
926+
if ((!!context.event.reference_id && context.event.reference_id.length > 0) || context.event.type !== 'error') {
927+
return;
928+
}
929+
930+
context.event.reference_id = Random.guid().replace('-', '').substring(0, 10);
931+
}
932+
}
933+
862934
class ErrorPlugin implements IEventPlugin {
863935
public priority:number = 50;
864936
public name:string = 'ErrorPlugin';

0 commit comments

Comments
 (0)