8

How can I set the user context with the sentry raven-java client?

I've tried adding a user_email tag, and adding user_email to the MDC. They both work as expecting, with the tag going to tags, and MDC going to additional data, but neither sets the sentry user context.

I also use sentry with javascript, and with raven-js, this works great:

Raven.setUserContext({
    email: '',
    id: ''
});

Is there a java equivalent?

Alden
  • 6,553
  • 2
  • 36
  • 50
  • See https://github.com/getsentry/raven-java/issues/110 - as of 5.1.0 you can add a `new UserInterface(id, username, ipAddress, email)` – Alden Mar 03 '16 at 12:04

3 Answers3

1

Seems can't send user information directly by logback. You can look at the implementation from raven-java:

protected Event buildEvent(ILoggingEvent iLoggingEvent) {
    EventBuilder eventBuilder = new EventBuilder()
            .withTimestamp(new Date(iLoggingEvent.getTimeStamp()))
            .withMessage(iLoggingEvent.getFormattedMessage())
            .withLogger(iLoggingEvent.getLoggerName())
            .withLevel(formatLevel(iLoggingEvent.getLevel()))
            .withExtra(THREAD_NAME, iLoggingEvent.getThreadName());
    ......
}

User info is send by: withSentryInterface(new UserInterface(...)), however what I see this implement only contains code for: StackTraceInterface and MessageInterface. I think you can add one by your self.

daisydanngo
  • 75
  • 1
  • 10
0

from https://github.com/getsentry/raven-java/tree/master/raven-log4j#mapped-tags :

set map tags in the appender:

log4j.appender.SentryAppender.mappedTags=User,OS

and set the MDC in runtime:

void logWithExtras() {
    // MDC extras
    MDC.put("User", "test user");
    MDC.put("OS", "Linux");

    // This adds a message with extras and MDC keys declared in mappedTags as tags to Sentry
    logger.info("This is a test");
}
lev
  • 3,986
  • 4
  • 33
  • 46
0

If still interested, run this on startup (assuming "userid" and "ip" are set on the MDC):

Raven.getStoredInstance().addBuilderHelper(eventBuilder -> {
  UserInterface userInterface = new UserInterface(MDC.get("userid"), null, MDC.get("ip"), null);
  eventBuilder.withSentryInterface(userInterface);
});
Albert
  • 61
  • 1
  • 7