'How to set the user object to Sentry in Spring Boot?
In a Spring Boot project I have a SentryConfig.java
file
package example.services.bo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
@Configuration
public class SentryConfig {
@Bean
public HandlerExceptionResolver sentryExceptionResolver() {
return new io.sentry.spring.SentryExceptionResolver();
}
}
and a sentry.properties
file:
dsn=http://123a98a06c844a85b34cdf0de1fcd114:[email protected]:9000/3
stacktrace.app.packages=example.services.bo
How I'll configure my Spring project to send user details to Sentry like, username and user id?
Edit:
@Configuration
public class SentryConfig {
@Bean
public HandlerExceptionResolver sentryExceptionResolver() {
Sentry.getContext().setUser(
new UserBuilder().setEmail("[email protected]").build()
);
return new io.sentry.spring.SentryExceptionResolver();
}
}
didn't help.
Solution 1:[1]
You can use the provided static methods by Sentry to set the user in the current context as seen in this usage example:
Sentry.getContext().setUser(
new UserBuilder().setEmail("[email protected]").build()
);
Once an event occurs this will be also sent to Sentry as additional information.
Solution 2:[2]
Assuming you need the username or user id of the client, who's making API call. For example for Android userName could be the person who has logged in. If this is the case then you need to use MDC
to push these details. You need to initialize MDC at the Servlet filter or interceptor levels, it's up to you to use one of these. It's better to use the interceptor since you will have an option to clear the MDC context.
For any other additional details, you can use sentry.properties
file. Read more about at https://docs.sentry.io/clients/java/config/
for MDC context to work you need to set mdctags
, in mdctags you can add all set of mdc tags you need. For example you would need userName
, userId
then add that as
mdctags=userName,userId
Solution 3:[3]
You can change exception-resolver-order of Sentry.
sentry.exception-resolver-order=2147483647
This setting will prevent Sentry from reporting exceptions handled in any other Spring exception handler.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Krisz |
Solution 2 | sonus21 |
Solution 3 | ozankyncu |