user context with java log4j2

117 views
Skip to first unread message

Jonathan Grimm

unread,
Jun 27, 2016, 8:24:52 PM6/27/16
to sentry
Hello, 

I'm using Sentry in Java with the log4j2 appender.  Things are working, but I don't see how I can set the user context.  I'm sending in some other info by putting the key/value pairs into the MDC, but I can't get Sentry to pick it up as the user (user count is 0 in Sentry).

I see the Javascript example to do this at https://docs.getsentry.com/hosted/learn/context/, but I don't see in any of the Java / log4j2 integration doc how to do it.  Help would be appreciated.

Thanks

Sonal Dedhia

unread,
Jun 28, 2016, 4:22:44 AM6/28/16
to sentry
Hello!

I have the same setup. 

Did you try setting the values before the exception is thrown? It works for me if I set the values before the exception is thrown.

Would anybody know how I can setup the tags/extraTags in a common location after the exception is thrown?

Thanks,
Sonal

Jonathan Grimm

unread,
Jun 28, 2016, 10:19:20 AM6/28/16
to sentry
What value are you actually setting / how are you setting it?  I just tried putting 'id' or 'User' in MDC.  It comes through into Sentry, and they are tags if I add those to the 'extraTags' configuration in the log4j2.xml, but my user count is still 0 for the exception.

I am setting these before the exception, basically at the beginning of the request.

Thanks for your help.

Ted Kaemming

unread,
Jun 28, 2016, 1:45:27 PM6/28/16
to gets...@googlegroups.com
Hello,


The `HttpEventBuilderHelper` is included automatically if you're using the `DefaultRavenFactory` and Servlets 2.4 or greater, as well as a framework that is setting the user details on `HttpServletRequest` instance. If this doesn't work for you, we recommend implementing a custom builder helper modeled off of the `HttpEventBuilderHelper` and adding it via a custom `RavenFactory` — this is covered in the documentation here: https://github.com/getsentry/raven-java#custom-ravenfactory 

There's no way to set the user interface values directly (via MDC or otherwise) on a per-message basis with any of the logging integrations.

Would anybody know how I can setup the tags/extraTags in a common location after the exception is thrown?

I don't understand this question. Can you clarify?

Ted

--
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.



--
--

Ted Kaemming

Infrastructure

Jonathan Grimm

unread,
Jun 28, 2016, 5:28:09 PM6/28/16
to sentry
Thanks Ted, I was able to add my custom RavenFactory that includes a custom event builder helper to add in the user information.

Sonal Dedhia

unread,
Jun 29, 2016, 12:18:29 AM6/29/16
to sentry
Hi Ted,

 I have a common exception class, MyExceptionHandler, which processes any exception and formats the response for the rest call. If I set the ThreadContext there, it is not passed to Sentry. If I set the ThreadContext before the exception is actually thrown, the tags are passed correctly to Sentry.

 Will it work if I set the ThreadContext in MyExceptionHandler? Since it is common data, maybe I should set it on application load or when a call is made?

Thanks,
Sonal

Sonal Dedhia

unread,
Jun 30, 2016, 1:35:30 AM6/30/16
to sentry

Ted Kaemming

unread,
Jun 30, 2016, 5:13:10 PM6/30/16
to gets...@googlegroups.com
I'm assuming your exception handler is executing in a different thread than the one that threw the exception? If so, there isn't any direct method to set contextual tags through the logging configurations that are not thread-local. The one exception to this is any static tag values which are set in the logging configurations, such as the `tags` attribute in this example log4j2 configuration: https://github.com/getsentry/raven-java/tree/master/raven-log4j2#configuration

If you need more flexibility and are really confident in your ability to manage your own global state without issues, you can use the `EventBuilder` interface as mentioned earlier to provide a class that reads some global state and adds the corresponding value(s) as tags to an event before it is built and sent to the Sentry server. We generally discourage doing this, but it is an option that exists if you absolutely require it.

Ted

Sonal Dedhia

unread,
Jul 1, 2016, 4:04:01 AM7/1/16
to sentry
The exception handler is in the same thread as the one that throws the exception. Static tags would not work for us as we need user specific information to be passed. Figured out that can get the same functionality by a ServletFilter, which sets the needful values.

Thanks,
Sonal
Reply all
Reply to author
Forward
0 new messages