Setting user context in python tornado using logging

Hi there. We have a set of microservices many of which are written in Python tornado.

I am investigating how we can leverage sentry’s user context as we don’t currently have this set up anywhere.

We use the logging integration with a raven handler (raven.handlers.logging.SentryHandler).

From what I can tell, the handler is a singleton that I can access

>>> logger.parent.handlers[1]
<raven.handlers.logging.SentryHandler object at 0x7fa7191ffbd0>

so technically I could grab the client instance and manually set user context at the beginning of a request, but I don’t this would work correctly as the thread context can change mid-request in tornado when the ioloop picks up execution in another coroutine.

Ideally, we would just be able to pass the user context in on the actual logger.error call, as we can fetch the context at that point from tornado’s own thread-safe request context.

Would something like this be possible?

                'msg': 'Unexpected error'
            extra=dict(get_request_log_dict(request), **{
                # tags will get extracted by raven sentryHandler and used as sentry tags
                'tags': {
                    'endpoint': request.endpoint,
                    'caller_name': request.transport.caller_name,
                'user_context': { 'id': <user uuid we fetch from our own context>

Thanks in advance.