From 4eb404b1ede00cbfa0ca24456a016b0d2a43662a Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 30 Oct 2024 16:28:46 +0100 Subject: [PATCH] fixed writing float as float to manifest --- .../Android/AndroidManifestConfiguration.cs | 7 +++++-- .../Android/AndroidManifestConfigurationTests.cs | 11 ++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs b/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs index d49fdf33f..c461662d4 100644 --- a/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs +++ b/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Text; using System.Xml; @@ -157,7 +158,8 @@ internal void ModifyManifest(string basePath) if (_options.SampleRate.HasValue) { - _logger.LogDebug("Setting SampleRate: {0}", _options.SampleRate); + // To keep the logs in line with what the SDK writes to the AndroidManifest we're formatting here too + _logger.LogDebug("Setting SampleRate: {0}", ((float)_options.SampleRate).ToString("F", CultureInfo.InvariantCulture)); androidManifest.SetSampleRate(_options.SampleRate.Value); } @@ -420,7 +422,8 @@ public void AddDisclaimerComment() => internal void SetDsn(string dsn) => SetMetaData($"{SentryPrefix}.dsn", dsn); internal void SetSampleRate(float sampleRate) => - SetMetaData($"{SentryPrefix}.sample-rate", sampleRate.ToString()); + // Keeping the sample-rate as float: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings + SetMetaData($"{SentryPrefix}.sample-rate", sampleRate.ToString("F", CultureInfo.InvariantCulture)); internal void SetRelease(string release) => SetMetaData($"{SentryPrefix}.release", release); diff --git a/test/Sentry.Unity.Editor.Tests/Android/AndroidManifestConfigurationTests.cs b/test/Sentry.Unity.Editor.Tests/Android/AndroidManifestConfigurationTests.cs index 29c98fa12..5d99159e2 100644 --- a/test/Sentry.Unity.Editor.Tests/Android/AndroidManifestConfigurationTests.cs +++ b/test/Sentry.Unity.Editor.Tests/Android/AndroidManifestConfigurationTests.cs @@ -223,17 +223,18 @@ public void ModifyManifest_DiagnosticLevel_TestCases( } [Test] - public void ModifyManifest_SampleRate_SetIfNotNull() + [TestCase(0.45f, "0.45")] + [TestCase(1, "1.00")] + public void ModifyManifest_SampleRate_SetIfNotNull(float sampleRate, string expectedSampleRate) { - const float expected = 0.6f; - _fixture.SentryUnityOptions!.SampleRate = expected; + _fixture.SentryUnityOptions!.SampleRate = sampleRate; var sut = _fixture.GetSut(); var manifest = WithAndroidManifest(basePath => sut.ModifyManifest(basePath)); - _fixture.UnityTestLogger.AssertLogContains(SentryLevel.Debug, $"Setting SampleRate: {expected}"); + _fixture.UnityTestLogger.AssertLogContains(SentryLevel.Debug, $"Setting SampleRate: {expectedSampleRate}"); Assert.True(manifest.Contains( - $""), + $""), $"Expected 'io.sentry.sample-rate' in Manifest:\n{manifest}"); }