-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathSentrySdk.cs
223 lines (190 loc) · 10.7 KB
/
SentrySdk.cs
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
using Sentry.Cocoa;
using Sentry.Cocoa.Extensions;
using Sentry.Extensibility;
// ReSharper disable once CheckNamespace
namespace Sentry;
public static partial class SentrySdk
{
private static void InitSentryCocoaSdk(SentryOptions options)
{
options.LogDebug("Initializing native SDK");
// Workaround for https://github.com/xamarin/xamarin-macios/issues/15252
ObjCRuntime.Runtime.MarshalManagedException += (_, args) =>
{
args.ExceptionMode = ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode;
};
// 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;
nativeOptions.EnableAutoSessionTracking = options.AutoSessionTracking;
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!;
};
}
}
// TODO: Finish SentryEventExtensions to enable these
// if (options.Native.EnableBeforeSend && options.BeforeSend is { } beforeSend)
// {
// nativeOptions.BeforeSend = evt =>
// {
// var sentryEvent = evt.ToSentryEvent(nativeOptions);
// var result = beforeSend(sentryEvent)?.ToCocoaSentryEvent(options, nativeOptions);
//
// // Note: Nullable result is allowed but delegate is generated incorrectly
// // See https://github.com/xamarin/xamarin-macios/issues/15299#issuecomment-1201863294
// return result!;
// };
// }
// if (options.Native.OnCrashedLastRun is { } onCrashedLastRun)
// {
// nativeOptions.OnCrashedLastRun = evt =>
// {
// var sentryEvent = evt.ToSentryEvent(nativeOptions);
// onCrashedLastRun(sentryEvent);
// };
// }
// 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)
// 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.
// This is partially due to our setting ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode above.
// Thankfully, 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
nativeOptions.BeforeSend = evt =>
{
// There should only be one exception on the event in this case
if (evt.Exceptions?.Length == 1)
{
// It will match the following characteristics
var ex = evt.Exceptions[0];
if (ex.Type == "SIGABRT" && ex.Value == "Signal 6, Code 0" &&
ex.Stacktrace?.Frames.Any(f => f.Function == "xamarin_unhandled_exception_handler") is true)
{
// Don't sent it
return null!;
}
}
// Other event, send as normal
return evt;
};
// 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;
}
}