Skip to content

Conversation

@ericsampson
Copy link

Resolves #3582

With the current SentryHttpFailedRequestHandler, the captured exception stack trace is always the same two frames; this leads to all events getting lumped into a single Issue, even though the application code path (and remote API) are totally different. As you can imagine, this is less than ideal :)

This PR addresses this issue by capturing a full stack trace on .NET 6+ using ExceptionDispatchInfo.SetRemoteStackTrace.

Tests have been expanded and improved. The test of the new functionality (HandleResponse_StackTraceIncludesCallerContext) was verified to fail using the existing code, and pass using the new code.

Very similar changes could likely be made to the SentryGraphQLHttpFailedRequestHandler.cs to give the same improvement, but I didn't want to bundle too many changes together into one PR.

I'd love to get this into 5.16.3 or 6.0.0 if possible, depending on the release timing :)

Thanks!

/// <param name="ranges">Collection of ranges to check.</param>
/// <param name="statusCode">Status code to check.</param>
/// <returns>True if any range contains the given status code.</returns>
internal static bool ContainsStatusCode(this IEnumerable<HttpStatusCodeRange> ranges, int statusCode)
Copy link
Author

Choose a reason for hiding this comment

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

Couldn't use the standard method name Contains because in that case the implicit conversion operators in HttpStatusCodeRange resulted in these extension methods not getting called.

Comment on lines -58 to +74
_hub?.DidNotReceive().CaptureEvent(Arg.Any<SentryEvent>());
_hub?.DidNotReceiveWithAnyArgs().CaptureEvent(null!);
Copy link
Author

@ericsampson ericsampson Nov 11, 2025

Choose a reason for hiding this comment

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

These tests weren't actually going to catch failures due to the use of DidNotReceive().CaptureEvent(Arg.Any<SentryEvent>());, which behaves in a subtle footgun manner for capturing methods that have optional arguments.

It's much safer to use DidNotReceiveWithAnyArgs and ReceivedWithAnyArgs in these scenarios, unless you're careful to specify an explicit value for every method parameter (whether Any or a specific value)

I made the same tweak throughout the rest of this test file.

Comment on lines +39 to +42
#if NET6_0_OR_GREATER
// Add a full stack trace into the exception to improve Issue grouping,
// see https://github.com/getsentry/sentry-dotnet/issues/3582
ExceptionDispatchInfo.SetRemoteStackTrace(exception, Environment.StackTrace);
Copy link
Author

Choose a reason for hiding this comment

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

The meat of the PR.

@jamescrosswell
Copy link
Collaborator

This looks awesome @ericsampson - we're a bit tight on time but will definitely do our best to get this reviewed and included in version 6... it's a slight change in behaviour so might be good to do this in the major release.

@ericsampson
Copy link
Author

ericsampson commented Nov 12, 2025

Totally understood @jamescrosswell! Thanks. Would it be worthwhile to start just by approving all the GH workflows to run to make sure that they don't surface any issues.

  1. Do you think we could get it into a 6.0.0 beta release? That way I can test it For Real in our prod API without having to vendor the repo into our codebase. If that sounds like a good plan, LMK if I should change this PR base branch and to what.

1b) Should I add a release note line?

  1. Given the "slight change in behavior", do you think that I should add a way that customers can opt-out of this change in case that it turns out affecting them in a way that they don't like? I could potentially add it to SentryExperimentalOptions.

Best,
Eric


var @event = new SentryEvent(exception);
var hint = new SentryHint(HintTypes.HttpResponseMessage, response);
exception.SetSentryMechanism(MechanismType);
Copy link
Author

Choose a reason for hiding this comment

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

everything below here in the file is only appears changed due to indenting

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.

SentryHttpMessageHandler issue grouping is too eager

2 participants