Sentry-python: Automatic propagation of scope with gevent and gunicorn

Created on 19 Feb 2021  路  3Comments  路  Source: getsentry/sentry-python

Environment

Which SDK and version?
sentry-sdk==0.20.2
gevent==21.1.2
gunicorn==20.0.4

Steps to Reproduce

I have this code:

from gevent import monkey
monkey.patch_all()

import os
import bottle
import gevent
import sentry_sdk
from sentry_sdk.integrations.threading import ThreadingIntegration

sentry_kwargs = {
    "environment": "sentry-test",
    "ignore_errors": ["greenlet.GreenletExit"],
    "dsn": os.environ["SENTRY_DSN"],
    "integrations": [ThreadingIntegration(propagate_hub=True)]
}
sentry_sdk.init(**sentry_kwargs)


@bottle.route('/request')
def request():
    with sentry_sdk.configure_scope() as scope:
        scope.set_tag("url", bottle.request.path)
        print(sentry_sdk.Hub.current.scope)
        print(sentry_sdk.Hub.current.scope._tags)
        gevent.spawn(_request_call)
        return {}


def _request_call():
    print(sentry_sdk.Hub.current.scope)
    print(sentry_sdk.Hub.current.scope._tags)

Expected Result

I would expect ThreadingIntegration(propagate_hub=True) would propagate scope to gevent, but it seems it's not the case.

Actual Result

<Scope id=0x111b31438 name=bottle>
{'url': '/request'}
<Scope id=0x1117296d8 name=None>
{}
enhancement

Most helpful comment

@lucas03 we have no immediate plans right now, we can consider it in the future but right now we also don't have it for asyncio either.

I'll leave this open as feature request.

All 3 comments

Automatic hub propagation only works for threads, not gevent green threads. Do this instead:

hub = Hub(sentry_sdk.Hub.current)
gevent.spawn(lambda: _request_call(hub))

def _request_call(hub):
    with hub:
        ...

yes, that is what you referred to on discord and I should have linked the doc here, sorry about that.

However this solution requires method calls to accept hub as an argument. I believe this makes programing more complex for developers and it's easier to make a mistake with this manual hub passing.

Is there no plan to add automatic support for green threads in the future?

@lucas03 we have no immediate plans right now, we can consider it in the future but right now we also don't have it for asyncio either.

I'll leave this open as feature request.

Was this page helpful?
0 / 5 - 0 ratings