diff --git a/CHANGELOG.md b/CHANGELOG.md index b597d27a97..f7621e46cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Added basic functionality to support `View Hierarchy` ([#2163](https://github.com/getsentry/sentry-dotnet/pull/2163)) - Allow `SentryUploadSources` to work even when not uploading symbols ([#2197](https://github.com/getsentry/sentry-dotnet/pull/2197)) - Add support for `BeforeSendTransaction` ([#2188](https://github.com/getsentry/sentry-dotnet/pull/2188)) +- Add `EnableTracing` option to simplify enabling tracing ([#2201](https://github.com/getsentry/sentry-dotnet/pull/2201)) ### Fixes diff --git a/SentryAspNetCore.slnf b/SentryAspNetCore.slnf index 4db1f91529..a4e25e52ea 100644 --- a/SentryAspNetCore.slnf +++ b/SentryAspNetCore.slnf @@ -2,6 +2,7 @@ "solution": { "path": "Sentry.sln", "projects": [ + "samples\\Sentry.Samples.AspNetCore.Basic\\Sentry.Samples.AspNetCore.Basic.csproj", "src\\Sentry.AspNetCore.Grpc\\Sentry.AspNetCore.Grpc.csproj", "src\\Sentry.AspNetCore\\Sentry.AspNetCore.csproj", "src\\Sentry.Extensions.Logging\\Sentry.Extensions.Logging.csproj", diff --git a/samples/Sentry.Samples.AspNetCore.Basic/Program.cs b/samples/Sentry.Samples.AspNetCore.Basic/Program.cs index 1a0cf9c2e0..91f83b27fc 100644 --- a/samples/Sentry.Samples.AspNetCore.Basic/Program.cs +++ b/samples/Sentry.Samples.AspNetCore.Basic/Program.cs @@ -12,24 +12,30 @@ public static void Main(string[] args) public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) - // Add Sentry integration - // In this example, DSN and Release are set via environment variable: - // See: Properties/launchSettings.json - .UseSentry() - // It can also be defined via configuration (including appsettings.json) - // or coded explicitly, via parameter like: - // .UseSentry("dsn") or .UseSentry(o => o.Dsn = ""; o.Release = "1.0"; ...) + .UseSentry(o => + { + // A DSN is required. You can set it here, or in configuration, or in an environment variable. + o.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537"; + + // Enable Sentry performance monitoring + o.EnableTracing = true; + +#if DEBUG + // Log debug information about the Sentry SDK + o.Debug = true; +#endif + }) // The App: .Configure(app => { - // An example ASP.NET Core middleware that throws an - // exception when serving a request to path: /throw app.UseRouting(); // Enable Sentry performance monitoring app.UseSentryTracing(); + // An example ASP.NET Core middleware that throws an + // exception when serving a request to path: /throw app.UseEndpoints(endpoints => { // Reported events will be grouped by route pattern diff --git a/samples/Sentry.Samples.AspNetCore.Basic/Properties/launchSettings.json b/samples/Sentry.Samples.AspNetCore.Basic/Properties/launchSettings.json index 9271f0717f..2894e1139b 100644 --- a/samples/Sentry.Samples.AspNetCore.Basic/Properties/launchSettings.json +++ b/samples/Sentry.Samples.AspNetCore.Basic/Properties/launchSettings.json @@ -13,8 +13,6 @@ "launchBrowser": true, "launchUrl": "throw", "environmentVariables": { - "SENTRY_DSN": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537", - "SENTRY_RELEASE": "e386dfd", "ASPNETCORE_ENVIRONMENT": "Development" } }, @@ -23,8 +21,6 @@ "launchBrowser": true, "launchUrl": "throw", "environmentVariables": { - "SENTRY_DSN": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537", - "SENTRY_RELEASE": "e386dfd", "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "http://localhost:59740" diff --git a/samples/Sentry.Samples.AspNetCore.Blazor.Server/Program.cs b/samples/Sentry.Samples.AspNetCore.Blazor.Server/Program.cs index 1fde924814..53e574bc2b 100644 --- a/samples/Sentry.Samples.AspNetCore.Blazor.Server/Program.cs +++ b/samples/Sentry.Samples.AspNetCore.Blazor.Server/Program.cs @@ -7,6 +7,7 @@ builder.WebHost.UseSentry(options => { options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537"; + options.EnableTracing = true; options.Debug = true; }); diff --git a/samples/Sentry.Samples.AspNetCore.Grpc/Program.cs b/samples/Sentry.Samples.AspNetCore.Grpc/Program.cs index a5a0223373..b1d9183cb6 100644 --- a/samples/Sentry.Samples.AspNetCore.Grpc/Program.cs +++ b/samples/Sentry.Samples.AspNetCore.Grpc/Program.cs @@ -41,6 +41,8 @@ public static IWebHost BuildWebHost(string[] args) => options.Release = "e386dfd"; // Could also be any format, such as: 2.0, or however version of your app is + options.EnableTracing = true; + options.MaxBreadcrumbs = 200; // Set a proxy for outgoing HTTP connections diff --git a/samples/Sentry.Samples.AspNetCore.Grpc/Startup.cs b/samples/Sentry.Samples.AspNetCore.Grpc/Startup.cs index 5bc4580274..353f597ee5 100644 --- a/samples/Sentry.Samples.AspNetCore.Grpc/Startup.cs +++ b/samples/Sentry.Samples.AspNetCore.Grpc/Startup.cs @@ -19,6 +19,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) } app.UseRouting(); + app.UseSentryTracing(); app.UseEndpoints(endpoints => { diff --git a/samples/Sentry.Samples.AspNetCore.Mvc/Program.cs b/samples/Sentry.Samples.AspNetCore.Mvc/Program.cs index ce7e31db86..366affbb7e 100644 --- a/samples/Sentry.Samples.AspNetCore.Mvc/Program.cs +++ b/samples/Sentry.Samples.AspNetCore.Mvc/Program.cs @@ -29,7 +29,7 @@ public static IWebHost BuildWebHost(string[] args) => options.MaxBreadcrumbs = 200; - options.TracesSampleRate = 1.0; + options.EnableTracing = true; // Set a proxy for outgoing HTTP connections options.HttpProxy = null; // new WebProxy("https://localhost:3128"); diff --git a/samples/Sentry.Samples.AspNetCore.Serilog/appsettings.json b/samples/Sentry.Samples.AspNetCore.Serilog/appsettings.json index eccce94abd..376c0e7665 100644 --- a/samples/Sentry.Samples.AspNetCore.Serilog/appsettings.json +++ b/samples/Sentry.Samples.AspNetCore.Serilog/appsettings.json @@ -4,6 +4,8 @@ "Sentry": { // The DSN can also be set via environment variable "Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537", + // Enable Sentry tracing features + "EnableTracing": true, // Opt-in for payload submission "MaxRequestBodySize": "Always", // Sends Cookies, User Id when one is logged on and user IP address to sentry. It's turned off by default. diff --git a/samples/Sentry.Samples.Aws.Lambda.AspNetCoreServer/appsettings.json b/samples/Sentry.Samples.Aws.Lambda.AspNetCoreServer/appsettings.json index e5f47d22f4..3dc9e1058b 100644 --- a/samples/Sentry.Samples.Aws.Lambda.AspNetCoreServer/appsettings.json +++ b/samples/Sentry.Samples.Aws.Lambda.AspNetCoreServer/appsettings.json @@ -10,6 +10,7 @@ "Sentry": { "NOTE1": "Add your own DSN below see the sample event in YOUR Sentry dashboard.", "Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537", + "EnableTracing": true, "NOTE2": "Many Sentry ASP.NET Core options are demonstrated in the other samples in this repository.", "NOTE3": "The most relevant one for serverless is to tell Sentry to flush out events after each lambda request.", diff --git a/samples/Sentry.Samples.EntityFramework/Program.cs b/samples/Sentry.Samples.EntityFramework/Program.cs index a28c1a4ffc..8fcc2ac879 100644 --- a/samples/Sentry.Samples.EntityFramework/Program.cs +++ b/samples/Sentry.Samples.EntityFramework/Program.cs @@ -7,7 +7,8 @@ { o.Debug = true; // To see SDK logs on the console o.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537"; - o.TracesSampleRate = 1; + o.EnableTracing = true; + // Add the EntityFramework integration to the SentryOptions of your app startup code: o.AddEntityFramework(); }); diff --git a/samples/Sentry.Samples.Google.Cloud.Functions/appsettings.json b/samples/Sentry.Samples.Google.Cloud.Functions/appsettings.json index a76e7ff6dd..de1f1b3b6a 100644 --- a/samples/Sentry.Samples.Google.Cloud.Functions/appsettings.json +++ b/samples/Sentry.Samples.Google.Cloud.Functions/appsettings.json @@ -3,6 +3,6 @@ "Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537", "MaxRequestBodySize": "Always", "SendDefaultPii": true, - "TracesSampleRate": 1 + "EnableTracing": true } } diff --git a/src/Sentry.DiagnosticSource/Internal/DiagnosticSource/SentryDiagnosticListenerIntegration.cs b/src/Sentry.DiagnosticSource/Internal/DiagnosticSource/SentryDiagnosticListenerIntegration.cs index 743e99cc7d..0c36ae501f 100644 --- a/src/Sentry.DiagnosticSource/Internal/DiagnosticSource/SentryDiagnosticListenerIntegration.cs +++ b/src/Sentry.DiagnosticSource/Internal/DiagnosticSource/SentryDiagnosticListenerIntegration.cs @@ -7,9 +7,9 @@ internal class SentryDiagnosticListenerIntegration : ISdkIntegration { public void Register(IHub hub, SentryOptions options) { - if (options.TracesSampleRate == 0 && options.TracesSampler == null) + if (!options.IsTracingEnabled) { - options.Log(SentryLevel.Info, "DiagnosticSource Integration is now disabled due to TracesSampleRate being set to zero, and no TracesSampler set."); + options.Log(SentryLevel.Info, "DiagnosticSource Integration is disabled because tracing is disabled."); options.DisableDiagnosticSourceIntegration(); return; } diff --git a/src/Sentry.EntityFramework/DbInterceptionIntegration.cs b/src/Sentry.EntityFramework/DbInterceptionIntegration.cs index 7d3d098b2c..c9a676f433 100644 --- a/src/Sentry.EntityFramework/DbInterceptionIntegration.cs +++ b/src/Sentry.EntityFramework/DbInterceptionIntegration.cs @@ -9,7 +9,7 @@ internal class DbInterceptionIntegration : ISdkIntegration public void Register(IHub hub, SentryOptions options) { - if (options.TracesSampleRate == 0) + if (!options.IsTracingEnabled) { options.DiagnosticLogger?.LogInfo(SampleRateDisabledMessage); } diff --git a/src/Sentry.EntityFramework/ErrorProcessors/DbConcurrencyExceptionProcessor.cs b/src/Sentry.EntityFramework/ErrorProcessors/DbConcurrencyExceptionProcessor.cs index 3afec998c1..0269d2e989 100644 --- a/src/Sentry.EntityFramework/ErrorProcessors/DbConcurrencyExceptionProcessor.cs +++ b/src/Sentry.EntityFramework/ErrorProcessors/DbConcurrencyExceptionProcessor.cs @@ -8,7 +8,7 @@ public class DbConcurrencyExceptionProcessor : SentryEventExceptionProcessor /// Extracts RowCount and RowError from . /// - protected override void ProcessException(DBConcurrencyException exception, SentryEvent sentryEvent) + protected internal override void ProcessException(DBConcurrencyException exception, SentryEvent sentryEvent) { sentryEvent.SetExtra("Row Count", exception.RowCount); sentryEvent.SetExtra("Row Error", exception.Row.RowError); diff --git a/src/Sentry.EntityFramework/ErrorProcessors/DbEntityValidationExceptionProcessor.cs b/src/Sentry.EntityFramework/ErrorProcessors/DbEntityValidationExceptionProcessor.cs index 630b729e03..3979220318 100644 --- a/src/Sentry.EntityFramework/ErrorProcessors/DbEntityValidationExceptionProcessor.cs +++ b/src/Sentry.EntityFramework/ErrorProcessors/DbEntityValidationExceptionProcessor.cs @@ -8,7 +8,7 @@ public class DbEntityValidationExceptionProcessor : SentryEventExceptionProcesso /// /// Extracts details from into the . /// - protected override void ProcessException(DbEntityValidationException exception, SentryEvent sentryEvent) + protected internal override void ProcessException(DbEntityValidationException exception, SentryEvent sentryEvent) { var errorList = new Dictionary>(); foreach (var error in exception.EntityValidationErrors.SelectMany(x => x.ValidationErrors)) diff --git a/src/Sentry/Internal/Hub.cs b/src/Sentry/Internal/Hub.cs index 727ca2af23..5fa210e3cd 100644 --- a/src/Sentry/Internal/Hub.cs +++ b/src/Sentry/Internal/Hub.cs @@ -123,28 +123,40 @@ internal ITransaction StartTransaction( { var transaction = new TransactionTracer(this, context); - // Tracing sampler callback runs regardless of whether a decision - // has already been made, as it can be used to override it. - if (_options.TracesSampler is { } tracesSampler) + // If tracing is explicitly disabled, we will always sample out. + // Do not invoke the TracesSampler, evaluate the TracesSampleRate, and override any sampling decision + // that may have been already set (i.e.: from a sentry-trace header). + if (_options.EnableTracing is false) { - var samplingContext = new TransactionSamplingContext( - context, - customSamplingContext); + transaction.IsSampled = false; + transaction.SampleRate = 0.0; + } + else + { + // Except when tracing is disabled, TracesSampler runs regardless of whether a decision + // has already been made, as it can be used to override it. + if (_options.TracesSampler is { } tracesSampler) + { + var samplingContext = new TransactionSamplingContext( + context, + customSamplingContext); + + if (tracesSampler(samplingContext) is { } sampleRate) + { + transaction.IsSampled = _randomValuesFactory.NextBool(sampleRate); + transaction.SampleRate = sampleRate; + } + } - if (tracesSampler(samplingContext) is { } sampleRate) + // Random sampling runs only if the sampling decision hasn't been made already. + if (transaction.IsSampled == null) { + var sampleRate = _options.TracesSampleRate; transaction.IsSampled = _randomValuesFactory.NextBool(sampleRate); transaction.SampleRate = sampleRate; } } - // Random sampling runs only if the sampling decision hasn't been made already. - if (transaction.IsSampled == null) - { - transaction.IsSampled = _randomValuesFactory.NextBool(_options.TracesSampleRate); - transaction.SampleRate = _options.TracesSampleRate; - } - // Use the provided DSC, or create one based on this transaction. // This must be done AFTER the sampling decision has been made. transaction.DynamicSamplingContext = diff --git a/src/Sentry/Platforms/Android/SentrySdk.cs b/src/Sentry/Platforms/Android/SentrySdk.cs index 65df9b106a..e4e5a35b92 100644 --- a/src/Sentry/Platforms/Android/SentrySdk.cs +++ b/src/Sentry/Platforms/Android/SentrySdk.cs @@ -108,7 +108,7 @@ private static void InitSentryAndroidSdk(SentryOptions options) } // These options we have behind feature flags - if (options.Android.EnableAndroidSdkTracing) + if (options.IsTracingEnabled && options.Android.EnableAndroidSdkTracing) { o.TracesSampleRate = (JavaDouble?)options.TracesSampleRate; diff --git a/src/Sentry/Platforms/iOS/SentrySdk.cs b/src/Sentry/Platforms/iOS/SentrySdk.cs index a204aea270..7b5ed27825 100644 --- a/src/Sentry/Platforms/iOS/SentrySdk.cs +++ b/src/Sentry/Platforms/iOS/SentrySdk.cs @@ -62,7 +62,7 @@ private static void InitSentryCocoaSdk(SentryOptions options) } // These options we have behind feature flags - if (options.iOS.EnableCocoaSdkTracing) + if (options.IsTracingEnabled && options.iOS.EnableCocoaSdkTracing) { cocoaOptions.TracesSampleRate = options.TracesSampleRate; diff --git a/src/Sentry/Properties/AssemblyInfo.cs b/src/Sentry/Properties/AssemblyInfo.cs index 2985f67713..c6bc3945c2 100644 --- a/src/Sentry/Properties/AssemblyInfo.cs +++ b/src/Sentry/Properties/AssemblyInfo.cs @@ -21,6 +21,7 @@ [assembly: InternalsVisibleTo("Sentry.DiagnosticSource, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")] [assembly: InternalsVisibleTo("Sentry.DiagnosticSource.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")] [assembly: InternalsVisibleTo("Sentry.DiagnosticSource.IntegrationTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")] +[assembly: InternalsVisibleTo("Sentry.EntityFramework, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")] [assembly: InternalsVisibleTo("Sentry.EntityFramework.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")] [assembly: InternalsVisibleTo("Sentry.Extensions.Logging, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")] [assembly: InternalsVisibleTo("Sentry.Extensions.Logging.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")] diff --git a/src/Sentry/SentryOptions.cs b/src/Sentry/SentryOptions.cs index e6c72acbc3..c5ebd61680 100644 --- a/src/Sentry/SentryOptions.cs +++ b/src/Sentry/SentryOptions.cs @@ -548,14 +548,51 @@ public bool ReportAssemblies /// public Dictionary DefaultTags => _defaultTags ??= new Dictionary(); - private double _tracesSampleRate; + /// + /// Indicates whether tracing is enabled, via any combination of + /// , , or . + /// + internal bool IsTracingEnabled => EnableTracing ?? (_tracesSampleRate > 0.0 || TracesSampler is not null); + + /// + /// Simplified option for enabling or disabling tracing. + /// + /// + /// Value + /// Effect + /// + /// + /// true + /// + /// Tracing is enabled. or will be used if set, + /// or 100% sample rate will be used otherwise. + /// + /// + /// + /// false + /// + /// Tracing is disabled, regardless of or . + /// + /// + /// + /// null + /// + /// The default setting. + /// Tracing is enabled only if or are set. + /// + /// + /// + /// + public bool? EnableTracing { get; set; } + + private double? _tracesSampleRate; /// /// Indicates the percentage of the tracing data that is collected. - /// Setting this to 0 discards all trace data. + /// Setting this to 0.0 discards all trace data. /// Setting this to 1.0 collects all trace data. /// Values outside of this range are invalid. - /// Default value is 0, which means tracing is disabled. + /// The default value is either 0.0 or 1.0, depending on the property. /// /// /// Random sampling rate is only applied to transactions that don't already @@ -564,13 +601,13 @@ public bool ReportAssemblies /// public double TracesSampleRate { - get => _tracesSampleRate; + get => _tracesSampleRate ?? (EnableTracing is true ? 1.0 : 0.0); set { - if (value is < 0 or > 1) + if (value is < 0.0 or > 1.0) { throw new InvalidOperationException( - $"The value {value} is not a valid tracing sample rate. Use values between 0 and 1."); + $"The value {value} is not a valid tracing sample rate. Use values between 0.0 and 1.0."); } _tracesSampleRate = value; diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt index df0d24bde9..81b4d2b320 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt @@ -562,6 +562,7 @@ namespace Sentry public string? Distribution { get; set; } public string? Dsn { get; set; } public bool EnableScopeSync { get; set; } + public bool? EnableTracing { get; set; } public string? Environment { get; set; } public System.TimeSpan FlushTimeout { get; set; } public System.Net.IWebProxy? HttpProxy { get; set; } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt index df0d24bde9..81b4d2b320 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt @@ -562,6 +562,7 @@ namespace Sentry public string? Distribution { get; set; } public string? Dsn { get; set; } public bool EnableScopeSync { get; set; } + public bool? EnableTracing { get; set; } public string? Environment { get; set; } public System.TimeSpan FlushTimeout { get; set; } public System.Net.IWebProxy? HttpProxy { get; set; } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt index df0d24bde9..81b4d2b320 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt @@ -562,6 +562,7 @@ namespace Sentry public string? Distribution { get; set; } public string? Dsn { get; set; } public bool EnableScopeSync { get; set; } + public bool? EnableTracing { get; set; } public string? Environment { get; set; } public System.TimeSpan FlushTimeout { get; set; } public System.Net.IWebProxy? HttpProxy { get; set; } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt index 22bb76bba1..d7651ec011 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt @@ -561,6 +561,7 @@ namespace Sentry public string? Distribution { get; set; } public string? Dsn { get; set; } public bool EnableScopeSync { get; set; } + public bool? EnableTracing { get; set; } public string? Environment { get; set; } public System.TimeSpan FlushTimeout { get; set; } public System.Net.IWebProxy? HttpProxy { get; set; } diff --git a/test/Sentry.Tests/HubTests.cs b/test/Sentry.Tests/HubTests.cs index 7394443029..8439abaa66 100644 --- a/test/Sentry.Tests/HubTests.cs +++ b/test/Sentry.Tests/HubTests.cs @@ -541,6 +541,65 @@ public void StartTransaction_StaticSampling_SampledOut() transaction.IsSampled.Should().BeFalse(); } + [Fact] + public void StartTransaction_EnableTracing_SampledIn() + { + // Arrange + _fixture.Options.EnableTracing = true; + var hub = _fixture.GetSut(); + + // Act + var transaction = hub.StartTransaction("name", "operation"); + + // Assert + transaction.IsSampled.Should().BeTrue(); + } + + [Fact] + public void StartTransaction_DisableTracing_SampledOut() + { + // Arrange + _fixture.Options.TracesSampleRate = 1.0; + _fixture.Options.EnableTracing = false; + var hub = _fixture.GetSut(); + + // Act + var transaction = hub.StartTransaction("name", "operation"); + + // Assert + transaction.IsSampled.Should().BeFalse(); + } + + [Fact] + public void StartTransaction_EnableTracing_Sampler_SampledIn() + { + // Arrange + _fixture.Options.TracesSampler = _ => 1.0; + _fixture.Options.EnableTracing = true; + var hub = _fixture.GetSut(); + + // Act + var transaction = hub.StartTransaction("name", "operation"); + + // Assert + transaction.IsSampled.Should().BeTrue(); + } + + [Fact] + public void StartTransaction_DisableTracing_Sampler_SampledOut() + { + // Arrange + _fixture.Options.TracesSampler = _ => 1.0; + _fixture.Options.EnableTracing = false; + var hub = _fixture.GetSut(); + + // Act + var transaction = hub.StartTransaction("name", "operation"); + + // Assert + transaction.IsSampled.Should().BeFalse(); + } + [Theory] [InlineData(0.25f)] [InlineData(0.50f)] diff --git a/test/Sentry.Tests/SentryOptionsTests.cs b/test/Sentry.Tests/SentryOptionsTests.cs index 72f12fd7e3..aeecae731b 100644 --- a/test/Sentry.Tests/SentryOptionsTests.cs +++ b/test/Sentry.Tests/SentryOptionsTests.cs @@ -34,4 +34,54 @@ public void AttachStackTrace_ByDefault_True() var sut = new SentryOptions(); Assert.True(sut.AttachStacktrace); } + + [Fact] + public void EnableTracing_Default_Null() + { + var sut = new SentryOptions(); + Assert.Null(sut.EnableTracing); + } + + [Fact] + public void IsTracingEnabled_Default_False() + { + var sut = new SentryOptions(); + Assert.False(sut.IsTracingEnabled); + } + + [Fact] + public void EnableTracing_WhenNull() + { + var sut = new SentryOptions + { + EnableTracing = null + }; + + Assert.Null(sut.EnableTracing); + Assert.Equal(0.0, sut.TracesSampleRate); + } + + [Fact] + public void EnableTracing_WhenFalse() + { + var sut = new SentryOptions + { + EnableTracing = false + }; + + Assert.False(sut.EnableTracing); + Assert.Equal(0.0, sut.TracesSampleRate); + } + + [Fact] + public void EnableTracing_WhenTrue() + { + var sut = new SentryOptions + { + EnableTracing = true + }; + + Assert.True(sut.EnableTracing); + Assert.Equal(1.0, sut.TracesSampleRate); + } }