-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathbaseclient.ts
527 lines (460 loc) · 16.4 KB
/
baseclient.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
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
import { Scope, Session } from '@sentry/scope';
import {
CaptureContext,
ClientLike,
SentryEvent,
EventProcessor,
Options,
ScopeLike,
SessionStatus,
Integration,
TransportRequest,
Transport,
EventType,
} from '@sentry/types';
import {
dateTimestampInSeconds,
getEventDescription,
Logger,
isPlainObject,
isPrimitive,
normalize,
truncate,
uuid4,
} from '@sentry/utils';
import { Dsn, eventToTransportRequest, NoopTransport, sessionToTransportRequest } from '@sentry/transport-base';
import { collectIntegrations } from './integrations';
/**
* Base implementation for all JavaScript SDK clients.
*
* Call the constructor with the corresponding backend constructor and options
* specific to the client subclass. To access these options later, use
* {@link Client.getOptions}. Also, the Backend instance is available via
* {@link Client.getBackend}.
*
* If a Dsn is specified in the options, it will be parsed and stored. Use
* {@link Client.getDsn} to retrieve the Dsn at any moment. In case the Dsn is
* invalid, the constructor will throw a {@link SentryException}. Note that
* without a valid Dsn, the SDK will not send any events to Sentry.
*
* Before sending an event via the backend, it is passed through
* {@link BaseClient._prepareEvent} to add SDK information and scope data
* (breadcrumbs and context). To add more custom information, override this
* method and extend the resulting prepared event.
*
* To issue automatically created events (e.g. via instrumentation), use
* {@link Client.captureEvent}. It will prepare the event and pass it through
* the callback lifecycle. To issue auto-breadcrumbs, use
* {@link Client.addBreadcrumb}.
*
* @example
* class NodeClient extends BaseClient<NodeBackend, NodeOptions> {
* public constructor(options: NodeOptions) {
* super(NodeBackend, options);
* }
*
* // ...
* }
*/
export abstract class BaseClient<O extends Options> implements ClientLike<O> {
/** Options passed to the SDK. */
public readonly options: O;
/** Logger used for debug mode */
public readonly logger = new Logger('Client');
/** The client Dsn, if specified in options. Without this Dsn, the SDK will be disabled. */
public readonly dsn?: Dsn;
protected _integrations: Record<string, Integration> = {};
protected _transport: Transport;
protected _lastEventId?: string;
protected _scope: ScopeLike;
protected _eventProcessors: EventProcessor[] = [];
protected _lastException?: unknown;
/**
* Initializes this client instance.
*
* @param options Options for the client.
*/
protected constructor(options: O) {
this.options = options || {};
this.logger.enabled = !!this.options.debug;
if (this.options.dsn) {
this.dsn = new Dsn(this.options.dsn);
}
this._scope = this.options._internal?.scope || new Scope();
this._transport =
!this.options.dsn || !this.options.transport
? new NoopTransport()
: new this.options.transport({
dsn: this.options.dsn,
...this.options.transportOptions,
});
this._integrations = this._setupIntegrations();
}
public lastEventId(): string | undefined {
return this._lastEventId;
}
public getScope(): ScopeLike {
return this._scope;
}
public setScope(scope: ScopeLike): void {
this._scope = scope;
}
public addEventProcessor(callback: EventProcessor): void {
this._eventProcessors.push(callback);
}
/**
* @inheritDoc
*/
public captureException(exception: unknown, captureContext: CaptureContext = {}): string | undefined {
const event = this._eventFromException(exception, captureContext);
return this.captureEvent(event, captureContext);
}
/**
* @inheritDoc
*/
public captureMessage(message: string, captureContext: CaptureContext = {}): string | undefined {
const event = isPrimitive(message)
? this._eventFromMessage(String(message), captureContext)
: this._eventFromException(message, captureContext);
return this.captureEvent(event, captureContext);
}
/**
* @inheritDoc
*/
public captureEvent(event: SentryEvent, captureContext: CaptureContext = {}): string | undefined {
// Drop two consecutive events originating from the same source (eg. browser Wrap integrations)
if (this._lastException && this._lastException === captureContext.hint?.originalException) {
delete this._lastException;
return;
} else {
this._lastException = captureContext.hint?.originalException;
}
const eventId = this._captureEvent(event, captureContext);
this._lastEventId = eventId;
return eventId;
}
/**
* @inheritDoc
*/
public captureSession(session: Session): void {
if (!session.release) {
this.logger.warn('Discarded session because of missing release');
} else {
this._sendSession(session);
// After sending, we set init false to inidcate it's not the first occurence
session.update({ init: false });
}
}
/**
* @inheritDoc
*/
public flush(timeout: number = 0): PromiseLike<boolean> {
return this._transport.flush(timeout);
}
/**
* @inheritDoc
*/
public close(timeout: number = 0): PromiseLike<boolean> {
return this._transport.flush(timeout).then(result => {
this.options.enabled = false;
return result;
});
}
protected _setupIntegrations(): Record<string, Integration> {
const integrations = collectIntegrations({
defaultIntegrations: this.options.defaultIntegrations ? this.options._internal?.defaultIntegrations : [],
discoveredIntegrations: this.options.discoverIntegrations ? this.options._internal?.discoveredIntegrations : [],
userIntegrations: this.options.integrations ? this.options.integrations : [],
});
return integrations.reduce((integrationsIndex: Record<string, Integration>, integration) => {
integrationsIndex[integration.name] = integration;
integration.install(this);
this.logger.log(`Integration installed: ${integration.name}`);
return integrationsIndex;
}, {});
}
/**
* @inheritDoc
*/
// TODO: Do we need generic here?
protected _sendRequest<T>(request: TransportRequest<T>): void {
this._transport.sendRequest(request).then(null, reason => {
this.logger.error(`Failed sending request: ${reason}`);
});
}
/** Updates existing session based on the provided event */
protected _updateSessionFromEvent(session: Session, event: SentryEvent): void {
let crashed = false;
let errored = false;
let userAgent;
const exceptions = event.exception && event.exception.values;
if (exceptions) {
errored = true;
for (const ex of exceptions) {
const mechanism = ex.mechanism;
if (mechanism && mechanism.handled === false) {
crashed = true;
break;
}
}
}
const user = event.user;
if (!session.userAgent) {
const headers = event.request ? event.request.headers : {};
for (const key in headers) {
if (key.toLowerCase() === 'user-agent') {
userAgent = headers[key];
break;
}
}
}
session.update({
...(crashed && { status: SessionStatus.Crashed }),
user,
userAgent,
errors: session.errors + Number(errored || crashed),
});
this.captureSession(session);
}
/** Deliver captured session to Sentry */
protected _sendSession(session: Session): void {
this._sendRequest(sessionToTransportRequest(session));
}
/**
* Applies `normalize` function on necessary `Event` attributes to make them safe for serialization.
* Normalized keys:
* - `breadcrumbs.data`
* - `user`
* - `contexts`
* - `extra`
* @param event Event
* @returns Normalized event
*/
protected _normalizeEvent(event: SentryEvent | null, depth: number): SentryEvent | null {
if (!event) {
return null;
}
const normalized = {
...event,
...(event.breadcrumbs && {
breadcrumbs: event.breadcrumbs.map(b => ({
...b,
...(b.data && {
data: normalize(b.data, depth),
}),
})),
}),
...(event.user && {
user: normalize(event.user, depth),
}),
...(event.contexts && {
contexts: normalize(event.contexts, depth),
}),
...(event.extra && {
extra: normalize(event.extra, depth),
}),
};
// event.contexts.trace stores information about a Transaction. Similarly,
// event.spans[] stores information about child Spans. Given that a
// Transaction is conceptually a Span, normalization should apply to both
// Transactions and Spans consistently.
// For now the decision is to skip normalization of Transactions and Spans,
// so this block overwrites the normalized event to add back the original
// Transaction information prior to normalization.
if (event.contexts && event.contexts.trace) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
normalized.contexts.trace = event.contexts.trace;
}
return normalized;
}
/**
* Enhances event using the client configuration.
* It takes care of all "static" values like environment, release and `dist`,
* as well as truncating overly long values.
* @param event event instance to be enhanced
*/
protected _applyClientOptions(event: SentryEvent): void {
const options = this.options;
const { environment, release, dist, maxValueLength = 250 } = options;
if (!('environment' in event)) {
event.environment = 'environment' in options ? environment : 'production';
}
if (event.release === undefined && release !== undefined) {
event.release = release;
}
if (event.dist === undefined && dist !== undefined) {
event.dist = dist;
}
if (event.message) {
event.message = truncate(event.message, maxValueLength);
}
const exception = event.exception && event.exception.values && event.exception.values[0];
if (exception && exception.value) {
exception.value = truncate(exception.value, maxValueLength);
}
const request = event.request;
if (request && request.url) {
request.url = truncate(request.url, maxValueLength);
}
}
protected _applySdkMetadata(event: SentryEvent): void {
if (this.options._internal?.sdk) {
const { name, version, integrations, packages } = this.options._internal?.sdk;
event.sdk = event.sdk || {
name,
version,
};
event.sdk.name = event.sdk.name || name;
event.sdk.version = event.sdk.version || version;
event.sdk.integrations = [...(event.sdk.integrations || []), ...(integrations || [])];
event.sdk.packages = [...(event.sdk.packages || []), ...(packages || [])];
}
}
/**
* This function adds all used integrations to the SDK info in the event.
* @param event The event that will be filled with all integrations.
*/
protected _applyIntegrationsMetadata(event: SentryEvent): void {
const sdkInfo = event.sdk;
const integrationsArray = Object.keys(this._integrations);
if (sdkInfo && integrationsArray.length > 0) {
sdkInfo.integrations = integrationsArray;
}
}
/**
* Tells the backend to send this event
* @param event The Sentry event to send
*/
protected _sendEvent(event: SentryEvent): void {
this._sendRequest(eventToTransportRequest(event));
}
/**
* Processes the event and logs an error in case of rejection
* @param event
* @param hint
* @param scope
*/
protected _captureEvent(event: SentryEvent, captureContext: CaptureContext): string | undefined {
const processedEvent = this._processEvent(event, captureContext);
if (!processedEvent) {
return;
}
// TODO: Make it configurable or move to @sentry/integration-browser-breadcrumbs
const eventType = processedEvent.type === EventType.Transaction ? 'transaction' : 'event';
this.getScope().addBreadcrumb(
{
category: `sentry.${eventType}`,
event_id: processedEvent.event_id,
level: processedEvent.level,
message: getEventDescription(processedEvent),
},
{
event: processedEvent,
},
);
return processedEvent.event_id;
}
/**
* Processes an event (either error or message) and sends it to Sentry.
*
* This also adds breadcrumbs and context information to the event. However,
* platform specific meta data (such as the User's IP address) must be added
* by the SDK implementor.
*
*
* @param event The event to send to Sentry.
* @param hint May contain additional information about the original exception.
* @param scope A scope containing event metadata.
* @returns A Promise that resolves with the event or rejects in case event was/will not be send.
*/
// eslint-disable-next-line complexity
protected _processEvent(event: SentryEvent, captureContext: CaptureContext): SentryEvent | null {
if (this.options.enabled === false) {
this.logger.error('SDK not enabled, will not send event.');
return null;
}
const isTransaction = event.type === 'transaction';
// 1.0 === 100% events are sent
// 0.0 === 0% events are sent
// Sampling for transaction happens somewhere else
if (!isTransaction && typeof this.options.sampleRate === 'number' && Math.random() > this.options.sampleRate) {
this.logger.error(
`Discarding event because it's not included in the random sample (sampling rate = ${this.options.sampleRate})`,
);
return null;
}
try {
let processedEvent: SentryEvent | null = {
...event,
event_id: event.event_id || uuid4(),
timestamp: event.timestamp || dateTimestampInSeconds(),
};
this._applyClientOptions(processedEvent);
this._applySdkMetadata(processedEvent);
this._applyIntegrationsMetadata(processedEvent);
const scope =
captureContext.scope instanceof Scope
? captureContext.scope
: this.getScope()
.clone()
.update(captureContext.scope);
processedEvent = scope.applyToEvent(processedEvent, captureContext.hint);
if (processedEvent === null) {
this.logger.error('A scope event processor returned null, will not send event.');
return null;
}
for (const processor of this._eventProcessors) {
if (typeof processor === 'function') {
const nextEvent = processor(processedEvent, captureContext.hint);
if (nextEvent === null) {
this.logger.error('A client event processor returned null, will not send event.');
return null;
}
processedEvent = nextEvent;
}
}
if (processedEvent === null) {
this.logger.error('A scope event processor returned null, will not send event.');
return null;
}
const normalizeDepth = this.options.normalizeDepth ?? 3;
if (normalizeDepth > 0) {
processedEvent = this._normalizeEvent(processedEvent, normalizeDepth);
}
const isInternalException = captureContext?.hint?.data?.__sentry__ === true;
if (isInternalException || isTransaction || !this.options.beforeSend) {
return processedEvent;
}
processedEvent = this.options.beforeSend(processedEvent as SentryEvent, captureContext?.hint);
if (!(isPlainObject(processedEvent) || processedEvent === null)) {
this.logger.error('`beforeSend` method has to return `null` or a valid event.');
return null;
}
if (processedEvent === null) {
this.logger.error('`beforeSend` returned `null`, will not send event.');
return null;
}
const session = this.getScope().getSession();
if (!isTransaction && session) {
this._updateSessionFromEvent(session as Session, processedEvent);
}
this._sendEvent(processedEvent);
return processedEvent;
} catch (e) {
this.captureException(e, {
hint: {
data: {
__sentry__: true,
},
originalException: e,
},
});
this.logger.error(
`Event processing pipeline threw an error, original event will not be sent. Details have been sent as a new event.\nReason: ${e}`,
);
return null;
}
}
protected abstract _eventFromException(exception: unknown, captureContext: CaptureContext): SentryEvent;
protected abstract _eventFromMessage(message: string, captureContext: CaptureContext): SentryEvent;
}