Skip to content

Commit 4e947d8

Browse files
Fix EnableTracing option conflict with TracesSampleRate (#2368)
* Fix tracing options * Update tests * Update API snapshots * Fix unrelated solution filter * Update CHANGELOG.md
1 parent a8c8ea5 commit 4e947d8

11 files changed

+157
-34
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
- Add tag filters to `SentryOptions` ([#2367](https://github.com/getsentry/sentry-dotnet/pull/2367))
1111

12+
### Fixes
13+
14+
- Fix `EnableTracing` option conflict with `TracesSampleRate` ([#2368](https://github.com/getsentry/sentry-dotnet/pull/2368))
15+
- NOTE: This is a potentially breaking change, as the `TracesSampleRate` property has been made nullable.
16+
Though extremely uncommon, if you are _retrieving_ the `TracesSampleRate` property for some reason, you will need to account for nulls.
17+
However, there is no change to the behavior or _typical_ usage of either of these properties.
18+
1219
### Dependencies
1320

1421
- Bump Cocoa SDK from v8.6.0 to v8.7.0 ([#2359](https://github.com/getsentry/sentry-dotnet/pull/2359))

SentryMobile.slnf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"test\\Sentry.Extensions.Logging.Tests\\Sentry.Extensions.Logging.Tests.csproj",
1818
"test\\Sentry.Maui.Device.TestApp\\Sentry.Maui.Device.TestApp.csproj",
1919
"test\\Sentry.Maui.Tests\\Sentry.Maui.Tests.csproj",
20-
"test\\Sentry.Testing.CrashableApp\\Sentry.Testing.CrashableApp.csproj",
2120
"test\\Sentry.Testing\\Sentry.Testing.csproj",
2221
"test\\Sentry.Tests\\Sentry.Tests.csproj"
2322
]

src/Sentry/Internal/Hub.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ internal ITransaction StartTransaction(
148148
// Random sampling runs only if the sampling decision hasn't been made already.
149149
if (transaction.IsSampled == null)
150150
{
151-
var sampleRate = _options.TracesSampleRate;
151+
var sampleRate = _options.TracesSampleRate ?? (_options.EnableTracing is true ? 1.0 : 0.0);
152152
transaction.IsSampled = _randomValuesFactory.NextBool(sampleRate);
153153
transaction.SampleRate = sampleRate;
154154
}

src/Sentry/Platforms/Android/SentrySdk.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Sentry.Android.Callbacks;
55
using Sentry.Android.Extensions;
66
using Sentry.JavaSdk.Android.Core;
7-
using Sentry.Protocol;
87

98
// ReSharper disable once CheckNamespace
109
namespace Sentry;
@@ -108,8 +107,9 @@ private static void InitSentryAndroidSdk(SentryOptions options)
108107
}
109108

110109
// These options we have behind feature flags
111-
if (options.IsTracingEnabled && options.Android.EnableAndroidSdkTracing)
110+
if (options is {IsTracingEnabled: true, Android.EnableAndroidSdkTracing: true})
112111
{
112+
o.EnableTracing = (JavaBoolean?)options.EnableTracing;
113113
o.TracesSampleRate = (JavaDouble?)options.TracesSampleRate;
114114

115115
if (options.TracesSampler is { } tracesSampler)

src/Sentry/Platforms/iOS/SentrySdk.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ private static void InitSentryCocoaSdk(SentryOptions options)
6666
}
6767

6868
// These options we have behind feature flags
69-
if (options.IsTracingEnabled && options.iOS.EnableCocoaSdkTracing)
69+
if (options is {IsTracingEnabled: true, iOS.EnableCocoaSdkTracing: true})
7070
{
71+
if (options.EnableTracing != null)
72+
{
73+
cocoaOptions.EnableTracing = options.EnableTracing.Value;
74+
}
75+
7176
cocoaOptions.TracesSampleRate = options.TracesSampleRate;
7277

7378
if (options.TracesSampler is { } tracesSampler)

src/Sentry/SentryOptions.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,12 @@ public IList<SubstringOrRegexPattern> FailedRequestTargets {
585585
/// Indicates whether tracing is enabled, via any combination of
586586
/// <see cref="EnableTracing"/>, <see cref="TracesSampleRate"/>, or <see cref="TracesSampler"/>.
587587
/// </summary>
588-
internal bool IsTracingEnabled => EnableTracing ?? (_tracesSampleRate > 0.0 || TracesSampler is not null);
588+
internal bool IsTracingEnabled => EnableTracing switch
589+
{
590+
false => false,
591+
null => TracesSampler is not null || TracesSampleRate is > 0.0,
592+
true => TracesSampler is not null || TracesSampleRate is > 0.0 or null
593+
};
589594

590595
/// <summary>
591596
/// Simplified option for enabling or disabling tracing.
@@ -622,25 +627,43 @@ public IList<SubstringOrRegexPattern> FailedRequestTargets {
622627

623628
/// <summary>
624629
/// Indicates the percentage of the tracing data that is collected.
625-
/// Setting this to <c>0.0</c> discards all trace data.
626-
/// Setting this to <c>1.0</c> collects all trace data.
627-
/// Values outside of this range are invalid.
628-
/// The default value is either <c>0.0</c> or <c>1.0</c>, depending on the <see cref="EnableTracing"/> property.
630+
/// <list type="table">
631+
/// <listheader>
632+
/// <term>Value</term>
633+
/// <description>Effect</description>
634+
/// </listheader>
635+
/// <item>
636+
/// <term><c>&gt;= 0.0 and &lt;=1.0</c></term>
637+
/// <description>
638+
/// A custom sample rate is used unless <see cref="EnableTracing"/> is <c>false</c>,
639+
/// or unless overriden by a <see cref="TracesSampler"/> function.
640+
/// Values outside of this range are invalid.
641+
/// </description>
642+
/// </item>
643+
/// <item>
644+
/// <term><c>null</c></term>
645+
/// <description>
646+
/// <b>The default setting.</b>
647+
/// The tracing sample rate is determined by the <see cref="EnableTracing"/> property,
648+
/// unless overriden by a <see cref="TracesSampler"/> function.
649+
/// </description>
650+
/// </item>
651+
/// </list>
629652
/// </summary>
630653
/// <remarks>
631654
/// Random sampling rate is only applied to transactions that don't already
632655
/// have a sampling decision set by other means, such as through <see cref="TracesSampler"/>,
633656
/// by inheriting it from an incoming trace header, or by copying it from <see cref="TransactionContext"/>.
634657
/// </remarks>
635-
public double TracesSampleRate
658+
public double? TracesSampleRate
636659
{
637-
get => _tracesSampleRate ?? (EnableTracing is true ? 1.0 : 0.0);
660+
get => _tracesSampleRate;
638661
set
639662
{
640663
if (value is < 0.0 or > 1.0)
641664
{
642-
throw new InvalidOperationException(
643-
$"The value {value} is not a valid tracing sample rate. Use values between 0.0 and 1.0.");
665+
throw new ArgumentOutOfRangeException(nameof(value), value,
666+
"The traces sample rate must be between 0.0 and 1.0, inclusive.");
644667
}
645668

646669
_tracesSampleRate = value;

test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ namespace Sentry
617617
public Sentry.StackTraceMode StackTraceMode { get; set; }
618618
public System.Collections.Generic.ICollection<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
619619
public System.Collections.Generic.IList<Sentry.TracePropagationTarget> TracePropagationTargets { get; set; }
620-
public double TracesSampleRate { get; set; }
620+
public double? TracesSampleRate { get; set; }
621621
public System.Func<Sentry.TransactionSamplingContext, double?>? TracesSampler { get; set; }
622622
public Sentry.Extensibility.ITransport? Transport { get; set; }
623623
public bool UseAsyncFileIO { get; set; }

test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ namespace Sentry
618618
public Sentry.StackTraceMode StackTraceMode { get; set; }
619619
public System.Collections.Generic.ICollection<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
620620
public System.Collections.Generic.IList<Sentry.TracePropagationTarget> TracePropagationTargets { get; set; }
621-
public double TracesSampleRate { get; set; }
621+
public double? TracesSampleRate { get; set; }
622622
public System.Func<Sentry.TransactionSamplingContext, double?>? TracesSampler { get; set; }
623623
public Sentry.Extensibility.ITransport? Transport { get; set; }
624624
public bool UseAsyncFileIO { get; set; }

test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ namespace Sentry
618618
public Sentry.StackTraceMode StackTraceMode { get; set; }
619619
public System.Collections.Generic.ICollection<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
620620
public System.Collections.Generic.IList<Sentry.TracePropagationTarget> TracePropagationTargets { get; set; }
621-
public double TracesSampleRate { get; set; }
621+
public double? TracesSampleRate { get; set; }
622622
public System.Func<Sentry.TransactionSamplingContext, double?>? TracesSampler { get; set; }
623623
public Sentry.Extensibility.ITransport? Transport { get; set; }
624624
public bool UseAsyncFileIO { get; set; }

test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ namespace Sentry
616616
public Sentry.StackTraceMode StackTraceMode { get; set; }
617617
public System.Collections.Generic.ICollection<Sentry.SubstringOrRegexPattern> TagFilters { get; set; }
618618
public System.Collections.Generic.IList<Sentry.TracePropagationTarget> TracePropagationTargets { get; set; }
619-
public double TracesSampleRate { get; set; }
619+
public double? TracesSampleRate { get; set; }
620620
public System.Func<Sentry.TransactionSamplingContext, double?>? TracesSampler { get; set; }
621621
public Sentry.Extensibility.ITransport? Transport { get; set; }
622622
public bool UseAsyncFileIO { get; set; }

0 commit comments

Comments
 (0)