-
-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathscope.dart
More file actions
360 lines (297 loc) · 9.96 KB
/
scope.dart
File metadata and controls
360 lines (297 loc) · 9.96 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
import 'dart:collection';
import 'sentry_attachment/sentry_attachment.dart';
import 'event_processor.dart';
import 'protocol.dart';
import 'sentry_options.dart';
import 'sentry_tracer.dart';
import 'tracing.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;
}
}
/// Information about the current user.
SentryUser? 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);
/// add an entry to the Scope's contexts
void setContexts(String key, dynamic value) {
_contexts[key] = (value is num || value is bool || value is String)
? {'value': value}
: value;
}
/// Removes a value from the Scope's contexts
void removeContexts(String key) {
_contexts.remove(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> _attachements = [];
List<SentryAttachment> get attachements => List.unmodifiable(_attachements);
Scope(this._options);
/// Adds a breadcrumb to the breadcrumbs queue
void addBreadcrumb(Breadcrumb breadcrumb, {dynamic hint}) {
// bail out if maxBreadcrumbs is zero
if (_options.maxBreadcrumbs == 0) {
return;
}
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;
}
}
// remove first item if list is full
if (_breadcrumbs.length >= _options.maxBreadcrumbs &&
_breadcrumbs.isNotEmpty) {
_breadcrumbs.removeFirst();
}
_breadcrumbs.add(breadcrumb);
}
void addAttachment(SentryAttachment attachment) {
_attachements.add(attachment);
}
void clearAttachments() {
_attachements.clear();
}
/// Clear all the breadcrumbs
void clearBreadcrumbs() {
_breadcrumbs.clear();
}
/// Adds an event processor
void addEventProcessor(EventProcessor eventProcessor) {
_eventProcessors.add(eventProcessor);
}
/// Resets the Scope to its default state
void clear() {
clearBreadcrumbs();
clearAttachments();
level = null;
_span = null;
_transaction = null;
user = null;
_fingerprint = [];
_tags.clear();
_extra.clear();
_eventProcessors.clear();
}
/// Sets a tag to the Scope
void setTag(String key, String value) {
_tags[key] = value;
}
/// Removes a tag from the Scope
void removeTag(String key) {
_tags.remove(key);
}
/// Sets an extra to the Scope
void setExtra(String key, dynamic value) {
_extra[key] = value;
}
/// Removes an extra from the Scope
void removeExtra(String key) => _extra.remove(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 && 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 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
..user = user
..fingerprint = List.from(fingerprint)
.._transaction = _transaction
.._span = _span;
for (final tag in _tags.keys) {
clone.setTag(tag, _tags[tag]!);
}
for (final extraKey in _extra.keys) {
clone.setExtra(extraKey, _extra[extraKey]);
}
for (final breadcrumb in _breadcrumbs) {
clone.addBreadcrumb(breadcrumb);
}
for (final eventProcessor in _eventProcessors) {
clone.addEventProcessor(eventProcessor);
}
contexts.forEach((key, value) {
if (value != null) {
clone.setContexts(key, value);
}
});
for (final attachment in _attachements) {
clone.addAttachment(attachment);
}
return clone;
}
}