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

Create a Sentry event for failed HTTP requests #2320

Merged
merged 13 commits into from Apr 28, 2023

Conversation

jamescrosswell
Copy link
Collaborator

@jamescrosswell jamescrosswell commented Apr 20, 2023

Captures failed HTTP Requests as a Sentry error event.

Closes #2217

@jamescrosswell jamescrosswell linked an issue Apr 20, 2023 that may be closed by this pull request
src/Sentry/HttpStatusCodeRange.cs Outdated Show resolved Hide resolved
src/Sentry/HttpStatusCodeRange.cs Outdated Show resolved Hide resolved
src/Sentry/HttpStatusCodeRange.cs Outdated Show resolved Hide resolved
src/Sentry/HttpStatusCodeRange.cs Outdated Show resolved Hide resolved
src/Sentry/ISentryFailedRequestHandler.cs Outdated Show resolved Hide resolved
src/Sentry/SentryHttpMessageHandler.cs Outdated Show resolved Hide resolved
src/Sentry/SentryFailedRequestHandler.cs Outdated Show resolved Hide resolved
src/Sentry/SentryFailedRequestHandler.cs Outdated Show resolved Hide resolved
src/Sentry/SentryFailedRequestHandler.cs Outdated Show resolved Hide resolved
src/Sentry/SentryOptions.cs Outdated Show resolved Hide resolved
@mattjohnsonpint
Copy link
Contributor

Good approach. 👍

@jamescrosswell jamescrosswell self-assigned this Apr 22, 2023
jamescrosswell and others added 3 commits April 25, 2023 11:35
Suggestions from code review

Co-authored-by: Matt Johnson-Pint <matt.johnson-pint@sentry.io>
…or when start > end...

- Tidied whitespace
- Enabled warnings when not using file scoped namespaces in the .editorconfig
CHANGELOG.md Outdated Show resolved Hide resolved
- Moved Response to Sentry.Protocol
- Fixed 2 tests that were failing on .NET 4.8
@jamescrosswell
Copy link
Collaborator Author

OK I think this is working now... @mattjohnsonpint see sample event.

@jamescrosswell jamescrosswell marked this pull request as ready for review April 28, 2023 02:09
@mattjohnsonpint mattjohnsonpint changed the title Feat/http client errors Create a Sentry event for failed HTTP requests Apr 28, 2023
.editorconfig Outdated Show resolved Hide resolved
@mattjohnsonpint mattjohnsonpint merged commit 7513600 into main Apr 28, 2023
15 checks passed
@mattjohnsonpint mattjohnsonpint deleted the feat/http-client-errors branch April 28, 2023 20:31

// The ContentLength might be null (but that's ok).
// See https://github.com/dotnet/runtime/issues/16162
var bodySize = response.Content?.Headers?.ContentLength;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattjohnsonpint this was the approach I'd originally tried. However it's not that it returns null - when running tests against the net43 target it sometimes throws a System.ObjectDisposedException... so I don't think this code is safe.

I also tried things like checking response.Content?.Headers.?.Contains("Content-Length") before reading this, which avoided the exception on net43 but would prevent us from capturing the BodySize in later versions of .NET where it could be calculated but wasn't stored in the header.

It looks like HttpContent.TryComputeLength (which I think is where they fixed all this) was introduced in .NET 4.5... so possibly NET5_0_OR_GREATER is the wrong cut-off point...

An alternative I toyed with was try... catch(ObjectDisposedException ex) {}. Possibly that's a better solution but it would definitely need to be accompanied by a code comment explaining why it was there... we'd need to keep using an #if directive in the unit tests, but it would mean we'd be capturing the BodySize in all circumstances where this was possible (failing only when an exception was thrown by older versions of the .NET Framework).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe now, as it's been moved before EnsureSuccessStatusCode which used to dispose the content (which is why you got ObjectDisposedException). See my comment just above this one.

As for specifically checking "Content-Length" - we could add that if bodySize == null, but I'd want to reproduce the issue first. I tried locally (on both NetFx and Mono) and the value was always present if it came from an actual HttpClient instance. And if it came just from ResponseMessage (like in the tests), then Headers was null, so there'd be nothing to check against.

Either way, I'm not too worried about it, if it happens to be absent. The disposed exception is the main thing, which is taken care of.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting... How did you work out where Dispose was being called?

I also just reaslised that relying on EnsureSuccessStatusCode to throw an exception means it's not possible to configure FailedRequestStatusCodes in the 200-299 range... nothing in that range will result in an exception being thrown here that will be captured by Sentry.

I can't think why someone would want to do that... Something to be aware of though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create a Sentry event for failed HTTP requests
2 participants