-
-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathSentrySdk.cs
More file actions
290 lines (248 loc) · 13.4 KB
/
SentrySdk.cs
File metadata and controls
290 lines (248 loc) · 13.4 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
using Sentry.Cocoa;
using Sentry.Cocoa.Extensions;
using Sentry.Extensibility;
using Sentry.Internal;
// ReSharper disable once CheckNamespace
namespace Sentry;
public static partial class SentrySdk
{
private static void InitSentryCocoaSdk(SentryOptions options)
{
options.LogDebug("Initializing native SDK");
// Set default release and distribution
options.Release ??= GetDefaultReleaseString();
options.Distribution ??= GetDefaultDistributionString();
// Set options for the Cocoa SDK
var nativeOptions = new SentryCocoaSdkOptions();
// These options are copied over from our SentryOptions
nativeOptions.AttachStacktrace = options.AttachStacktrace;
nativeOptions.Debug = options.Debug;
nativeOptions.DiagnosticLevel = options.DiagnosticLevel.ToCocoaSentryLevel();
nativeOptions.Dsn = options.Dsn;
// Hardcode to false because .NET SDK manages sessions itself
nativeOptions.EnableAutoSessionTracking = false;
nativeOptions.EnableCaptureFailedRequests = options.CaptureFailedRequests;
nativeOptions.FailedRequestStatusCodes = GetFailedRequestStatusCodes(options.FailedRequestStatusCodes);
nativeOptions.MaxAttachmentSize = (nuint)options.MaxAttachmentSize;
nativeOptions.MaxBreadcrumbs = (nuint)options.MaxBreadcrumbs;
nativeOptions.MaxCacheItems = (nuint)options.MaxCacheItems;
nativeOptions.ReleaseName = options.Release;
nativeOptions.SampleRate = options.SampleRate;
nativeOptions.SendClientReports = options.SendClientReports;
nativeOptions.SendDefaultPii = options.SendDefaultPii;
nativeOptions.SessionTrackingIntervalMillis = (nuint)options.AutoSessionTrackingInterval.TotalMilliseconds;
if (options.Environment is { } environment)
{
nativeOptions.Environment = environment;
}
// These options are not available in the Sentry Cocoa SDK
// nativeOptions.? = options.InitCacheFlushTimeout;
// nativeOptions.? = options.MaxQueueItems;
// nativeOptions.? = options.ShutdownTimeout;
// NOTE: options.CacheDirectoryPath - No option for this in Sentry Cocoa, but caching is still enabled
// https://github.com/getsentry/sentry-cocoa/issues/1051
// NOTE: Tags in options.DefaultTags should not be passed down, because we already call SetTag on each
// one when sending events, which is relayed through the scope observer.
if (options.BeforeBreadcrumbInternal is { } beforeBreadcrumb)
{
nativeOptions.BeforeBreadcrumb = b =>
{
// Note: The Cocoa SDK doesn't yet support hints.
// See https://github.com/getsentry/sentry-cocoa/issues/2325
var hint = new SentryHint();
var breadcrumb = b.ToBreadcrumb(options.DiagnosticLogger);
var result = beforeBreadcrumb(breadcrumb, hint)?.ToCocoaBreadcrumb();
// Note: Nullable result is allowed but delegate is generated incorrectly
// See https://github.com/xamarin/xamarin-macios/issues/15299#issuecomment-1201863294
return result!;
};
}
// These options we have behind feature flags
if (options is { IsPerformanceMonitoringEnabled: true, Native.EnableTracing: true })
{
nativeOptions.TracesSampleRate = options.TracesSampleRate;
if (options.TracesSampler is { } tracesSampler)
{
nativeOptions.TracesSampler = cocoaContext =>
{
var context = cocoaContext.ToTransactionSamplingContext();
var result = tracesSampler(context);
// Note: Nullable result is allowed but delegate is generated incorrectly
// See https://github.com/xamarin/xamarin-macios/issues/15299#issuecomment-1201863294
return result!;
};
}
}
nativeOptions.BeforeSend = evt => ProcessOnBeforeSend(options, evt)!;
if (options.OnCrashedLastRun is { } onCrashedLastRun)
{
nativeOptions.OnCrashedLastRun = evt =>
{
// because we delegate to user code, we need to protect anything that could happen in this event
try
{
var sentryEvent = evt.ToSentryEvent();
if (sentryEvent != null)
{
onCrashedLastRun(sentryEvent);
}
}
catch (Exception ex)
{
options.LogError(ex, "Crashed Last Run Error");
}
};
}
// These options are from Cocoa's SentryOptions
nativeOptions.AttachScreenshot = options.Native.AttachScreenshot;
nativeOptions.AppHangTimeoutInterval = options.Native.AppHangTimeoutInterval.TotalSeconds;
nativeOptions.IdleTimeout = options.Native.IdleTimeout.TotalSeconds;
nativeOptions.Dist = options.Distribution;
nativeOptions.EnableAppHangTracking = options.Native.EnableAppHangTracking;
nativeOptions.EnableAppHangTrackingV2 = options.Native.EnableAppHangTrackingV2;
nativeOptions.EnableAutoBreadcrumbTracking = options.Native.EnableAutoBreadcrumbTracking;
nativeOptions.EnableAutoPerformanceTracing = options.Native.EnableAutoPerformanceTracing;
nativeOptions.EnableCoreDataTracing = options.Native.EnableCoreDataTracing;
nativeOptions.EnableFileIOTracing = options.Native.EnableFileIOTracing;
nativeOptions.EnableNetworkBreadcrumbs = options.Native.EnableNetworkBreadcrumbs;
nativeOptions.EnableNetworkTracking = options.Native.EnableNetworkTracking;
#pragma warning disable CS0618 // Type or member is obsolete
nativeOptions.EnableWatchdogTerminationTracking = options.Native.EnableWatchdogTerminationTracking;
#pragma warning restore CS0618 // Type or member is obsolete
nativeOptions.EnableSwizzling = options.Native.EnableSwizzling;
nativeOptions.EnableUIViewControllerTracing = options.Native.EnableUIViewControllerTracing;
nativeOptions.EnableUserInteractionTracing = options.Native.EnableUserInteractionTracing;
nativeOptions.UrlSessionDelegate = options.Native.UrlSessionDelegate;
// StitchAsyncCode removed from Cocoa SDK in 8.6.0 with https://github.com/getsentry/sentry-cocoa/pull/2973
// nativeOptions.StitchAsyncCode = options.Native.StitchAsyncCode;
// In-App Excludes and Includes to be passed to the Cocoa SDK
options.Native.InAppExcludes?.ForEach(x => nativeOptions.AddInAppExclude(x));
options.Native.InAppIncludes?.ForEach(x => nativeOptions.AddInAppInclude(x));
// These options are intentionally not expose or modified
// nativeOptions.Enabled
// nativeOptions.SdkInfo
// nativeOptions.Integrations
// nativeOptions.DefaultIntegrations
// nativeOptions.EnableProfiling (deprecated)
// Set hybrid SDK name
SentryCocoaHybridSdk.SetSdkName("sentry.cocoa.dotnet");
// Now initialize the Cocoa SDK
SentryCocoaSdk.StartWithOptions(nativeOptions);
// Set options for the managed SDK that depend on the Cocoa SDK. (The user will not be able to modify these.)
options.AddEventProcessor(new CocoaEventProcessor());
options.CrashedLastRun = () => SentryCocoaSdk.CrashedLastRun;
options.EnableScopeSync = true;
options.ScopeObserver = new CocoaScopeObserver(options);
// Note: don't use AddProfilingIntegration as it would print a warning if user used it too.
if (!options.HasIntegration<ProfilingIntegration>())
{
options.AddIntegration(new ProfilingIntegration());
}
// TODO: Pause/Resume
}
private static string GetDefaultReleaseString()
{
var packageName = GetBundleValue("CFBundleIdentifier");
var packageVersion = GetBundleValue("CFBundleShortVersionString");
var buildVersion = GetBundleValue("CFBundleVersion");
return $"{packageName}@{packageVersion}+{buildVersion}";
}
private static string GetDefaultDistributionString() => GetBundleValue("CFBundleVersion");
private static string GetBundleValue(string key) => NSBundle.MainBundle.ObjectForInfoDictionary(key).ToString();
private static CocoaSdk.SentryHttpStatusCodeRange[] GetFailedRequestStatusCodes(IList<HttpStatusCodeRange> httpStatusCodeRanges)
{
var nativeRanges = new CocoaSdk.SentryHttpStatusCodeRange[httpStatusCodeRanges.Count];
for (var i = 0; i < httpStatusCodeRanges.Count; i++)
{
var range = httpStatusCodeRanges[i];
nativeRanges[i] = new CocoaSdk.SentryHttpStatusCodeRange(range.Start, range.End);
}
return nativeRanges;
}
[DebuggerStepThrough]
internal static CocoaSdk.SentryEvent? ProcessOnBeforeSend(SentryOptions options, CocoaSdk.SentryEvent evt)
=> ProcessOnBeforeSend(options, evt, CurrentHub);
/// <summary>
/// This overload allows us to inject an IHub for testing. During normal execution, the CurrentHub is used.
/// However, since this class is static, there's no easy alternative way to inject this when executing tests.
/// </summary>
internal static CocoaSdk.SentryEvent? ProcessOnBeforeSend(SentryOptions options, CocoaSdk.SentryEvent evt, IHub hub)
{
if (hub is DisabledHub)
{
return evt;
}
// When we have an unhandled managed exception, we send that to Sentry twice - once managed and once native.
// The managed exception is what a .NET developer would expect, and it is sent by the Sentry.NET SDK
// But we also get a native SIGABRT since it crashed the application, which is sent by the Sentry Cocoa SDK.
// There should only be one exception on the event in this case
if ((options.Native.SuppressSignalAborts || options.Native.SuppressExcBadAccess) && evt.Exceptions?.Length == 1)
{
// It will match the following characteristics
var ex = evt.Exceptions[0];
// Thankfully, sometimes we can see Xamarin's unhandled exception handler on the stack trace, so we can filter
// them out. Here is the function that calls abort(), which we will use as a filter:
// https://github.com/xamarin/xamarin-macios/blob/c55fbdfef95028ba03d0f7a35aebca03bd76f852/runtime/runtime.m#L1114-L1122
if (options.Native.SuppressSignalAborts && ex.Type == "SIGABRT" && ex.Value == "Signal 6, Code 0" &&
ex.Stacktrace?.Frames.Any(f => f.Function == "xamarin_unhandled_exception_handler") is true)
{
// Don't send it
options.LogDebug("Discarded {0} error ({1}). Captured as managed exception instead.", ex.Type,
ex.Value);
return null!;
}
// Similar workaround for NullReferenceExceptions. We don't have any easy way to know whether the
// exception is managed code (compiled to native) or original native code though.
// See: https://github.com/getsentry/sentry-dotnet/issues/3776
if (options.Native.SuppressExcBadAccess && ex.Type == "EXC_BAD_ACCESS")
{
// Don't send it
options.LogDebug("Discarded {0} error ({1}). Captured as managed exception instead.", ex.Type,
ex.Value);
return null!;
}
}
// We run our SIGABRT checks first before running managed processors.
// Because we delegate to user code, we need to catch/log exceptions.
try
{
// Normally the event processors would be invoked by the SentryClient, but the Cocoa SDK has its own client,
// so we need to manually invoke any managed event processors here to apply them to Native events.
var manualProcessors = GetEventProcessors(hub)
.Where(p => p is not MainSentryEventProcessor)
.ToArray();
if (manualProcessors.Length == 0 && options.BeforeSendInternal is null)
{
return evt;
}
var sentryEvent = evt.ToSentryEvent();
if (SentryEventHelper.ProcessEvent(sentryEvent, manualProcessors, null, options) is not { } processedEvent)
{
return null;
}
processedEvent = SentryEventHelper.DoBeforeSend(processedEvent, new SentryHint(), options);
if (processedEvent == null)
{
return null;
}
// we only support a subset of mutated data to be passed back to the native SDK at this time
processedEvent.CopyToCocoaSentryEvent(evt);
return evt;
}
catch (Exception ex)
{
options.LogError(ex, "Error running managed event processors for native event");
return evt;
}
static IEnumerable<ISentryEventProcessor> GetEventProcessors(IHub hub)
{
if (hub is Hub fullHub)
{
return fullHub.ScopeManager.GetCurrent().Key.GetAllEventProcessors();
}
IEnumerable<ISentryEventProcessor>? eventProcessors = null;
hub.ConfigureScope(scope => eventProcessors = scope.GetAllEventProcessors());
return eventProcessors ?? [];
}
}
}