Skip to content

Commit

Permalink
Finish span with exception in SentryHttpMessageHandler (#1037)
Browse files Browse the repository at this point in the history
* Finish span with exception in SentryHttpMessageHandler

* Changelog

* add tests

Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>
  • Loading branch information
Tyrrrz and bruno-garcia authored Jun 3, 2021
1 parent 29d301a commit b492d45
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
### Features

- Sentry.EntityFramework moved to this repository ([#1017](https://github.com/getsentry/sentry-dotnet/pull/1017))
- Additional `netstandard2.1` target added. Sample with .NET Core 3.1 console app.
- `UseBreadcrumbs` is called automatically by `AddEntityFramework`
- Additional `netstandard2.1` target added. Sample with .NET Core 3.1 console app.
- `UseBreadcrumbs` is called automatically by `AddEntityFramework`

### Fixes

- normalize line breaks ([#1016](https://github.com/getsentry/sentry-dotnet/pull/1016))
- Normalize line breaks ([#1016](https://github.com/getsentry/sentry-dotnet/pull/1016))
- Finish span with exception in SentryHttpMessageHandler ([#1037](https://github.com/getsentry/sentry-dotnet/pull/1037))

## 3.4.0-beta.0

Expand Down
10 changes: 4 additions & 6 deletions src/Sentry/SentryHttpMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Sentry.Extensibility;
Expand Down Expand Up @@ -79,12 +80,9 @@ protected override async Task<HttpResponseMessage> SendAsync(

return response;
}
catch
catch (Exception ex)
{
// TODO: attach the exception to the span, once
// that API is available.
span?.Finish(SpanStatus.UnknownError);

span?.Finish(ex);
throw;
}
}
Expand Down
32 changes: 30 additions & 2 deletions test/Sentry.Tests/SentryHttpMessageHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
Expand Down Expand Up @@ -71,7 +72,7 @@ public async Task SendAsync_TransactionOnScope_StartsNewSpan()
var hub = Substitute.For<IHub>();

var transaction = new TransactionTracer(
Substitute.For<IHub>(),
hub,
"foo",
"bar"
);
Expand All @@ -92,5 +93,32 @@ public async Task SendAsync_TransactionOnScope_StartsNewSpan()
span.IsFinished
);
}

[Fact]
public async Task SendAsync_ExceptionThrown_ExceptionLinkedToSpan()
{
// Arrange
var hub = Substitute.For<IHub>();

var transaction = new TransactionTracer(
hub,
"foo",
"bar"
);

hub.GetSpan().ReturnsForAnyArgs(transaction);

var exception = new Exception();

using var innerHandler = new FakeHttpMessageHandler(() => throw exception);
using var sentryHandler = new SentryHttpMessageHandler(innerHandler, hub);
using var client = new HttpClient(sentryHandler);

// Act
await Assert.ThrowsAsync<Exception>(() => client.GetAsync("https://example.com/"));

// Assert
hub.Received(1).BindException(exception, Arg.Any<ISpan>()); // second argument is an implicitly created span
}
}
}

0 comments on commit b492d45

Please sign in to comment.