Repository navigation
Expand file tree
/
Copy pathScriptableSentryUnityOptions.cs
More file actions
384 lines (329 loc) · 19.3 KB
/
Copy pathScriptableSentryUnityOptions.cs
File metadata and controls
384 lines (329 loc) · 19.3 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using System;
using System.Collections.Generic;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;
using UnityEngine;
using UnityEngine.Serialization;
namespace Sentry.Unity;
public class ScriptableSentryUnityOptions : ScriptableObject
{
/// <summary>
/// Relative to Assets/Resources
/// </summary>
internal const string ConfigRootFolder = "Sentry";
/// <summary>
/// Main Sentry config name for Unity
/// </summary>
internal const string ConfigName = "SentryOptions";
/// <summary>
/// Path for the config for Unity
/// </summary>
public static string GetConfigPath(string? notDefaultConfigName = null)
=> $"Assets/Resources/{ConfigRootFolder}/{notDefaultConfigName ?? ConfigName}.asset";
[field: SerializeField] public bool Enabled { get; set; } = true;
[field: SerializeField] public string? Dsn { get; set; }
[field: SerializeField] public bool CaptureInEditor { get; set; } = true;
[field: SerializeField] public bool EnableErrorEventThrottling { get; set; } = false;
[field: SerializeField] public int ThrottleDedupeWindow { get; set; } = (int)TimeSpan.FromSeconds(1).TotalMilliseconds;
// Deprecated debouncing properties - kept for backwards compatibility
[field: SerializeField]
[Obsolete("Use EnableThrottling instead. This property will be removed in a future version.")]
public bool EnableLogDebouncing { get; set; } = false;
[field: SerializeField]
[Obsolete("Use EnableThrottling and ThrottleDedupeWindow instead. This property will be removed in a future version.")]
public int DebounceTimeLog { get; set; } = (int)TimeSpan.FromSeconds(1).TotalMilliseconds;
[field: SerializeField]
[Obsolete("Use EnableThrottling and ThrottleDedupeWindow instead. This property will be removed in a future version.")]
public int DebounceTimeWarning { get; set; } = (int)TimeSpan.FromSeconds(1).TotalMilliseconds;
[field: SerializeField]
[Obsolete("Use EnableThrottling and ThrottleDedupeWindow instead. This property will be removed in a future version.")]
public int DebounceTimeError { get; set; } = (int)TimeSpan.FromSeconds(1).TotalMilliseconds;
[field: SerializeField] public double TracesSampleRate { get; set; } = 0;
[field: SerializeField] public bool AutoStartupTraces { get; set; } = true;
[field: SerializeField] public bool AutoSceneLoadTraces { get; set; } = true;
[field: SerializeField] public bool AutoAwakeTraces { get; set; } = false;
[field: SerializeField] public bool AutoSessionTracking { get; set; } = true;
/// <summary>
/// Interval in milliseconds a session terminates if put in the background.
/// </summary>
[field: SerializeField] public int AutoSessionTrackingInterval { get; set; } = (int)TimeSpan.FromSeconds(30).TotalMilliseconds;
[field: SerializeField] public string ReleaseOverride { get; set; } = string.Empty;
[field: SerializeField] public string EnvironmentOverride { get; set; } = string.Empty;
[field: SerializeField] public bool AttachStacktrace { get; set; }
[field: SerializeField] public bool AttachScreenshot { get; set; }
[field: SerializeField] public ScreenshotQuality ScreenshotQuality { get; set; } = ScreenshotQuality.High;
[field: SerializeField] public int ScreenshotCompression { get; set; } = 75;
[field: SerializeField] public bool AttachViewHierarchy { get; set; } = false;
[field: SerializeField] public int MaxViewHierarchyRootObjects { get; set; } = 100;
[field: SerializeField] public int MaxViewHierarchyObjectChildCount { get; set; } = 20;
[field: SerializeField] public int MaxViewHierarchyDepth { get; set; } = 10;
[field: SerializeField] public bool AutoFrameMetrics { get; set; } = false;
[field: SerializeField]
public int FrameMetricsIntervalSeconds { get; set; } = 1;
[field: SerializeField] public bool AutoMemoryMetrics { get; set; } = false;
[field: SerializeField] public bool AutoGcMetrics { get; set; } = false;
[field: SerializeField] public bool AutoNetworkMetrics { get; set; } = false;
[field: SerializeField]
[field: FormerlySerializedAs("<MemoryMetricsSampleIntervalSeconds>k__BackingField")]
public int MemoryMetricsIntervalSeconds { get; set; } = 60;
[field: SerializeField]
[field: FormerlySerializedAs("<GcMetricsSampleIntervalSeconds>k__BackingField")]
public int GcMetricsIntervalSeconds { get; set; } = 60;
[field: SerializeField]
[field: FormerlySerializedAs("<NetworkMetricsSampleIntervalSeconds>k__BackingField")]
public int NetworkMetricsIntervalSeconds { get; set; } = 10;
[field: SerializeField] public bool EnableStructuredLogging { get; set; } = false;
[field: SerializeField] public bool StructuredLogOnDebugLog { get; set; } = false;
[field: SerializeField] public bool StructuredLogOnDebugLogWarning { get; set; } = true;
[field: SerializeField] public bool StructuredLogOnDebugLogAssertion { get; set; } = true;
[field: SerializeField] public bool StructuredLogOnDebugLogError { get; set; } = true;
[field: SerializeField] public bool StructuredLogOnDebugLogException { get; set; } = true;
[field: SerializeField] public bool AddBreadcrumbsWithStructuredLogs { get; set; } = false;
[field: SerializeField] public bool BreadcrumbsForLogs { get; set; } = true;
[field: SerializeField] public bool BreadcrumbsForWarnings { get; set; } = true;
[field: SerializeField] public bool BreadcrumbsForAsserts { get; set; } = true;
[field: SerializeField] public bool BreadcrumbsForErrors { get; set; } = true;
[field: SerializeField] public bool CaptureLogErrorEvents { get; set; } = true;
[field: SerializeField] public int MaxBreadcrumbs { get; set; } = SentryConstants.DefaultMaxBreadcrumbs;
[field: SerializeField] public ReportAssembliesMode ReportAssembliesMode { get; set; } = ReportAssembliesMode.Version;
[field: SerializeField] public bool SendDefaultPii { get; set; }
[field: SerializeField] public bool IsEnvironmentUser { get; set; }
[field: SerializeField] public bool EnableOfflineCaching { get; set; } = true;
[field: SerializeField] public int MaxCacheItems { get; set; } = 30;
/// <summary>
/// Time in milliseconds for flushing the cache at startup
/// </summary>
[field: SerializeField] public int InitCacheFlushTimeout { get; set; } = (int)TimeSpan.Zero.TotalMilliseconds;
[field: SerializeField] public float SampleRate { get; set; } = 1.0f;
[field: SerializeField] public int ShutdownTimeout { get; set; } = 2000;
[field: SerializeField] public int MaxQueueItems { get; set; } = 30;
[field: SerializeField]
[Obsolete("The C# ANR watchdog is deprecated. Use EnableAppHangTracking instead. This property will be removed in a future version.")]
public bool AnrDetectionEnabled { get; set; } = false;
[field: SerializeField]
[Obsolete("The C# ANR watchdog is deprecated. Use EnableAppHangTracking instead. This property will be removed in a future version.")]
public int AnrTimeout { get; set; } = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
[field: SerializeField] public bool EnableAppHangTracking { get; set; } = false;
[field: SerializeField] public int AppHangTimeout { get; set; } = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
[field: SerializeField] public bool CaptureFailedRequests { get; set; } = true;
// We hold the status codes as a list of ints to be able to serialize it in the editor.
[field: SerializeField] public List<int> FailedRequestStatusCodes { get; set; } = new() { 500, 599 };
[field: SerializeField] public bool FilterBadGatewayExceptions { get; set; } = true;
[field: SerializeField] public bool FilterWebExceptions { get; set; } = true;
[field: SerializeField] public bool FilterSocketExceptions { get; set; } = true;
[field: SerializeField] public bool IosNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool AndroidNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool NdkIntegrationEnabled { get; set; } = true;
[field: SerializeField] public bool NdkScopeSyncEnabled { get; set; } = true;
[field: SerializeField] public int PostGenerateGradleProjectCallbackOrder { get; set; } = 1;
[field: SerializeField] public bool WindowsNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool MacosNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool LinuxNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool XboxNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool PlayStationNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool SwitchNativeSupportEnabled { get; set; } = true;
[field: SerializeField] public bool Il2CppLineNumberSupportEnabled { get; set; } = true;
/// <summary>
/// Metrics are always sent to Sentry.
/// </summary>
/// <remarks>
/// This option no longer has any effect. The getter always returns <see langword="true"/> and the
/// setter is ignored, matching <see cref="SentryOptions.EnableMetrics"/> in the .NET SDK.
/// </remarks>
[Obsolete("Metrics are always enabled. This option is ignored and will be removed in a future version. Disable the individual Auto*Metrics options instead, or filter emitted metrics with SetBeforeSendMetric.")]
public bool EnableMetrics { get => true; set { } }
[field: SerializeField] public ExperimentalSentryUnityOptions Experimental { get; set; } = new();
[field: SerializeField] public SentryOptionsConfiguration? OptionsConfiguration { get; set; }
[field: SerializeField] public bool Debug { get; set; } = true;
[field: SerializeField] public bool DebugOnlyInEditor { get; set; } = true;
[field: SerializeField] public SentryLevel DiagnosticLevel { get; set; } = SentryLevel.Warning;
/// <summary>
/// Loads the ScriptableSentryUnityOptions from <c>Resource</c>.
/// </summary>
/// <returns>The <c>SentryUnityOptions</c> generated from the <c>ScriptableSentryUnityOptions</c></returns>
/// <remarks>
/// This gets called from <c>SentryInitialization</c> during the game's startup.
/// </remarks>
public static SentryUnityOptions? LoadSentryUnityOptions()
{
var scriptableOptions = Resources.Load<ScriptableSentryUnityOptions>($"{ConfigRootFolder}/{ConfigName}");
if (scriptableOptions is not null)
{
return scriptableOptions.ToSentryUnityOptions();
}
return null;
}
internal SentryUnityOptions ToSentryUnityOptions(
IApplication? application = null,
ISentryUnityInfo? unityInfo = null,
bool isBuilding = false)
{
application ??= ApplicationAdapter.Instance;
var options = new SentryUnityOptions(application, unityInfo: unityInfo, isBuilding: isBuilding)
{
Enabled = Enabled,
Dsn = Dsn,
CaptureInEditor = CaptureInEditor,
#pragma warning disable CS0618 // Type or member is obsolete
EnableLogDebouncing = EnableLogDebouncing,
DebounceTimeLog = TimeSpan.FromMilliseconds(DebounceTimeLog),
DebounceTimeWarning = TimeSpan.FromMilliseconds(DebounceTimeWarning),
DebounceTimeError = TimeSpan.FromMilliseconds(DebounceTimeError),
#pragma warning restore CS0618
TracesSampleRate = TracesSampleRate,
AutoStartupTraces = AutoStartupTraces,
AutoSceneLoadTraces = AutoSceneLoadTraces,
AutoSessionTracking = AutoSessionTracking,
AutoSessionTrackingInterval = TimeSpan.FromMilliseconds(AutoSessionTrackingInterval),
AttachStacktrace = AttachStacktrace,
AttachScreenshot = AttachScreenshot,
ScreenshotQuality = ScreenshotQuality,
ScreenshotCompression = ScreenshotCompression,
AttachViewHierarchy = AttachViewHierarchy,
MaxViewHierarchyRootObjects = MaxViewHierarchyRootObjects,
MaxViewHierarchyObjectChildCount = MaxViewHierarchyObjectChildCount,
MaxViewHierarchyDepth = MaxViewHierarchyDepth,
AutoFrameMetrics = AutoFrameMetrics,
FrameMetricsInterval = TimeSpan.FromSeconds(FrameMetricsIntervalSeconds),
AutoMemoryMetrics = AutoMemoryMetrics,
AutoGcMetrics = AutoGcMetrics,
AutoNetworkMetrics = AutoNetworkMetrics,
MemoryMetricsInterval = TimeSpan.FromSeconds(MemoryMetricsIntervalSeconds),
GcMetricsInterval = TimeSpan.FromSeconds(GcMetricsIntervalSeconds),
NetworkMetricsInterval = TimeSpan.FromSeconds(NetworkMetricsIntervalSeconds),
MaxBreadcrumbs = MaxBreadcrumbs,
ReportAssembliesMode = ReportAssembliesMode,
SendDefaultPii = SendDefaultPii,
IsEnvironmentUser = IsEnvironmentUser,
MaxCacheItems = MaxCacheItems,
InitCacheFlushTimeout = TimeSpan.FromMilliseconds(InitCacheFlushTimeout),
SampleRate = SampleRate == 1.0f ? null : SampleRate, // To skip the random check for dropping events
ShutdownTimeout = TimeSpan.FromMilliseconds(ShutdownTimeout),
MaxQueueItems = MaxQueueItems,
// Because SentryOptions.Debug is used inside the .NET SDK to setup the ConsoleLogger we
// need to set it here directly.
Debug = ShouldDebug(application.IsEditor && !isBuilding),
DiagnosticLevel = DiagnosticLevel,
CaptureLogErrorEvents = CaptureLogErrorEvents,
#pragma warning disable CS0618 // Type or member is obsolete
AnrTimeout = TimeSpan.FromMilliseconds(AnrTimeout),
#pragma warning restore CS0618
EnableAppHangTracking = EnableAppHangTracking,
AppHangTimeout = TimeSpan.FromMilliseconds(AppHangTimeout),
CaptureFailedRequests = CaptureFailedRequests,
FilterBadGatewayExceptions = FilterBadGatewayExceptions,
IosNativeSupportEnabled = IosNativeSupportEnabled,
AndroidNativeSupportEnabled = AndroidNativeSupportEnabled,
NdkIntegrationEnabled = NdkIntegrationEnabled,
NdkScopeSyncEnabled = NdkScopeSyncEnabled,
PostGenerateGradleProjectCallbackOrder = PostGenerateGradleProjectCallbackOrder,
WindowsNativeSupportEnabled = WindowsNativeSupportEnabled,
MacosNativeSupportEnabled = MacosNativeSupportEnabled,
LinuxNativeSupportEnabled = LinuxNativeSupportEnabled,
XboxNativeSupportEnabled = XboxNativeSupportEnabled,
PlayStationNativeSupportEnabled = PlayStationNativeSupportEnabled,
SwitchNativeSupportEnabled = SwitchNativeSupportEnabled,
Il2CppLineNumberSupportEnabled = Il2CppLineNumberSupportEnabled,
PerformanceAutoInstrumentationEnabled = AutoAwakeTraces,
EnableLogs = EnableStructuredLogging,
CaptureStructuredLogsForLogType =
{
[LogType.Log] = StructuredLogOnDebugLog,
[LogType.Warning] = StructuredLogOnDebugLogWarning,
[LogType.Assert] = StructuredLogOnDebugLogAssertion,
[LogType.Error] = StructuredLogOnDebugLogError,
[LogType.Exception] = StructuredLogOnDebugLogException
},
AddBreadcrumbsWithStructuredLogs = AddBreadcrumbsWithStructuredLogs
};
options.Experimental.MacosBackend = Experimental.MacosBackend;
options.Experimental.WindowsBackend = Experimental.WindowsBackend;
options.Experimental.LinuxBackend = Experimental.LinuxBackend;
#pragma warning disable CS0618 // Type or member is obsolete
options.Experimental.EnableNativeAppHangTracking = Experimental.EnableNativeAppHangTracking;
#pragma warning restore CS0618 // Type or member is obsolete
// By default, the cacheDirectoryPath gets set on known platforms. We're overwriting this behaviour here.
if (!EnableOfflineCaching)
{
options.CacheDirectoryPath = null;
}
if (!string.IsNullOrWhiteSpace(ReleaseOverride))
{
options.Release = ReleaseOverride;
}
if (!string.IsNullOrWhiteSpace(EnvironmentOverride))
{
options.Environment = EnvironmentOverride;
}
options.AddBreadcrumbsForLogType[LogType.Log] = BreadcrumbsForLogs;
options.AddBreadcrumbsForLogType[LogType.Warning] = BreadcrumbsForWarnings;
options.AddBreadcrumbsForLogType[LogType.Assert] = BreadcrumbsForAsserts;
options.AddBreadcrumbsForLogType[LogType.Error] = BreadcrumbsForErrors;
options.FailedRequestStatusCodes = new List<HttpStatusCodeRange>();
for (var i = 0; i < FailedRequestStatusCodes.Count; i += 2)
{
options.FailedRequestStatusCodes.Add(
new HttpStatusCodeRange(FailedRequestStatusCodes[i], FailedRequestStatusCodes[i + 1]));
}
#pragma warning disable CS0618 // Type or member is obsolete
if (AnrDetectionEnabled)
{
options.AddIntegration(new AnrIntegration(SentryMonoBehaviour.Instance));
}
#pragma warning restore CS0618
if (OptionsConfiguration != null)
{
options.LogDebug("OptionsConfiguration found. Calling configure.");
OptionsConfiguration.Configure(options);
}
// The macOS / Windows / Linux Native backends hand crash uploads to an out-of-process handler that gets
// killed when the player exits. Enforce a floor so the handler has time to flush.
if (options.Experimental.MacosBackend == MacosBackend.Native
|| options.Experimental.WindowsBackend == WindowsBackend.Native
|| options.Experimental.LinuxBackend == LinuxBackend.Native)
{
var minimum = TimeSpan.FromSeconds(10);
if (options.ShutdownTimeout < minimum)
{
options.LogInfo(
"Native backend: raising ShutdownTimeout from {0}ms to the {1}ms floor.",
(int)options.ShutdownTimeout.TotalMilliseconds,
(int)minimum.TotalMilliseconds);
options.ShutdownTimeout = minimum;
}
}
if (EnableErrorEventThrottling && options.Throttler is null)
{
options.Throttler = new ErrorEventThrottler(TimeSpan.FromMilliseconds(ThrottleDedupeWindow));
}
// We need to set up logging here because the configure callback might have changed the debug options.
// Without setting up here we might miss out on logs between option-loading (now) and Init - i.e. native configuration
options.SetupUnityLogging();
// ExceptionFilters are added by default to the options.
HandleExceptionFilter(options);
return options;
}
private void HandleExceptionFilter(SentryUnityOptions options)
{
if (!options.FilterBadGatewayExceptions)
{
options.RemoveExceptionFilter<UnityBadGatewayExceptionFilter>();
}
if (!FilterWebExceptions)
{
options.RemoveExceptionFilter<UnityWebExceptionFilter>();
}
if (!FilterSocketExceptions)
{
options.RemoveExceptionFilter<UnitySocketExceptionFilter>();
}
}
internal bool ShouldDebug(bool isEditorPlayer)
{
if (!isEditorPlayer)
{
return !DebugOnlyInEditor && Debug;
}
return Debug;
}
}