Repository navigation
Expand file tree
/
Copy pathscope.ts
More file actions
450 lines (394 loc) · 11.3 KB
/
Copy pathscope.ts
File metadata and controls
450 lines (394 loc) · 11.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
import {
Breadcrumb,
Context,
Contexts,
SentryEvent,
EventHint,
EventProcessor,
Extra,
Extras,
Primitive,
ScopeLike,
ScopeContext,
Severity,
Span,
Transaction,
User,
BreadcrumbHint,
} from '@sentry/types';
import { dateTimestampInSeconds, isPlainObject } from '@sentry/utils';
import { Session } from './session';
type ScopeOptions = {
maxBreadcrumbs?: number;
beforeBreadcrumb?(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): Breadcrumb | null;
};
/**
* Holds additional event information. {@link Scope.applyToEvent} will be
* called by the client before an event will be sent.
*/
export class Scope implements ScopeLike {
/** Array of breadcrumbs. */
public breadcrumbs: Breadcrumb[] = [];
/** User */
public user: User = {};
/** Tags */
public tags: { [key: string]: Primitive } = {};
/** Extra */
public extra: Extras = {};
/** Contexts */
public contexts: Contexts = {};
/** Fingerprint */
public fingerprint?: string[];
/** Severity */
public level?: Severity;
/** Transaction Name */
public transactionName?: string;
/** Span */
public span?: Span;
/** Session */
public session?: Session;
/** Flag if notifiying is happening. */
private _notifyingListeners: boolean = false;
/** Callback for client to receive scope changes. */
private _scopeListeners: Array<(scope: Scope) => void> = [];
/** Callback list that will be called after {@link applyToEvent}. */
private _eventProcessors: EventProcessor[] = [];
private _maxBreadcrumbs: number;
private _beforeBreadcrumb: (breadcrumb: Breadcrumb, hint?: BreadcrumbHint) => Breadcrumb | null;
public constructor({ maxBreadcrumbs, beforeBreadcrumb }: ScopeOptions = {}) {
this._maxBreadcrumbs = maxBreadcrumbs ?? 100;
this._beforeBreadcrumb = beforeBreadcrumb || (breadcrumb => breadcrumb);
}
/**
* Inherit values from the parent scope.
* @param scope to clone.
*/
public clone(): Scope {
const newScope = new Scope();
newScope.breadcrumbs = [...this.breadcrumbs];
newScope.tags = { ...this.tags };
newScope.extra = { ...this.extra };
newScope.contexts = { ...this.contexts };
newScope.user = this.user;
newScope.level = this.level;
newScope.span = this.span;
newScope.session = this.session;
newScope.transactionName = this.transactionName;
newScope.fingerprint = this.fingerprint;
newScope._eventProcessors = [...this._eventProcessors];
return newScope;
}
/**
* Add internal on change listener. Used for sub SDKs that need to store the scope.
* @hidden
*/
public addScopeListener(callback: (scope: Scope) => void): void {
this._scopeListeners.push(callback);
}
/**
* @inheritDoc
*/
public addEventProcessor(callback: EventProcessor): this {
this._eventProcessors.push(callback);
return this;
}
/**
* @inheritDoc
*/
public setUser(user: User | null): this {
this.user = user || {};
if (this.session) {
this.session.update({ user });
}
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public getUser(): User | undefined {
return this.user;
}
/**
* @inheritDoc
*/
public setTags(tags: { [key: string]: Primitive }): this {
this.tags = {
...this.tags,
...tags,
};
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public setTag(key: string, value: Primitive): this {
this.tags = { ...this.tags, [key]: value };
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public setExtras(extras: Extras): this {
this.extra = {
...this.extra,
...extras,
};
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public setExtra(key: string, extra: Extra): this {
this.extra = { ...this.extra, [key]: extra };
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public setFingerprint(fingerprint: string[]): this {
this.fingerprint = fingerprint;
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public setLevel(level: Severity): this {
this.level = level;
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public setTransactionName(name?: string): this {
this.transactionName = name;
this._notifyScopeListeners();
return this;
}
/**
* Can be removed in major version.
* @deprecated in favor of {@link this.setTransactionName}
*/
public setTransaction(name?: string): this {
return this.setTransactionName(name);
}
/**
* @inheritDoc
*/
public setContext(key: string, context: Context | null): this {
if (context === null) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.contexts[key];
} else {
this.contexts = { ...this.contexts, [key]: context };
}
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public setSpan(span?: Span): this {
this.span = span;
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public getSpan(): Span | undefined {
return this.span;
}
/**
* @inheritDoc
*/
public getTransaction(): Transaction | undefined {
// often, this span will be a transaction, but it's not guaranteed to be
const span = this.getSpan() as undefined | (Span & { spanRecorder: { spans: Span[] } });
// try it the new way first
if (span?.transaction) {
return span?.transaction;
}
// fallback to the old way (known bug: this only finds transactions with sampled = true)
if (span?.spanRecorder?.spans[0]) {
return span.spanRecorder.spans[0] as Transaction;
}
// neither way found a transaction
return undefined;
}
/**
* @inheritDoc
*/
public setSession(session?: Session): this {
if (!session) {
delete this.session;
} else {
this.session = session;
}
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public getSession(): Session | undefined {
return this.session;
}
/**
* @inheritDoc
*/
public update(captureContext: ScopeContext): this {
if (!isPlainObject(captureContext)) {
return this;
}
this.tags = { ...this.tags, ...captureContext.tags };
this.extra = { ...this.extra, ...captureContext.extra };
this.contexts = { ...this.contexts, ...captureContext.contexts };
if (captureContext.user && Object.keys(captureContext.user).length) {
this.user = captureContext.user;
}
if (captureContext.level) {
this.level = captureContext.level;
}
if (captureContext.fingerprint) {
this.fingerprint = captureContext.fingerprint;
}
return this;
}
/**
* @inheritDoc
*/
public clear(): this {
this.breadcrumbs = [];
this.tags = {};
this.extra = {};
this.user = {};
this.contexts = {};
this.level = undefined;
this.transactionName = undefined;
this.fingerprint = undefined;
this.span = undefined;
this.session = undefined;
this._notifyScopeListeners();
return this;
}
/**
* @inheritDoc
*/
public addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): this {
let preparedBreadcrumb: Breadcrumb | null = {
timestamp: dateTimestampInSeconds(),
...breadcrumb,
};
preparedBreadcrumb = this._beforeBreadcrumb(preparedBreadcrumb, hint);
if (preparedBreadcrumb !== null) {
// Absolute maximum number of breadcrumbs added to an event. The `maxBreadcrumbs` option cannot be higher than this value.
const maxBreadcrumbs = Math.min(this._maxBreadcrumbs, 100);
this.breadcrumbs = [...this.breadcrumbs, preparedBreadcrumb].slice(-maxBreadcrumbs);
this._notifyScopeListeners();
}
return this;
}
/**
* @inheritDoc
*/
public clearBreadcrumbs(): this {
this.breadcrumbs = [];
this._notifyScopeListeners();
return this;
}
/**
* Applies the current context and fingerprint to the event.
* Note that breadcrumbs will be added by the client.
* Also if the event has already breadcrumbs on it, we do not merge them.
* @param event Event
* @param hint May contain additional informartion about the original exception.
* @hidden
*/
public applyToEvent(event: SentryEvent, hint?: EventHint): SentryEvent | null {
if (this.extra && Object.keys(this.extra).length) {
event.extra = { ...this.extra, ...event.extra };
}
if (this.tags && Object.keys(this.tags).length) {
event.tags = { ...this.tags, ...event.tags };
}
if (this.user && Object.keys(this.user).length) {
event.user = { ...this.user, ...event.user };
}
if (this.contexts && Object.keys(this.contexts).length) {
event.contexts = { ...this.contexts, ...event.contexts };
}
if (this.level) {
event.level = this.level;
}
if (this.transactionName) {
event.transaction = this.transactionName;
}
// We want to set the trace context for normal events only if there isn't already
// a trace context on the event. There is a product feature in place where we link
// errors with transaction and it relys on that.
if (this.span) {
event.contexts = { trace: this.span.getTraceContext(), ...event.contexts };
const transactionName = this.span.transaction?.name;
if (transactionName) {
event.tags = { transaction: transactionName, ...event.tags };
}
}
this._applyFingerprint(event);
event.breadcrumbs = [...(event.breadcrumbs || []), ...this.breadcrumbs];
event.breadcrumbs = event.breadcrumbs.length > 0 ? event.breadcrumbs : undefined;
let processedEvent = event;
for (const processor of this._eventProcessors) {
if (typeof processor === 'function') {
const nextEvent = processor(processedEvent, hint);
if (nextEvent === null) {
return null;
}
processedEvent = nextEvent;
}
}
return processedEvent;
}
// TODO: Is there no better solution to this? I know that Electron/RN(?) is using it.
/**
* This will be called on every set call.
*/
protected _notifyScopeListeners(): void {
// We need this check for this._notifyingListeners to be able to work on scope during updates
// If this check is not here we'll produce endless recursion when something is done with the scope
// during the callback.
if (!this._notifyingListeners) {
this._notifyingListeners = true;
this._scopeListeners.forEach(callback => {
callback(this);
});
this._notifyingListeners = false;
}
}
/**
* Applies fingerprint from the scope to the event if there's one,
* uses message if there's one instead or get rid of empty fingerprint
*/
private _applyFingerprint(event: SentryEvent): void {
// Make sure it's an array first and we actually have something in place
event.fingerprint = event.fingerprint
? Array.isArray(event.fingerprint)
? event.fingerprint
: [event.fingerprint]
: [];
// If we have something on the scope, then merge it with event
if (this.fingerprint) {
event.fingerprint = event.fingerprint.concat(this.fingerprint);
}
// If we have no data at all, remove empty array default
if (event.fingerprint && !event.fingerprint.length) {
delete event.fingerprint;
}
}
}