Skip to content

Activate request Transaction as current span#87

Merged
intgr merged 2 commits intointgr:mainfrom
Hugo-C:expose-current-transaction
Dec 30, 2025
Merged

Activate request Transaction as current span#87
intgr merged 2 commits intointgr:mainfrom
Hugo-C:expose-current-transaction

Conversation

@Hugo-C
Copy link
Contributor

@Hugo-C Hugo-C commented May 31, 2025

Hi, I am looking to add some details on what's taking time in a request.
I looked at the documentation and examples provided by sentry:

But for some reasons it doesn't works with the transaction created in this crate. My attempt is here.

So I propose to expose the created transaction using a Rocket guard.

The result of performance_with_customs_spans:
image

get_current_transaction is what's really needed, SentryGuard and wrap_in_span are just for ease of use. We can also decide to keep get_current_transaction private and only expose SentryGuard ...

@intgr intgr self-requested a review June 13, 2025 21:53
@Hugo-C Hugo-C force-pushed the expose-current-transaction branch from 86d1977 to be2c6b6 Compare July 15, 2025 21:41
@Hugo-C
Copy link
Contributor Author

Hugo-C commented Jul 19, 2025

@intgr let me know when you had some time to review it

@intgr
Copy link
Owner

intgr commented Sep 27, 2025

I am terribly sorry, I have procrastinated about this PR.

But for some reasons it doesn't works with the transaction created in this crate. My attempt is here.

But my gut feeling is that we're not doing something right with Sentry APIs, and that's why it's not working using the approach documented by upstream.

The proposed solution seems more like a work-around. We should If doing it the "upstream way" doesn't work, we should understand it better, before implementing this approach.

I suspect what's going wrong right now is that we're creating a Transaction, but we're not telling Sentry "this is the current active transaction". But I haven't looked yet in detail.

@Hugo-C
Copy link
Contributor Author

Hugo-C commented Sep 30, 2025

Thank you for your reply.
Actix seems indeed to work out of the box. I'll look into it.

@Hugo-C Hugo-C force-pushed the expose-current-transaction branch from b284bd3 to 2eb9324 Compare November 29, 2025 20:29
@Hugo-C
Copy link
Contributor Author

Hugo-C commented Nov 29, 2025

The trick was simply to use Scope.set_span, to "stack" the transactions (Sentry doc). For some reasons I believed it was done by Sentry automatically 😕.
Doing it the right way allow us to have captured messages linked:
image

I cleaned the PR to have only relevant commits. I also recycled the get_current_transaction, it looks better to me than the akward invalid_transaction.

Copy link
Owner

@intgr intgr left a comment

Choose a reason for hiding this comment

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

Thanks! looks good to me.

Ooooops, this took over half a year. 😅

Self::start_transaction(name)
let transaction = sentry::start_transaction(transaction_context);
Hub::current().configure_scope(|scope| {
scope.set_span(Some(transaction.clone().into()));
Copy link
Owner

Choose a reason for hiding this comment

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

For a moment I thought that we could use scope.get_span() later to retrieve the transaction & avoid needing to clone/local_cache_once!()

But it's probably a bad idea, as the user might override the span. We should still finish the original one.

@intgr intgr changed the title Expose the currently running transaction Activate request Transaction as current span Dec 30, 2025
@intgr intgr merged commit 910d8de into intgr:main Dec 30, 2025
1 check passed
@intgr
Copy link
Owner

intgr commented Dec 30, 2025

@Hugo-C
Copy link
Contributor Author

Hugo-C commented Dec 31, 2025

Wonderful!

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.

2 participants