diff --git a/CHANGELOG.md b/CHANGELOG.md index 714c12015..a3b0cf8fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixes +- On iOS and macOS, native crashes now respect the Event Sample Rate (`SampleRate`). Unity passes it to the Cocoa SDK with both runtime and build-time initialization, so native crashes are no longer always sent. ([#2940](https://github.com/getsentry/sentry-unity/pull/2940)) - Values set in a `SentryCliOptionsConfiguration`, such as an auth token read from an environment variable, no longer get saved into `SentryCliOptions.asset` by the Sentry window after a build. The configuration now runs on a copy of the options. ([#2938](https://github.com/getsentry/sentry-unity/pull/2938)) - The configuration window's tooltips and help boxes and the option docs describe the SDK's defaults and requirements correctly: `Auto Set UserName` is disabled by default, `Attach Screenshots` also attaches screenshots to events on background threads, `Org Slug` is required unless the Auth Token is an organization token, `Scene Traces` has no Unity version requirement, and `IosWatchdogTerminationIntegrationEnabled` turns on reporting of watchdog terminations on iOS. ([#2933](https://github.com/getsentry/sentry-unity/pull/2933)) - The SDK no longer throws a `NullReferenceException` during startup on Android and iOS when it can't load its options, for example when the configuration lives in an OBB file that isn't available yet. ([#2931](https://github.com/getsentry/sentry-unity/pull/2931)) diff --git a/src/Sentry.Unity.Editor.iOS/NativeOptions.cs b/src/Sentry.Unity.Editor.iOS/NativeOptions.cs index 8522eeb12..e40150977 100644 --- a/src/Sentry.Unity.Editor.iOS/NativeOptions.cs +++ b/src/Sentry.Unity.Editor.iOS/NativeOptions.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Globalization; using System.IO; namespace Sentry.Unity.Editor.iOS; @@ -33,6 +34,7 @@ internal static string Generate(SentryUnityOptions options) @""appHangTimeoutInterval"": @{options.AppHangTimeout.TotalSeconds.ToString(System.Globalization.CultureInfo.InvariantCulture)}, @""enableCaptureFailedRequests"": @{ToObjCString(options.CaptureFailedRequests)}, @""failedRequestStatusCodes"" : @[{failedRequestStatusCodesArray}], + {GetSampleRateEntry(options.SampleRate)} @""sendDefaultPii"" : @{ToObjCString(options.SendDefaultPii)}, @""attachScreenshot"" : @{ToObjCString(options.AttachScreenshot)}, @""release"" : @""{options.Release}"", @@ -60,6 +62,11 @@ internal static string Generate(SentryUnityOptions options) return nativeOptions; } + // SampleRate is null at a rate of 1.0. Leaving the key out keeps sentry-cocoa's default, which is also 1.0. + private static string GetSampleRateEntry(float? sampleRate) => sampleRate.HasValue + ? $"@\"sampleRate\" : @{sampleRate.Value.ToString(CultureInfo.InvariantCulture)}," + : string.Empty; + private static string ToObjCString(bool b) => b ? "YES" : "NO"; private static string ToNativeDiagnosticLevel(SentryLevel sentryLevel) diff --git a/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs b/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs index dced1a8ab..498f74966 100644 --- a/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs +++ b/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs @@ -49,6 +49,12 @@ public static bool Init(SentryUnityOptions options) Logger?.LogDebug("Setting DiagnosticLevel: {0}", diagnosticLevel); OptionsSetString(cOptions, "diagnosticLevel", diagnosticLevel); + if (options.SampleRate.HasValue) + { + Logger?.LogDebug("Setting SampleRate: {0}", options.SampleRate.Value); + OptionsSetDouble(cOptions, "sampleRate", options.SampleRate.Value); + } + Logger?.LogDebug("Setting SendDefaultPii: {0}", options.SendDefaultPii); OptionsSetInt(cOptions, "sendDefaultPii", options.SendDefaultPii ? 1 : 0); diff --git a/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs b/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs index fea49a8b1..aa55e5ff2 100644 --- a/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs +++ b/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.IO; using NUnit.Framework; @@ -110,4 +111,41 @@ public void CreateOptionsFile_FilterBadGatewayDisabled_DoesNotAddFiltering() var nativeOptions = File.ReadAllText(_testOptionsFilePath); StringAssert.DoesNotContain("event.request.url containsString:@\"operate-sdk-telemetry.unity3d.com\"", nativeOptions); } + + [Test] + public void CreateOptionsFile_SampleRateSet_WritesSampleRate() + { + NativeOptions.CreateFile(_testOptionsFilePath, new SentryUnityOptions { SampleRate = 0.3f }); + + var nativeOptions = File.ReadAllText(_testOptionsFilePath); + StringAssert.Contains("@\"sampleRate\" : @0.3,", nativeOptions); + } + + [Test] + public void CreateOptionsFile_SampleRateSetUnderCommaDecimalCulture_WritesInvariantSampleRate() + { + var originalCulture = CultureInfo.CurrentCulture; + try + { + CultureInfo.CurrentCulture = new CultureInfo("de-DE"); + + NativeOptions.CreateFile(_testOptionsFilePath, new SentryUnityOptions { SampleRate = 0.3f }); + } + finally + { + CultureInfo.CurrentCulture = originalCulture; + } + + var nativeOptions = File.ReadAllText(_testOptionsFilePath); + StringAssert.Contains("@\"sampleRate\" : @0.3,", nativeOptions); + } + + [Test] + public void CreateOptionsFile_SampleRateNotSet_LeavesSampleRateOut() + { + NativeOptions.CreateFile(_testOptionsFilePath, new SentryUnityOptions { SampleRate = null }); + + var nativeOptions = File.ReadAllText(_testOptionsFilePath); + StringAssert.DoesNotContain("sampleRate", nativeOptions); + } }