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

Tracing now works for SentryHttpMessageHandler even when there is no active transaction #3360

Merged
merged 9 commits into from May 13, 2024

Conversation

jamescrosswell
Copy link
Collaborator

@jamescrosswell jamescrosswell commented May 10, 2024

Resolves #2931

Problem summary

This logic in the SentryHttpMessageHandler means spans only gets created by SentryHttpMessageHandler if there is already an active transaction set on the scope.:

var span = _hub.GetSpan()?.StartChild(
"http.client",
$"{method} {url}" // e.g. "GET https://example.com"
);

If I add this to a MAUI event handler, a span for the HttpClient request is sent correctly as part of the transaction:

        var transaction = SentrySdk.StartTransaction("OnCounterClicked", "Scour the web");
        SentrySdk.ConfigureScope(scope => scope.Transaction = transaction);
        try
        {
            var messageHandler = new SentryHttpMessageHandler();
            var httpClient = new HttpClient(messageHandler, true);
            var html = await httpClient.GetStringAsync("https://example.com/");
            CounterBtn.Text = html;
        }
        finally
        {
            transaction.Finish();
        }

Solution

I've used Hub.StartSpan instead. This either creates a transaction (if no transaction is set on the scope) or create a child span of the transaction (if a transaction has been set on the scope).

@jamescrosswell jamescrosswell marked this pull request as ready for review May 10, 2024 10:02
Copy link
Contributor

@bitsandfoxes bitsandfoxes left a comment

Choose a reason for hiding this comment

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

Can we find a solution where there is no need to differentiate between Hub and HubAdapter? The hub vs _realHub thing makes my head spin.

src/Sentry/HubExtensions.cs Outdated Show resolved Hide resolved
src/Sentry/IMetricHub.cs Outdated Show resolved Hide resolved
src/Sentry/Internal/Hub.cs Outdated Show resolved Hide resolved
bitsandfoxes
bitsandfoxes previously approved these changes May 13, 2024
src/Sentry/HubExtensions.cs Show resolved Hide resolved
CHANGELOG.md Show resolved Hide resolved
Copy link
Contributor

@bitsandfoxes bitsandfoxes left a comment

Choose a reason for hiding this comment

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

Is this going to work with Requests? Afaik there needs to be a Span within the Transaction?

Copy link
Contributor

@bitsandfoxes bitsandfoxes left a comment

Choose a reason for hiding this comment

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

Is this going to work with Requests? Afaik there needs to be a Span within the Transaction?

I think this is unrelated to the fix here. If it didn't work before, this won't break it.

@bitsandfoxes bitsandfoxes merged commit 3d5201f into main May 13, 2024
13 checks passed
@bitsandfoxes bitsandfoxes deleted the messagehandler-transaction branch May 13, 2024 15:24
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 on MAUI not propagating traces for HttpClient requests
3 participants