-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathExceptionlessClient.ts
286 lines (235 loc) · 10 KB
/
ExceptionlessClient.ts
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
import { Configuration } from "./configuration/Configuration.js";
import { SettingsManager } from "./configuration/SettingsManager.js";
import { EventBuilder } from "./EventBuilder.js";
import { Event, KnownEventDataKeys } from "./models/Event.js";
import { UserDescription } from "./models/data/UserDescription.js";
import { EventContext } from "./models/EventContext.js";
import { EventPluginContext } from "./plugins/EventPluginContext.js";
import { EventPluginManager } from "./plugins/EventPluginManager.js";
import { PluginContext } from "./plugins/PluginContext.js";
export class ExceptionlessClient {
private _intervalId: ReturnType<typeof setInterval> | undefined;
private _timeoutId: ReturnType<typeof setTimeout> | undefined;
protected _initialized = false;
public constructor(public config: Configuration = new Configuration()) { }
/** Resume background submission, resume any timers. */
public async startup(configurationOrApiKey?: (config: Configuration) => void | string): Promise<void> {
if (configurationOrApiKey && !this._initialized) {
this._initialized = true;
if (typeof configurationOrApiKey === "string") {
this.config.apiKey = configurationOrApiKey;
} else {
configurationOrApiKey(this.config);
}
this.config.services.queue.onEventsPosted(() =>
Promise.resolve(this.updateSettingsTimer())
);
await SettingsManager.applySavedServerSettings(this.config);
}
if (!this.config.isValid) {
this.config.services.log.warn("Exceptionless is not configured and will not process events.");
return;
}
this.updateSettingsTimer(!!configurationOrApiKey);
await EventPluginManager.startup(new PluginContext(this));
const { queue } = this.config.services;
await queue.startup();
if (this.config.usePersistedQueueStorage) {
// TODO: Can we schedule this as part of startup?
await queue.process();
}
if (this.config.sessionsEnabled) {
await this.submitSessionStart();
}
}
/** Submit events, pause any timers and go into low power mode. */
public async suspend(): Promise<void> {
await EventPluginManager.suspend(new PluginContext(this));
const { queue } = this.config.services;
await queue.suspend();
await queue.process();
this.suspendSettingsTimer();
}
private suspendSettingsTimer(): void {
clearTimeout(this._timeoutId);
this._timeoutId = undefined;
clearInterval(this._intervalId);
this._intervalId = undefined;
}
public async processQueue(): Promise<void> {
await this.config.services.queue.process();
}
private updateSettingsTimer(startingUp: boolean = false) {
this.suspendSettingsTimer();
if (!this.config.isValid) {
return;
}
const interval = this.config.updateSettingsWhenIdleInterval;
if (interval > 0) {
let initialDelay: number = interval;
if (startingUp) {
initialDelay = this.config.settingsVersion > 0 ? 15000 : 5000;
}
this.config.services.log.info(`Update settings every ${interval}ms (${initialDelay || 0}ms delay)`);
// TODO: Look into better async scheduling..
const updateSettings = () =>
void SettingsManager.updateSettings(this.config);
if (initialDelay < interval) {
this._timeoutId = setTimeout(updateSettings, initialDelay);
}
this._intervalId = setInterval(updateSettings, interval);
}
}
public createException(exception: Error): EventBuilder {
const pluginContextData = new EventContext();
pluginContextData.setException(exception);
return this.createEvent(pluginContextData).setType("error");
}
public submitException(exception: Error): Promise<EventPluginContext> {
return this.createException(exception).submit();
}
public createUnhandledException(exception: Error, submissionMethod?: string): EventBuilder {
const builder = this.createException(exception);
builder.context.markAsUnhandledError();
builder.context.setSubmissionMethod(submissionMethod || "");
return builder;
}
public submitUnhandledException(exception: Error, submissionMethod?: string): Promise<EventPluginContext> {
return this.createUnhandledException(exception, submissionMethod).submit();
}
public createFeatureUsage(feature: string): EventBuilder {
return this.createEvent().setType("usage").setSource(feature);
}
public submitFeatureUsage(feature: string): Promise<EventPluginContext> {
return this.createFeatureUsage(feature).submit();
}
public createLog(message: string): EventBuilder;
public createLog(source: string, message: string): EventBuilder;
public createLog(source: string | undefined, message: string, level: string): EventBuilder;
public createLog(sourceOrMessage: string, message?: string, level?: string): EventBuilder {
let builder = this.createEvent().setType("log");
if (level) {
builder = builder.setSource(sourceOrMessage).setMessage(message)
.setProperty(KnownEventDataKeys.Level, level);
} else if (message) {
builder = builder.setSource(sourceOrMessage).setMessage(message);
} else {
builder = builder.setMessage(sourceOrMessage);
try {
// TODO: Look into using https://www.stevefenton.co.uk/Content/Blog/Date/201304/Blog/Obtaining-A-Class-Name-At-Runtime-In-TypeScript/
const caller = this.createLog.caller;
builder = builder.setSource(
caller && caller.caller && caller.caller.name,
);
} catch (ex) {
this.config.services.log.trace(`Unable to resolve log source: ${ex instanceof Error ? ex.message : ex + ''}`);
}
}
return builder;
}
public submitLog(message: string): Promise<EventPluginContext>;
public submitLog(source: string, message: string): Promise<EventPluginContext>;
public submitLog(source: string | undefined, message: string, level: string): Promise<EventPluginContext>;
public submitLog(sourceOrMessage: string, message?: string, level?: string): Promise<EventPluginContext> {
return this.createLog(sourceOrMessage, <string>message, <string>level).submit();
}
public createNotFound(resource: string): EventBuilder {
return this.createEvent().setType("404").setSource(resource);
}
public submitNotFound(resource: string): Promise<EventPluginContext> {
return this.createNotFound(resource).submit();
}
public createSessionStart(): EventBuilder {
return this.createEvent().setType("session");
}
public submitSessionStart(): Promise<EventPluginContext> {
return this.createSessionStart().submit();
}
public async submitSessionEnd(sessionIdOrUserId?: string): Promise<void> {
const { currentSessionIdentifier, enabled, isValid, services } = this.config;
const sessionId = sessionIdOrUserId || currentSessionIdentifier;
if (sessionId && enabled && isValid) {
services.log.info(`Submitting session end: ${sessionId}`);
await services.submissionClient.submitHeartbeat(sessionId, true);
}
}
public async submitSessionHeartbeat(sessionIdOrUserId?: string): Promise<void> {
const { currentSessionIdentifier, enabled, isValid, services } = this.config;
const sessionId = sessionIdOrUserId || currentSessionIdentifier;
if (sessionId && enabled && isValid) {
services.log.info(`Submitting session heartbeat: ${sessionId}`);
await services.submissionClient.submitHeartbeat(sessionId, false);
}
}
public createEvent(context?: EventContext): EventBuilder {
return new EventBuilder({ date: new Date() }, this, context);
}
/**
* Submits the event to the server.
*
* @param event The event
* @param context Contextual data used by event plugins to enrich the event details
*/
public async submitEvent(event: Event, context?: EventContext): Promise<EventPluginContext> {
const pluginContext = new EventPluginContext(this, event, context ?? new EventContext());
if (!event) {
pluginContext.cancelled = true;
return pluginContext;
}
if (!this.config.enabled || !this.config.isValid) {
this.config.services.log.info("Event submission is currently disabled.");
pluginContext.cancelled = true;
return pluginContext;
}
if (!event.data) {
event.data = {};
}
if (!event.tags || !event.tags.length) {
event.tags = [];
}
await EventPluginManager.run(pluginContext);
if (pluginContext.cancelled) {
return pluginContext;
}
const ev = pluginContext.event;
// ensure all required data
if (!ev.type || ev.type.length === 0) {
ev.type = "log";
}
if (!ev.date) {
ev.date = new Date();
}
await this.config.services.queue.enqueue(ev);
if (ev.reference_id && ev.reference_id.length > 0) {
pluginContext.log.info(`Setting last reference id "${ev.reference_id}"`);
this.config.services.lastReferenceIdManager.setLast(ev.reference_id);
}
return pluginContext;
}
/**
* Updates the user"s email address and description of an event for the specified reference id.
* @param referenceId The reference id of the event to update.
* @param email The user"s email address to set on the event.
* @param description The user"s description of the event.
* @param callback The submission response.
*/
public async updateUserEmailAndDescription(referenceId: string, email: string, description: string): Promise<void> {
if (!referenceId || !email || !description || !this.config.enabled || !this.config.isValid) {
return;
}
const userDescription: UserDescription = { email_address: email, description };
const response = await this.config.services.submissionClient.submitUserDescription(referenceId, userDescription);
if (!response.success) {
this.config.services.log.error(
`Failed to submit user email and description for event "${referenceId}": ${response.status} ${response.message}`,
);
}
}
/**
* Gets the last event client id that was submitted to the server.
* @returns {string} The event client id.
*/
public getLastReferenceId(): string | null {
return this.config.services.lastReferenceIdManager.getLast();
}
}