Skip to content

Manual transaction handling not instrumenting spans correctly #4096

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

Closed
cmanallen opened this issue Feb 24, 2025 · 7 comments
Closed

Manual transaction handling not instrumenting spans correctly #4096

cmanallen opened this issue Feb 24, 2025 · 7 comments

Comments

@cmanallen
Copy link
Member

cmanallen commented Feb 24, 2025

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.22.0

Steps to Reproduce

import sentry_sdk

sentry_sdk.init(dsn=DSN, debug=True, traces_sample_rate=1)

@sentry_sdk.trace
def test():
    return 1 + 1

transaction = sentry_sdk.start_transaction(
    name="replays.consumer.process_recording",
    op="replays.consumer",
)

test()

transaction.finish()

Expected Result

A trace that looks like this:

Image

https://sentry-sdks.sentry.io/discover/trace/1b95c2cc910b434ea3f98a4b957c7c87/?dataset=transactions&field=title&field=project&field=user.display&field=timestamp&name=All%20Errors&node=txn-6ab2c26430f746fc860e615275f8ae49&project=4508876404162560&query=&queryDataset=transaction-like&sort=-timestamp&source=discover&statsPeriod=5m&timestamp=1740434501&yAxis=count%28%29

Actual Result

A trace that looks like this:

Image

https://sentry-sdks.sentry.io/discover/trace/412b2b91a64047a8884b6b46cf1585ca/?dataset=transactions&field=title&field=project&field=user.display&field=timestamp&name=All%20Errors&node=txn-6c3772f93adf4e96900f5f5aa9fff341&project=4508876404162560&query=&queryDataset=transaction-like&sort=-timestamp&source=discover&statsPeriod=5m&timestamp=1740434513&yAxis=count%28%29

@cmanallen
Copy link
Member Author

This is how I fixed it:

import sentry_sdk

sentry_sdk.init(dsn=DSN, debug=True, traces_sample_rate=1)

@sentry_sdk.trace
def test():
    return 1 + 1

with sentry_sdk.start_transaction(
    name="replays.consumer.process_recording", op="replays.consumer"
):
    test()

The context manager works but transaction start() and finish() do not.

@cmanallen
Copy link
Member Author

Not sure if this is an SDK bug or just a misunderstanding on my part. Is start_transaction only supposed to be called as a context manager?

@antonpirker
Copy link
Member

Yes, start_transaction() should always be called with a context manager.

@sentrivana
Copy link
Contributor

start_transaction creates the transaction, but it is not actually set on the scope; the scope management happens in the __enter__/__exit__ of the context manager. Without the transaction being set on the scope the transaction will not be considered active.

So the two options are either to use the context manager (with start_transaction(...):, or txn = start_transaction(...); txn.__enter__(); txn.__exit__()), or start_transaction alone and then set the transaction on the scope manually. (We can improve the docs on this.)

@cmanallen
Copy link
Member Author

@cmanallen
Copy link
Member Author

cmanallen commented Feb 25, 2025

Ah never mind I call with with transaction.start_child(...) later on.

@antonpirker
Copy link
Member

As this is resolved, I am closing this issue.

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

No branches or pull requests

3 participants