Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Write SampleRate as float to manifest #1872

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Fixed an issue where the SDK would write the `SampleRate` as an `int` instead of a `float` to the Android Manifest, causing issues during the Android SDK's initialization ([#1872](https://github.com/getsentry/sentry-unity/pull/1872))
- The SDK no longer calls into `Application.persistentDataPath` on unknown platforms. This prevents crashes during startup on platforms with restricted disk access like the Nintendo Switch ([#1870](https://github.com/getsentry/sentry-unity/pull/1870))

### Dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
$"<meta-data android:name=\"io.sentry.sample-rate\" android:value=\"{expected}\" />"),
$"<meta-data android:name=\"io.sentry.sample-rate\" android:value=\"{expectedSampleRate}\" />"),
$"Expected 'io.sentry.sample-rate' in Manifest:\n{manifest}");
}

Expand Down
Loading