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

Add EnableTracing option #2201

Merged
merged 7 commits into from
Feb 28, 2023
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 @@ -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

Expand Down
1 change: 1 addition & 0 deletions SentryAspNetCore.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 15 additions & 9 deletions samples/Sentry.Samples.AspNetCore.Basic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
"launchBrowser": true,
"launchUrl": "throw",
"environmentVariables": {
"SENTRY_DSN": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537",
"SENTRY_RELEASE": "e386dfd",
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
builder.WebHost.UseSentry(options =>
{
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
options.EnableTracing = true;
options.Debug = true;
});

Expand Down
2 changes: 2 additions & 0 deletions samples/Sentry.Samples.AspNetCore.Grpc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions samples/Sentry.Samples.AspNetCore.Grpc/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
}

app.UseRouting();
app.UseSentryTracing();

app.UseEndpoints(endpoints =>
{
Expand Down
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.AspNetCore.Mvc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions samples/Sentry.Samples.AspNetCore.Serilog/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
3 changes: 2 additions & 1 deletion samples/Sentry.Samples.EntityFramework/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537",
"MaxRequestBodySize": "Always",
"SendDefaultPii": true,
"TracesSampleRate": 1
"EnableTracing": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry.EntityFramework/DbInterceptionIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DbConcurrencyExceptionProcessor : SentryEventExceptionProcessor<DBC
/// <summary>
/// Extracts RowCount and RowError from <see cref="DBConcurrencyException"/>.
/// </summary>
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DbEntityValidationExceptionProcessor : SentryEventExceptionProcesso
/// <summary>
/// Extracts details from <see cref="DbEntityValidationException"/> into the <see cref="SentryEvent"/>.
/// </summary>
protected override void ProcessException(DbEntityValidationException exception, SentryEvent sentryEvent)
protected internal override void ProcessException(DbEntityValidationException exception, SentryEvent sentryEvent)
{
var errorList = new Dictionary<string, List<string>>();
foreach (var error in exception.EntityValidationErrors.SelectMany(x => x.ValidationErrors))
Expand Down
40 changes: 26 additions & 14 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Platforms/Android/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Platforms/iOS/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/Sentry/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
49 changes: 43 additions & 6 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -548,14 +548,51 @@ public bool ReportAssemblies
/// </remarks>
public Dictionary<string, string> DefaultTags => _defaultTags ??= new Dictionary<string, string>();

private double _tracesSampleRate;
/// <summary>
/// Indicates whether tracing is enabled, via any combination of
/// <see cref="EnableTracing"/>, <see cref="TracesSampleRate"/>, or <see cref="TracesSampler"/>.
/// </summary>
internal bool IsTracingEnabled => EnableTracing ?? (_tracesSampleRate > 0.0 || TracesSampler is not null);

/// <summary>
/// Simplified option for enabling or disabling tracing.
/// <list type="table">
/// <listheader>
/// <term>Value</term>
/// <description>Effect</description>
/// </listheader>
/// <item>
/// <term><c>true</c></term>
/// <description>
/// Tracing is enabled. <see cref="TracesSampleRate"/> or <see cref="TracesSampler"/> will be used if set,
/// or 100% sample rate will be used otherwise.
/// </description>
/// </item>
/// <item>
/// <term><c>false</c></term>
/// <description>
/// Tracing is disabled, regardless of <see cref="TracesSampleRate"/> or <see cref="TracesSampler"/>.
/// </description>
/// </item>
/// <item>
/// <term><c>null</c></term>
/// <description>
/// <b>The default setting.</b>
/// Tracing is enabled only if <see cref="TracesSampleRate"/> or <see cref="TracesSampler"/> are set.
/// </description>
/// </item>
/// </list>
/// </summary>
public bool? EnableTracing { get; set; }

private double? _tracesSampleRate;

/// <summary>
/// Indicates the percentage of the tracing data that is collected.
/// Setting this to <c>0</c> discards all trace data.
/// Setting this to <c>0.0</c> discards all trace data.
/// Setting this to <c>1.0</c> collects all trace data.
/// Values outside of this range are invalid.
/// Default value is <c>0</c>, which means tracing is disabled.
/// The default value is either <c>0.0</c> or <c>1.0</c>, depending on the <see cref="EnableTracing"/> property.
/// </summary>
/// <remarks>
/// Random sampling rate is only applied to transactions that don't already
Expand All @@ -564,13 +601,13 @@ public bool ReportAssemblies
/// </remarks>
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Loading