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 support for BeforeSendTransaction #2188

Merged
merged 10 commits into from
Feb 27, 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 @@ -5,6 +5,7 @@
### Features

- 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))

### Fixes

Expand Down
54 changes: 50 additions & 4 deletions src/Sentry/SentryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,68 @@ public void CaptureTransaction(Transaction transaction)
"to properly finalize the transaction and send it to Sentry.");
}


// Sampling decision MUST have been made at this point
Debug.Assert(transaction.IsSampled != null,
Debug.Assert(transaction.IsSampled is not null,
"Attempt to capture transaction without sampling decision.");


if (transaction.IsSampled != true)
if (transaction.IsSampled is false)
{
_options.ClientReportRecorder.RecordDiscardedEvent(DiscardReason.SampleRate, DataCategory.Transaction);
_options.LogDebug("Transaction dropped by sampling.");
return;
}

var processedTransaction = BeforeSendTransaction(transaction);
if (processedTransaction is null) // Rejected transaction
{
_options.ClientReportRecorder.RecordDiscardedEvent(DiscardReason.BeforeSend, DataCategory.Transaction);
_options.LogInfo("Transaction dropped by BeforeSendTransaction callback.");
return;
}

CaptureEnvelope(Envelope.FromTransaction(transaction));
SeanFeldman marked this conversation as resolved.
Show resolved Hide resolved
}

private Transaction? BeforeSendTransaction(Transaction transaction)
{
if (_options.BeforeSendTransaction is null)
{
return transaction;
}

_options.LogDebug("Calling the BeforeSendTransaction callback");

try
{
return _options.BeforeSendTransaction?.Invoke(transaction);
}
catch (Exception e)
{
// Attempt to demystify exceptions before adding them as breadcrumbs.
e.Demystify();

_options.LogError("The BeforeSendTransaction callback threw an exception. It will be added as breadcrumb and continue.", e);

var data = new Dictionary<string, string>
{
{"message", e.Message}
};

if (e.StackTrace is not null)
{
data.Add("stackTrace", e.StackTrace);
}

transaction.AddBreadcrumb(
message: "BeforeSendTransaction callback failed.",
category: "SentryClient",
data: data,
level: BreadcrumbLevel.Error);
}

return transaction;
}

/// <inheritdoc />
public void CaptureSession(SessionUpdate sessionUpdate)
{
Expand Down
10 changes: 10 additions & 0 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,16 @@ public float? SampleRate
/// </remarks>
public Func<SentryEvent, SentryEvent?>? BeforeSend { get; set; }

/// <summary>
/// A callback to invoke before sending a transaction to Sentry
/// </summary>
/// <remarks>
/// The return of this transaction will be sent to Sentry. This allows the application
/// a chance to inspect and/or modify the transaction before it's sent. If the transaction
/// should not be sent at all, return null from the callback.
/// </remarks>
public Func<Transaction, Transaction?>? BeforeSendTransaction { get; set; }

/// <summary>
/// A callback invoked when a breadcrumb is about to be stored.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ namespace Sentry
public Sentry.Extensibility.IBackgroundWorker? BackgroundWorker { get; set; }
public System.Func<Sentry.Breadcrumb, Sentry.Breadcrumb?>? BeforeBreadcrumb { get; set; }
public System.Func<Sentry.SentryEvent, Sentry.SentryEvent?>? BeforeSend { get; set; }
public System.Func<Sentry.Transaction, Sentry.Transaction?>? BeforeSendTransaction { get; set; }
public string? CacheDirectoryPath { get; set; }
public System.Action<System.Net.Http.HttpClient>? ConfigureClient { get; set; }
public System.Func<bool>? CrashedLastRun { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ namespace Sentry
public Sentry.Extensibility.IBackgroundWorker? BackgroundWorker { get; set; }
public System.Func<Sentry.Breadcrumb, Sentry.Breadcrumb?>? BeforeBreadcrumb { get; set; }
public System.Func<Sentry.SentryEvent, Sentry.SentryEvent?>? BeforeSend { get; set; }
public System.Func<Sentry.Transaction, Sentry.Transaction?>? BeforeSendTransaction { get; set; }
public string? CacheDirectoryPath { get; set; }
public System.Action<System.Net.Http.HttpClient>? ConfigureClient { get; set; }
public System.Func<bool>? CrashedLastRun { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ namespace Sentry
public Sentry.Extensibility.IBackgroundWorker? BackgroundWorker { get; set; }
public System.Func<Sentry.Breadcrumb, Sentry.Breadcrumb?>? BeforeBreadcrumb { get; set; }
public System.Func<Sentry.SentryEvent, Sentry.SentryEvent?>? BeforeSend { get; set; }
public System.Func<Sentry.Transaction, Sentry.Transaction?>? BeforeSendTransaction { get; set; }
public string? CacheDirectoryPath { get; set; }
public System.Action<System.Net.Http.HttpClient>? ConfigureClient { get; set; }
public System.Func<bool>? CrashedLastRun { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ namespace Sentry
public Sentry.Extensibility.IBackgroundWorker? BackgroundWorker { get; set; }
public System.Func<Sentry.Breadcrumb, Sentry.Breadcrumb?>? BeforeBreadcrumb { get; set; }
public System.Func<Sentry.SentryEvent, Sentry.SentryEvent?>? BeforeSend { get; set; }
public System.Func<Sentry.Transaction, Sentry.Transaction?>? BeforeSendTransaction { get; set; }
public string? CacheDirectoryPath { get; set; }
public System.Action<System.Net.Http.HttpClient>? ConfigureClient { get; set; }
public System.Func<bool>? CrashedLastRun { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
Timestamp: DateTimeOffset_1,
Message: BeforeSendTransaction callback failed.,
Data: {
message: Exception message!,
stackTrace:
at Task Sentry.Tests.SentryClientTests.CaptureTransaction_BeforeSendTransactionThrows_ErrorToEventBreadcrumb()
at Transaction Sentry.SentryClient.BeforeSendTransaction(...)
},
Category: SentryClient,
Level: error
}
]
71 changes: 71 additions & 0 deletions test/Sentry.Tests/SentryClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,77 @@ public void CaptureTransaction_DisposedClient_DoesNotThrow()
});
}

[Fact]
public void CaptureTransaction_BeforeSendTransaction_RejectEvent()
{
_fixture.SentryOptions.BeforeSendTransaction = _ => null;

var sut = _fixture.GetSut();
sut.CaptureTransaction(
new Transaction("test name", "test operation")
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now // finished
});

_ = _fixture.BackgroundWorker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
}

[Fact]
public void CaptureTransaction_BeforeSendTransaction_ModifyEvent()
{
Transaction received = null;
_fixture.SentryOptions.BeforeSendTransaction = tx => received = tx;

var transaction = new Transaction("test name", "test operation")
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now // finished
};

var sut = _fixture.GetSut();
sut.CaptureTransaction(transaction);

Assert.Same(transaction, received);
}

[Fact]
public void CaptureTransaction_BeforeSendTransaction_SamplingNull_DropsEvent()
{
_fixture.SentryOptions.SampleRate = null;

Transaction received = null;
_fixture.SentryOptions.BeforeSendTransaction = e => received = e;

var transaction = new Transaction("test name", "test operation")
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now // finished
};

var sut = _fixture.GetSut();

sut.CaptureTransaction(transaction);

Assert.Same(transaction, received);
}

[Fact]
public void CaptureTransaction_BeforeSendTransaction_RejectEvent_RecordsDiscard()
{
_fixture.SentryOptions.BeforeSendTransaction = _ => null;

var sut = _fixture.GetSut();
sut.CaptureTransaction( new Transaction("test name", "test operation")
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now // finished
});

_fixture.ClientReportRecorder.Received(1)
.RecordDiscardedEvent(DiscardReason.BeforeSend, DataCategory.Transaction);
}

[Fact]
public void Dispose_Worker_FlushCalled()
{
Expand Down
17 changes: 17 additions & 0 deletions test/Sentry.Tests/SentryClientTests.verify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,21 @@ public Task CaptureEvent_BeforeEventThrows_ErrorToEventBreadcrumb()

return Verify(@event.Breadcrumbs);
}

[Fact]
public Task CaptureTransaction_BeforeSendTransactionThrows_ErrorToEventBreadcrumb()
SeanFeldman marked this conversation as resolved.
Show resolved Hide resolved
{
var error = new Exception("Exception message!");
_fixture.SentryOptions.BeforeSendTransaction = _ => throw error;

var transaction = new Transaction("name", "operation")
{
IsSampled = true
};

var sut = _fixture.GetSut();
sut.CaptureTransaction(transaction);

return Verify(transaction.Breadcrumbs);
}
}