Skip to content

Commit

Permalink
DisabledHub.StartTransaction now returns NoOpTransaction (#3581)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescrosswell authored Sep 3, 2024
1 parent db1af61 commit 2a0a905
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
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

0 comments on commit 2a0a905

Please sign in to comment.