feat: 로그 모니터링을 위한 환경 구축#79
Conversation
Summary of ChangesHello @starboxxxx, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 이 PR은 포괄적인 로그 모니터링 환경을 구축합니다. 구조화된 로깅을 위해 Logstash를 통합하며, 특히 컨트롤러 상호작용 및 보안 관련 예외에 대한 AOP 기반 로깅을 구현합니다. 이는 오류 및 성공 로깅을 중앙 집중화하고 표준화하여 시스템의 가시성과 문제 해결 능력을 향상시키는 것을 목표로 합니다. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
이 PR은 AOP와 MDC를 사용하여 컨트롤러 및 인증 필터 수준에서 구조화된 JSON 로그를 생성함으로써 로그 모니터링 환경을 구축합니다. 전반적으로 좋은 접근 방식이지만, 몇 가지 개선점을 제안합니다. 특히, AspectConfig에서 MDC를 정리하는 부분은 동시성 환경에서 발생할 수 있는 로그 오염 문제를 방지하기 위해 finally 블록을 사용하는 것이 중요합니다. 또한 JwtAuthenticationFilter의 예외 처리 로직에서 코드 중복을 줄이고, AspectUtil에서 예외 로깅을 좀 더 상세하게 하여 디버깅을 용이하게 할 수 있습니다.
| public void doReturn(JoinPoint joinPoint, Object result) { | ||
| aspectUtil.putCommon(joinPoint); | ||
| aspectUtil.putSuccess(result); | ||
|
|
||
| log.info("Request Success"); | ||
|
|
||
| aspectUtil.clear(); | ||
| } |
There was a problem hiding this comment.
doReturn 메소드에서 aspectUtil.clear()가 마지막에 호출되고 있습니다. 만약 log.info()와 같은 중간 과정에서 예외가 발생하면 clear()가 호출되지 않아 MDC 정보가 현재 스레드에 남아 다른 요청의 로그를 오염시킬 수 있습니다. try-finally 블록을 사용하여 MDC가 항상 정리되도록 보장하는 것이 안전합니다.
| public void doReturn(JoinPoint joinPoint, Object result) { | |
| aspectUtil.putCommon(joinPoint); | |
| aspectUtil.putSuccess(result); | |
| log.info("Request Success"); | |
| aspectUtil.clear(); | |
| } | |
| public void doReturn(JoinPoint joinPoint, Object result) { | |
| try { | |
| aspectUtil.putCommon(joinPoint); | |
| aspectUtil.putSuccess(result); | |
| log.info("Request Success"); | |
| } finally { | |
| aspectUtil.clear(); | |
| } | |
| } |
| public void doThrowing(JoinPoint joinPoint, Throwable ex) { | ||
| aspectUtil.putCommon(joinPoint); | ||
| aspectUtil.putError(ex); | ||
|
|
||
| log.error("Request Failed"); | ||
|
|
||
| aspectUtil.clear(); | ||
| } |
There was a problem hiding this comment.
doThrowing 메소드 또한 doReturn과 마찬가지로 try-finally 블록을 사용하여 MDC가 항상 정리되도록 해야 합니다. 예외 처리 로직에서 또 다른 예외가 발생할 경우 MDC 정보가 유출(leak)되어 다른 요청의 로그를 오염시킬 수 있습니다.
public void doThrowing(JoinPoint joinPoint, Throwable ex) {
try {
aspectUtil.putCommon(joinPoint);
aspectUtil.putError(ex);
log.error("Request Failed");
} finally {
aspectUtil.clear();
}
}| HttpServletRequest request, | ||
| HttpServletResponse response, | ||
| FilterChain filterChain |
There was a problem hiding this comment.
상위 클래스인 OncePerRequestFilter의 doFilterInternal 메소드는 파라미터에 org.springframework.lang.NonNull 애노테이션을 사용하고 있습니다. 코드의 명확성과 일관성을 위해, 그리고 Spring 프레임워크와의 호환성을 위해 @NonNull 애노테이션을 추가하는 것을 권장합니다. @NotNull이 제거되었는데, org.springframework.lang.NonNull을 import하여 @NonNull로 대체하는 것이 좋습니다.
| HttpServletRequest request, | |
| HttpServletResponse response, | |
| FilterChain filterChain | |
| @NonNull HttpServletRequest request, | |
| @NonNull HttpServletResponse response, | |
| @NonNull FilterChain filterChain |
| } catch (JwtException ex) { | ||
| aspectUtil.putCommonFromRequest(request); | ||
| aspectUtil.putError(ex); | ||
|
|
||
| log.error("Authentication Error"); | ||
|
|
||
| aspectUtil.clear(); | ||
| throw ex; | ||
|
|
||
| } catch (AccessDeniedException ex) { | ||
| aspectUtil.putCommonFromRequest(request); | ||
| aspectUtil.putError(ex); | ||
|
|
||
| log.error("Access Denied Error"); | ||
|
|
||
| aspectUtil.clear(); | ||
| accessDeniedHandler.handle(request, response, ex); | ||
| } |
There was a problem hiding this comment.
JwtException과 AccessDeniedException을 처리하는 catch 블록에 로깅 관련 코드가 중복되고 있습니다. 코드 가독성과 유지보수성을 높이기 위해 이 로직을 별도의 private 헬퍼 메소드로 추출하는 것을 고려해 보세요.
예시:
private void logAndClearMdc(HttpServletRequest request, Throwable ex, String errorMessage) {
aspectUtil.putCommonFromRequest(request);
aspectUtil.putError(ex);
log.error(errorMessage);
aspectUtil.clear();
}
// ...
} catch (JwtException ex) {
logAndClearMdc(request, ex, "Authentication Error");
throw ex;
} catch (AccessDeniedException ex) {
logAndClearMdc(request, ex, "Access Denied Error");
accessDeniedHandler.handle(request, response, ex);
}| } | ||
| else { | ||
| MDC.put("errorCode", "UNEXPECTED"); | ||
| MDC.put("errorMessage", shorten(ex.getMessage())); |
🍀 이슈 번호
✅ 작업 사항
⌨ 기타