python icon indicating copy to clipboard operation
python copied to clipboard

Unable to capture server exceptions with Sentry

Open Gunni opened this issue 7 months ago • 2 comments

I am trying to use sentry_sdk to capture exceptions in the varlink api.

So far the only way i have found is to wrap varlink.RequestHandler like this:

class ServiceRequestHandler(varlink.RequestHandler):
    service = service

    def __init__(self, *args, **kwargs):
        with sentry_sdk.start_transaction(op='fallback', name='ServiceRequestHandler') as t:
            try:
                super().__init__(*args, **kwargs)
            except:
                sentry_sdk.capture_exception()
                raise

The problem is that this starts a new transaction instead of using the one i already made:

@service.interface('com.example')
class Example:
    def Add(self, something):
        with sentry_sdk.start_transaction(op='call', name=f'{Path(__file__).name}::Add'):
            ...

And my workaround also marks all the exceptions i do get as "handled" because of how I captured them.

Any ideas?

Gunni avatar Jun 20 '25 15:06 Gunni

Better workaround than above:

@contextmanager
def continue_trace(sentry, name):
    n = f'{Path(__file__).name}::{name}'
    try:
        with sentry_sdk.start_transaction(op='call', name=n):
            yield
    except:
        # Workaround for: https://github.com/varlink/python/issues/62
        sentry_sdk.capture_exception()
        raise

@service.interface('com.example')
class Example:
    def Add(self, something):
        with continue_trace('Add'):
            ...

Using this the transaction is correct and i can even propagate traces through api calls (not included in example), but they still show as handled exceptions because of the try/except.

Gunni avatar Jun 20 '25 16:06 Gunni

I have no experience with sentry, so I don't even quite understand the question. Sorry.

behrmann avatar Jun 24 '25 13:06 behrmann