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

DisabledHub.StartTransaction now returns NoOpTransaction #3581

Merged
merged 3 commits into from
Sep 3, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Fixes
- On mobile devices, the SDK no longer throws a `FormatException` for `ProcessorFrequency` when trying to report native events ([#3541](https://github.com/getsentry/sentry-dotnet/pull/3541))

### API Changes
- When the Sentry SDK is disabled, `SentrySdk.StartTransaction()` now returns a `NoOpTransaction`, which avoids unnecessary memory allocations ([#3581](https://github.com/getsentry/sentry-dotnet/pull/3581))

### Dependencies

- Bump Cocoa SDK from v8.35.0 to v8.36.0 ([#3570](https://github.com/getsentry/sentry-dotnet/pull/3570), [#3575](https://github.com/getsentry/sentry-dotnet/pull/3575))
Expand Down
8 changes: 3 additions & 5 deletions src/Sentry/Extensibility/DisabledHub.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Sentry.Internal;
using Sentry.Protocol.Envelopes;
using Sentry.Protocol.Metrics;

Expand Down Expand Up @@ -47,11 +48,8 @@ public void ConfigureScope(Action<Scope> configureScope)
/// <summary>
/// Returns a dummy transaction.
/// </summary>
public ITransactionTracer StartTransaction(
ITransactionContext context,
IReadOnlyDictionary<string, object?> customSamplingContext) =>
// Transactions from DisabledHub are always sampled out
new TransactionTracer(this, context) { IsSampled = false };
public ITransactionTracer StartTransaction(ITransactionContext context,
IReadOnlyDictionary<string, object?> customSamplingContext) => NoOpTransaction.Instance;

/// <summary>
/// No-Op.
Expand Down
34 changes: 24 additions & 10 deletions test/Sentry.AspNet.Tests/HttpContextExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ public class HttpContextExtensionsTests
public void StartSentryTransaction_CreatesValidTransaction()
{
// Arrange
using var _ = SentrySdk.UseHub(new Hub(
new SentryOptions
{
Dsn = ValidDsn
},
Substitute.For<ISentryClient>()
));

var context = HttpContextBuilder.Build();

// Act
Expand Down Expand Up @@ -69,13 +77,16 @@ public void FinishSentryTransaction_FinishesTransaction()
public void StartSentryTransaction_SendDefaultPii_set_to_true_sets_cookies()
{
// Arrange
using var _ = SentrySdk.UseHub(new Hub(
new SentryOptions
{
Dsn = ValidDsn,
SendDefaultPii = true
},
Substitute.For<ISentryClient>()
));
var context = HttpContextBuilder.BuildWithCookies(new[] { new HttpCookie("foo", "bar") });

SentryClientExtensions.SentryOptionsForTestingOnly = new SentryOptions
{
SendDefaultPii = true
};

// Act
var transaction = context.StartSentryTransaction();

Expand All @@ -87,13 +98,16 @@ public void StartSentryTransaction_SendDefaultPii_set_to_true_sets_cookies()
public void StartSentryTransaction_SendDefaultPii_set_to_true_does_not_set_cookies_if_none_found()
{
// Arrange
using var _ = SentrySdk.UseHub(new Hub(
new SentryOptions
{
Dsn = ValidDsn,
SendDefaultPii = true
},
Substitute.For<ISentryClient>()
));
var context = HttpContextBuilder.BuildWithCookies(new HttpCookie[] { });

SentryClientExtensions.SentryOptionsForTestingOnly = new SentryOptions
{
SendDefaultPii = true
};

// Act
var transaction = context.StartSentryTransaction();

Expand Down
Loading