https://intothemaze.tistory.com/14
https://intothemaze.tistory.com/15
Sentry 설정의 (아마도) 마지막 포스팅은
1. ControllerAdvice에 의해 전역 예외 처리되기 전에 Exception을 Sentry로 전송
2. 해당 예외를 Custom 필터링 한 후 Sentry에 전달
이 두 가지에 대해 알아보고자 한다.
SentryExceptionResolver
Sentry는 ExceptionHandler에 의해 예외가 처리되는 경우 Sentry로 예외를 보내지 않는다.
그러나 Exception이 처리 되어도 로깅 및 모니터링이 필요한 예외가 있을 수 있기 때문에 ExceptionHandler 앞에 SentryExceptionResolver를 두어 모든 예외를 Sentry로 전달할 수 있다.
Recording Exceptions
In order to record all exceptions thrown by your controllers, you can register io.sentry.spring.SentryExceptionResolver as a Bean in your application. Once registered, all exceptions will be sent to Sentry and then passed on to the default exception handlers.
@Configuration
public class SentryConfig {
@Bean
public HandlerExceptionResolver resolveSentryException() {
return new SentryExceptionResolver();
}
}
public class SentryExceptionResolver implements HandlerExceptionResolver, Ordered {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
Sentry.captureException(ex);
// null = run other HandlerExceptionResolvers to actually handle the exception
return null;
}
@Override
public int getOrder() {
return Integer.MIN_VALUE;
}
}
- Sentry.captureException(): Exception 발생 시 Sentry로 전송한다.
- getOrder(): 값이 작을수록 우선순위가 높게 설정된다. Integer.MIN_VALUE를 반환함으로써 기존 Exception Handler 보다 먼저 실행되도록 보장한다.
CustomBeforeSendCallback
SentryExceptionResolver를 구현한 건 좋지만, 모든 예외를 로깅 및 모니터링 하고 싶은 건 아닌데🤔
그럴 때는 Sentry의 BeforeSendCallback를 구현하여 Custome 필터링 기능을 추가하면 된다.
beforeSend는 이벤트가 서버로 전송되기 바로 전에 호출되며, 데이터를 편집할 수 있는 마지막 단계이다.
따라서 커스텀 필터링 로직을 추가하여 SentryExceptionResolver에 의해 전달된 예외를 필터링 할 수 있다.
Using beforeSend
All Sentry SDKs support the beforeSend callback method. beforeSend is called immediately before the event is sent to the server, so it’s the final place where you can edit its data. It receives the event object as a parameter, so you can use that to modify the event’s data or drop it completely (by returning null) based on custom logic and the data available on the event.
import io.sentry.SentryEvent;
import io.sentry.SentryOptions;
import io.sentry.Hint
import org.springframework.stereotype.Component;
@Component
public class CustomBeforeSendCallback implements SentryOptions.BeforeSendCallback {
@Override
public SentryEvent execute(SentryEvent event, Hint hint) {
if (event.getThrowable() instanceof SQLException) {
event.setFingerprints(Arrays.asList("database-connection-error"));
}
return event;
}
}
Sentry 공식 문서에 나와 있는 위의 코드를 참고하여 아래와 같이 작성하였다.
@Component
public class CustomBeforeSendCallback implements SentryOptions.BeforeSendCallback {
@Override
public SentryEvent execute(SentryEvent event, Hint hint) {
if (event.getThrowable() instanceof NotFoundException) {
return null;
}
return event;
}
}
서버로 전송하지 않을 예외들을 필터링하는 Blacklist 방식의 코드이다.
필터링 할 예외가 늘어날 경우 private 도우미 메서드를 만들어 사용하자.
Reference
https://docs.sentry.io/platforms/java/guides/spring/configuration/filtering/
https://www.tabnine.com/code/java/methods/io.sentry.Sentry/capture
'Etc.' 카테고리의 다른 글
[GIT] Trunk-based development (0) | 2023.11.20 |
---|---|
[Sentry] Sentry Slack 연동 및 Logging 레벨 설정 (Spring Boot) (0) | 2023.08.07 |
[Sentry] Spring Boot에 Sentry 적용하기 (0) | 2023.08.01 |