How to pass user context to Raven in Django

228 views
Skip to first unread message

Mateusz Moneta

unread,
Jan 22, 2016, 11:07:34 AM1/22/16
to sentry
Hi,

I want to set user context based on https://docs.getsentry.com/hosted/learn/context/#capturing-the-user but I can't get it to work.
I read Raven code and it seems that is impossible to set user context in different way than calling client.user_context() function or am I missing something?
In Django where you have logging setup by LOGGING setting this is completely useless in this case, I tried to pass {'user': {}} by logger extra arg but it does not work, is there any other way to do it?

Thanks in advance,
Mateusz

David Cramer

unread,
Jan 22, 2016, 1:32:06 PM1/22/16
to gets...@googlegroups.com, Mateusz Moneta
There’s a few important things here:

1. Sentry is not logging. Using LOGGING is a shortcut to capture things that look like fatal exceptions. It has limited capability and that will never change.

2. Context is not bound to a log handler, or a log message, it is bound to a cycle. When you set context you also need to unset the context at some point to stop it from propagating.

3. The logging handler allows you to pass in a custom client, with that custom client you can then do whatever you want as if it had nothing to do w/ logging. That is, you can easily bind user_context() on the client and it will propagate just as it would for other calls.
--
You received this message because you are subscribed to the Google Groups "sentry" group.
To unsubscribe from this group and stop receiving emails from it, send an email to getsentry+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mateusz Moneta

unread,
Jan 23, 2016, 5:43:52 AM1/23/16
to David Cramer, gets...@googlegroups.com
Thanks for reply David!

Ok, so I need to prepare separate logging handler for each request to correctly set user context? Do you plan to add in the future possibility to override this context by logger `extra` argument as for example request object can be passed to each logger.log method? Sometimes it can be much easier to do it just by passing it as a part of an extra argument instead of creating separate handler.
--
BR,
Mateusz

David Cramer

unread,
Jan 24, 2016, 2:00:33 PM1/24/16
to Mateusz Moneta, gets...@googlegroups.com
Our goal is to continue to move away from the logging paradigm (there’s stronger reasons for some upcoming things), so we have no plans to expand that part of the SDK. The ``extra`` bit itself is already a pretty big hack and causes a lot of confusion.

You can pass the ‘client’ param to the handler. You don’t need it per request, you just need to ensure that in your request cycle you also call client.context.clear().
Reply all
Reply to author
Forward
0 new messages