-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
scope.dart
458 lines (380 loc) · 13.1 KB
/
scope.dart
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
import 'dart:async';
import 'dart:collection';
import 'event_processor.dart';
import 'protocol.dart';
import 'scope_observer.dart';
import 'sentry_attachment/sentry_attachment.dart';
import 'sentry_options.dart';
import 'sentry_span_interface.dart';
import 'sentry_tracer.dart';
/// Scope data to be sent with the event
class Scope {
/// How important this event is.
SentryLevel? level;
String? _transaction;
/// The name of the transaction which generated this event,
/// for example, the route name: `"/users/<username>/"`.
String? get transaction {
return ((_span is SentryTracer) ? (_span as SentryTracer?)?.name : null) ??
_transaction;
}
set transaction(String? transaction) {
_transaction = transaction;
if (_transaction != null && _span != null) {
final currentTransaction =
(_span is SentryTracer) ? (_span as SentryTracer?) : null;
currentTransaction?.name = _transaction!;
}
}
ISentrySpan? _span;
/// Returns active transaction or null if there is no active transaction.
ISentrySpan? get span => _span;
set span(ISentrySpan? span) {
_span = span;
if (_span != null) {
final currentTransaction =
(_span is SentryTracer) ? (_span as SentryTracer?) : null;
_transaction = currentTransaction?.name ?? _transaction;
}
}
SentryUser? _user;
/// Get the current user.
SentryUser? get user => _user;
/// Sets the current user
/// This method is deprecated, use the [setUser(user)] instead.
/// This method will be removed in the future.
/// The breaking change is due to the [enableScopeSync] feature that
/// requires returning a [Future].
@Deprecated('Use [setUser(user)] instead')
set user(SentryUser? user) {
_user = user;
}
void _setUserSync(SentryUser? user) {
_user = user;
}
/// Set the current user.
Future<void> setUser(SentryUser? user) async {
_setUserSync(user);
await _callScopeObservers(
(scopeObserver) async => await scopeObserver.setUser(user));
}
List<String> _fingerprint = [];
/// Used to deduplicate events by grouping ones with the same fingerprint
/// together.
///
/// Example:
///
/// // A completely custom fingerprint:
/// var custom = ['foo', 'bar', 'baz'];
List<String> get fingerprint => List.unmodifiable(_fingerprint);
set fingerprint(List<String> fingerprint) {
_fingerprint = List.from(fingerprint);
}
/// List of breadcrumbs for this scope.
final Queue<Breadcrumb> _breadcrumbs = Queue();
/// Unmodifiable List of breadcrumbs
/// See also:
/// * https://docs.sentry.io/enriching-error-data/breadcrumbs/?platform=javascript
List<Breadcrumb> get breadcrumbs => List.unmodifiable(_breadcrumbs);
final Map<String, String> _tags = {};
/// Name/value pairs that events can be searched by.
Map<String, String> get tags => Map.unmodifiable(_tags);
final Map<String, dynamic> _extra = {};
/// Arbitrary name/value pairs attached to the scope.
///
/// Sentry.io docs do not talk about restrictions on the values, other than
/// they must be JSON-serializable.
Map<String, dynamic> get extra => Map.unmodifiable(_extra);
final Contexts _contexts = Contexts();
/// Unmodifiable map of the scope contexts key/value
/// See also:
/// * https://docs.sentry.io/platforms/java/enriching-events/context/
Map<String, dynamic> get contexts => Map.unmodifiable(_contexts);
void _setContextsSync(String key, dynamic value) {
// if it's a List, it should not be a List<SentryRuntime> because it can't
// be wrapped by the value object since it's a special property for having
// multiple runtimes and it has a dedicated property within the Contexts class.
_contexts[key] = (value is num ||
value is bool ||
value is String ||
(value is List &&
(value is! List<SentryRuntime> &&
key != SentryRuntime.listType)))
? {'value': value}
: value;
}
/// add an entry to the Scope's contexts
Future<void> setContexts(String key, dynamic value) async {
_setContextsSync(key, value);
await _callScopeObservers(
(scopeObserver) async => await scopeObserver.setContexts(key, value));
}
/// Removes a value from the Scope's contexts
Future<void> removeContexts(String key) async {
_contexts.remove(key);
await _callScopeObservers(
(scopeObserver) async => await scopeObserver.removeContexts(key));
}
/// Scope's event processor list
///
/// Scope's event processors are executed before the global Event processors
final List<EventProcessor> _eventProcessors = [];
List<EventProcessor> get eventProcessors =>
List.unmodifiable(_eventProcessors);
final SentryOptions _options;
final List<SentryAttachment> _attachments = [];
List<SentryAttachment> get attachments => List.unmodifiable(_attachments);
@Deprecated('Use [attachments] instead')
List<SentryAttachment> get attachements => attachments;
Scope(this._options);
bool _addBreadCrumbSync(Breadcrumb breadcrumb, {dynamic hint}) {
// bail out if maxBreadcrumbs is zero
if (_options.maxBreadcrumbs == 0) {
return false;
}
Breadcrumb? processedBreadcrumb = breadcrumb;
// run before breadcrumb callback if set
if (_options.beforeBreadcrumb != null) {
processedBreadcrumb = _options.beforeBreadcrumb!(
processedBreadcrumb,
hint: hint,
);
if (processedBreadcrumb == null) {
_options.logger(
SentryLevel.info,
'Breadcrumb was dropped by beforeBreadcrumb',
);
return false;
}
}
// remove first item if list is full
if (_breadcrumbs.length >= _options.maxBreadcrumbs &&
_breadcrumbs.isNotEmpty) {
_breadcrumbs.removeFirst();
}
_breadcrumbs.add(processedBreadcrumb);
return true;
}
/// Adds a breadcrumb to the breadcrumbs queue
Future<void> addBreadcrumb(Breadcrumb breadcrumb, {dynamic hint}) async {
if (_addBreadCrumbSync(breadcrumb, hint: hint)) {
await _callScopeObservers((scopeObserver) async =>
await scopeObserver.addBreadcrumb(breadcrumb));
}
}
void addAttachment(SentryAttachment attachment) {
_attachments.add(attachment);
}
void clearAttachments() {
_attachments.clear();
}
void _clearBreadcrumbsSync() {
_breadcrumbs.clear();
}
/// Clear all the breadcrumbs
Future<void> clearBreadcrumbs() async {
_clearBreadcrumbsSync();
await _callScopeObservers(
(scopeObserver) async => await scopeObserver.clearBreadcrumbs());
}
/// Adds an event processor
void addEventProcessor(EventProcessor eventProcessor) {
_eventProcessors.add(eventProcessor);
}
/// Resets the Scope to its default state
Future<void> clear() async {
clearAttachments();
level = null;
_span = null;
_transaction = null;
_fingerprint = [];
_tags.clear();
_extra.clear();
_eventProcessors.clear();
_clearBreadcrumbsSync();
_setUserSync(null);
await clearBreadcrumbs();
await setUser(null);
}
void _setTagSync(String key, String value) {
_tags[key] = value;
}
/// Sets a tag to the Scope
Future<void> setTag(String key, String value) async {
_setTagSync(key, value);
await _callScopeObservers(
(scopeObserver) async => await scopeObserver.setTag(key, value));
}
/// Removes a tag from the Scope
Future<void> removeTag(String key) async {
_tags.remove(key);
await _callScopeObservers(
(scopeObserver) async => await scopeObserver.removeTag(key));
}
void _setExtraSync(String key, dynamic value) {
_extra[key] = value;
}
/// Sets an extra to the Scope
Future<void> setExtra(String key, dynamic value) async {
_setExtraSync(key, value);
await _callScopeObservers(
(scopeObserver) async => await scopeObserver.setExtra(key, value));
}
/// Removes an extra from the Scope
Future<void> removeExtra(String key) async {
_extra.remove(key);
await _callScopeObservers(
(scopeObserver) async => await scopeObserver.removeExtra(key));
}
Future<SentryEvent?> applyToEvent(
SentryEvent event, {
dynamic hint,
}) async {
event = event.copyWith(
transaction: event.transaction ?? _transaction,
user: _mergeUsers(user, event.user),
breadcrumbs: (event.breadcrumbs?.isNotEmpty ?? false)
? event.breadcrumbs
: List.from(_breadcrumbs),
tags: tags.isNotEmpty ? _mergeEventTags(event) : event.tags,
extra: extra.isNotEmpty ? _mergeEventExtra(event) : event.extra,
);
if (event is! SentryTransaction) {
event = event.copyWith(
fingerprint: (event.fingerprint?.isNotEmpty ?? false)
? event.fingerprint
: _fingerprint,
level: level ?? event.level);
}
_contexts.clone().forEach((key, value) {
// add the contexts runtime list to the event.contexts.runtimes
if (key == SentryRuntime.listType &&
value is List<SentryRuntime> &&
value.isNotEmpty) {
_mergeEventContextsRuntimes(value, event);
} else if (key != SentryRuntime.listType &&
(!event.contexts.containsKey(key) || event.contexts[key] == null) &&
value != null) {
event.contexts[key] = value;
}
});
final span = _span;
if (event.contexts.trace == null && span != null) {
event.contexts.trace = span.context.toTraceContext(
sampled: span.sampled,
);
}
SentryEvent? processedEvent = event;
for (final processor in _eventProcessors) {
try {
processedEvent = await processor.apply(processedEvent!, hint: hint);
} catch (exception, stackTrace) {
_options.logger(
SentryLevel.error,
'An exception occurred while processing event by a processor',
exception: exception,
stackTrace: stackTrace,
);
}
if (processedEvent == null) {
_options.logger(SentryLevel.debug, 'Event was dropped by a processor');
break;
}
}
return processedEvent;
}
/// Merge the scope contexts runtimes and the event contexts runtimes.
void _mergeEventContextsRuntimes(
List<SentryRuntime> values, SentryEvent event) {
for (final runtime in values) {
event.contexts.addRuntime(runtime);
}
}
/// If the scope and the event have tag entries with the same key,
/// the event tags will be kept.
Map<String, String> _mergeEventTags(SentryEvent event) =>
tags.map((key, value) => MapEntry(key, value))..addAll(event.tags ?? {});
/// If the scope and the event have extra entries with the same key,
/// the event extra will be kept.
Map<String, dynamic> _mergeEventExtra(SentryEvent event) =>
extra.map((key, value) => MapEntry(key, value))
..addAll(event.extra ?? {});
/// If scope and event have a user, the user of the event takes
/// precedence.
SentryUser? _mergeUsers(SentryUser? scopeUser, SentryUser? eventUser) {
if (scopeUser == null && eventUser != null) {
return eventUser;
}
if (eventUser == null && scopeUser != null) {
return scopeUser;
}
// otherwise the user of scope takes precedence over the event user
return scopeUser?.copyWith(
id: eventUser?.id,
email: eventUser?.email,
ipAddress: eventUser?.ipAddress,
username: eventUser?.username,
extras: _mergeUserExtra(eventUser?.extras, scopeUser.extras),
);
}
/// If the User on the scope and the user of an event have extra entries with
/// the same key, the event user extra will be kept.
Map<String, dynamic> _mergeUserExtra(
Map<String, dynamic>? eventExtra,
Map<String, dynamic>? scopeExtra,
) {
final map = <String, dynamic>{};
if (eventExtra != null) {
map.addAll(eventExtra);
}
if (scopeExtra == null) {
return map;
}
for (var value in scopeExtra.entries) {
map.putIfAbsent(value.key, () => value.value);
}
return map;
}
/// Clones the current Scope
Scope clone() {
final clone = Scope(_options)
..level = level
..fingerprint = List.from(fingerprint)
.._transaction = _transaction
.._span = _span;
_setUserSync(user);
final tags = List.from(_tags.keys);
for (final tag in tags) {
final value = _tags[tag];
if (value != null) {
clone._setTagSync(tag, value);
}
}
for (final extraKey in List.from(_extra.keys)) {
clone._setExtraSync(extraKey, _extra[extraKey]);
}
for (final breadcrumb in List.from(_breadcrumbs)) {
clone._addBreadCrumbSync(breadcrumb);
}
for (final eventProcessor in List.from(_eventProcessors)) {
clone.addEventProcessor(eventProcessor);
}
for (final entry in Map.from(contexts).entries) {
if (entry.value != null) {
clone._setContextsSync(entry.key, entry.value);
}
}
for (final attachment in List.from(_attachments)) {
clone.addAttachment(attachment);
}
return clone;
}
Future<void> _callScopeObservers(
Future<void> Function(ScopeObserver) action) async {
if (_options.enableScopeSync) {
for (final scopeObserver in _options.scopeObservers) {
await action(scopeObserver);
}
}
}
}