-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
load_contexts_integration.dart
205 lines (175 loc) · 6.98 KB
/
load_contexts_integration.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
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:sentry/sentry.dart';
import '../sentry_flutter_options.dart';
/// Load Device's Contexts from the iOS SDK.
///
/// This integration calls the iOS SDK via Message channel to load the
/// Device's contexts before sending the event back to the iOS SDK via
/// Message channel (already enriched with all the information).
///
/// The Device's contexts are:
/// App, Device and OS.
///
/// ps. This integration won't be run on Android because the Device's Contexts
/// is set on Android when the event is sent to the Android SDK via
/// the Message channel.
/// We intend to unify this behaviour in the future.
///
/// This integration is only executed on iOS & MacOS Apps.
class LoadContextsIntegration extends Integration<SentryFlutterOptions> {
final MethodChannel _channel;
LoadContextsIntegration(this._channel);
@override
FutureOr<void> call(Hub hub, SentryFlutterOptions options) async {
options.addEventProcessor(
_LoadContextsIntegrationEventProcessor(_channel, options),
);
options.sdk.addIntegration('loadContextsIntegration');
}
}
class _LoadContextsIntegrationEventProcessor extends EventProcessor {
_LoadContextsIntegrationEventProcessor(this._channel, this._options);
final MethodChannel _channel;
final SentryFlutterOptions _options;
@override
FutureOr<SentryEvent?> apply(SentryEvent event, {hint}) async {
try {
final loadContexts = await _channel.invokeMethod('loadContexts');
final infos =
Map<String, dynamic>.from(loadContexts is Map ? loadContexts : {});
final contextsMap = infos['contexts'] as Map?;
if (contextsMap != null && contextsMap.isNotEmpty) {
final contexts = Contexts.fromJson(
Map<String, dynamic>.from(contextsMap),
);
final eventContexts = event.contexts.clone();
contexts.forEach(
(key, dynamic value) {
if (value != null) {
final currentValue = eventContexts[key];
if (key == SentryRuntime.listType) {
contexts.runtimes.forEach(eventContexts.addRuntime);
} else if (currentValue == null) {
eventContexts[key] = value;
} else {
if (key == SentryOperatingSystem.type &&
currentValue is SentryOperatingSystem &&
value is SentryOperatingSystem) {
final osMap = {...value.toJson(), ...currentValue.toJson()};
final os = SentryOperatingSystem.fromJson(osMap);
eventContexts[key] = os;
}
}
}
},
);
event = event.copyWith(contexts: eventContexts);
}
final tagsMap = infos['tags'] as Map?;
if (tagsMap != null && tagsMap.isNotEmpty) {
final tags = event.tags ?? {};
final newTags = Map<String, String>.from(tagsMap);
for (final tag in newTags.entries) {
if (!tags.containsKey(tag.key)) {
tags[tag.key] = tag.value;
}
}
event = event.copyWith(tags: tags);
}
final extraMap = infos['extra'] as Map?;
if (extraMap != null && extraMap.isNotEmpty) {
final extras = event.extra ?? {};
final newExtras = Map<String, dynamic>.from(extraMap);
for (final extra in newExtras.entries) {
if (!extras.containsKey(extra.key)) {
extras[extra.key] = extra.value;
}
}
event = event.copyWith(extra: extras);
}
final userMap = infos['user'] as Map?;
if (event.user == null && userMap != null && userMap.isNotEmpty) {
final user = Map<String, dynamic>.from(userMap);
event = event.copyWith(user: SentryUser.fromJson(user));
}
final distString = infos['dist'] as String?;
if (event.dist == null && distString != null) {
event = event.copyWith(dist: distString);
}
final environmentString = infos['environment'] as String?;
if (event.environment == null && environmentString != null) {
event = event.copyWith(environment: environmentString);
}
final fingerprintList = infos['fingerprint'] as List?;
if (fingerprintList != null && fingerprintList.isNotEmpty) {
final eventFingerprints = event.fingerprint ?? [];
final newFingerprint = List<String>.from(fingerprintList);
for (final fingerprint in newFingerprint) {
if (!eventFingerprints.contains(fingerprint)) {
eventFingerprints.add(fingerprint);
}
}
event = event.copyWith(fingerprint: eventFingerprints);
}
final levelString = infos['level'] as String?;
if (event.level == null && levelString != null) {
event = event.copyWith(level: SentryLevel.fromName(levelString));
}
final breadcrumbsList = infos['breadcrumbs'] as List?;
if (breadcrumbsList != null && breadcrumbsList.isNotEmpty) {
final breadcrumbs = event.breadcrumbs ?? [];
final newBreadcrumbs =
List<Map<dynamic, dynamic>>.from(breadcrumbsList);
for (final breadcrumb in newBreadcrumbs) {
final newBreadcrumb = Map<String, dynamic>.from(breadcrumb);
final crumb = Breadcrumb.fromJson(newBreadcrumb);
breadcrumbs.add(crumb);
}
breadcrumbs.sort((a, b) {
return a.timestamp.compareTo(b.timestamp);
});
event = event.copyWith(breadcrumbs: breadcrumbs);
}
final integrationsList = infos['integrations'] as List?;
if (integrationsList != null && integrationsList.isNotEmpty) {
final integrations = List<String>.from(integrationsList);
final sdk = event.sdk ?? _options.sdk;
for (final integration in integrations) {
sdk.addIntegration(integration);
}
event = event.copyWith(sdk: sdk);
}
final packageMap = infos['package'] as Map?;
if (packageMap != null && packageMap.isNotEmpty) {
final package = Map<String, String>.from(packageMap);
final sdk = event.sdk ?? _options.sdk;
final name = package['sdk_name'];
final version = package['version'];
if (name != null &&
version != null &&
!sdk.packages.any((element) =>
element.name == name && element.version == version)) {
sdk.addPackage(name, version);
}
event = event.copyWith(sdk: sdk);
}
// on iOS, captureEnvelope does not call the beforeSend callback,
// hence we need to add these tags here.
if (event.sdk?.name == 'sentry.dart.flutter') {
final tags = event.tags ?? {};
tags['event.origin'] = 'flutter';
tags['event.environment'] = 'dart';
event = event.copyWith(tags: tags);
}
} catch (exception, stackTrace) {
_options.logger(
SentryLevel.error,
'loadContextsIntegration failed',
exception: exception,
stackTrace: stackTrace,
);
}
return event;
}
}