-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#338 [feat] Observability 개선
- Loading branch information
Showing
16 changed files
with
149 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/asap/server/presentation/common/log/LoggingAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.asap.server.presentation.common.log; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
@Aspect | ||
@Component | ||
@Slf4j | ||
public class LoggingAspect { | ||
|
||
private static final String REQUEST_FORMAT = "URI : %s | Request : %s"; | ||
private static final String RESPONSE_FORMAT = "URI : %s | Response : %s"; | ||
|
||
@Pointcut("execution(* com.asap.server.presentation.controller..*(..))") | ||
public void serviceLoggingExecute() { | ||
} | ||
|
||
@Pointcut("execution(* com.asap.server.presentation.common.advice.ControllerExceptionAdvice..*(..))") | ||
public void exceptionLoggingExecute() { | ||
} | ||
|
||
@Around("com.asap.server.presentation.common.log.LoggingAspect.serviceLoggingExecute()") | ||
public Object logging(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { | ||
final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); | ||
final String uri = request.getRequestURI(); | ||
final String args = getParameterNameAndArgs(proceedingJoinPoint); | ||
|
||
log.info(String.format(REQUEST_FORMAT, uri, args)); | ||
final Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs()); | ||
log.info(String.format(RESPONSE_FORMAT, uri, returnValue)); | ||
|
||
return returnValue; | ||
} | ||
|
||
@Around("com.asap.server.presentation.common.log.LoggingAspect.exceptionLoggingExecute()") | ||
public Object errorLogging(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { | ||
final Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs()); | ||
|
||
final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); | ||
final String uri = request.getRequestURI(); | ||
log.info(String.format(RESPONSE_FORMAT, uri, returnValue)); | ||
return returnValue; | ||
} | ||
|
||
private String getParameterNameAndArgs(final ProceedingJoinPoint proceedingJoinPoint) { | ||
final MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature(); | ||
final String[] parameterNames = methodSignature.getParameterNames(); | ||
final Object[] args = proceedingJoinPoint.getArgs(); | ||
|
||
if (parameterNames == null || args == null || parameterNames.length != args.length) { | ||
return "[]"; | ||
} | ||
|
||
return IntStream.range(0, parameterNames.length) | ||
.mapToObj(i -> parameterNames[i] + " : " + args[i]) | ||
.collect(Collectors.joining(", ", "[", "]")); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/asap/server/presentation/common/log/MDCContextTaskDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.asap.server.presentation.common.log; | ||
|
||
import java.util.Map; | ||
import org.slf4j.MDC; | ||
import org.springframework.core.task.TaskDecorator; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class MDCContextTaskDecorator implements TaskDecorator { | ||
@Override | ||
public Runnable decorate(Runnable runnable) { | ||
final Map<String, String> mdc = MDC.getCopyOfContextMap(); | ||
return () -> { | ||
try { | ||
if (mdc != null) { | ||
MDC.setContextMap(mdc); | ||
} | ||
runnable.run(); | ||
} finally { | ||
if (mdc != null) { | ||
mdc.clear(); | ||
} | ||
} | ||
}; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/asap/server/presentation/common/log/MDCFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.asap.server.presentation.common.log; | ||
|
||
import jakarta.servlet.Filter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
import org.slf4j.MDC; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Order(1) | ||
@Component | ||
public class MDCFilter implements Filter { | ||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) | ||
throws IOException, ServletException { | ||
MDC.put("traceId", UUID.randomUUID().toString()); | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
MDC.clear(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/com/asap/server/presentation/config/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 0 additions & 78 deletions
78
src/main/java/com/asap/server/presentation/config/aspect/LoggingAspect.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
...pper/CustomHttpServletRequestWrapper.java → ...cate/CustomHttpServletRequestWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 7 additions & 4 deletions
11
...g/filter/CustomServletWrappingFilter.java → ...uplicate/CustomServletWrappingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...ig/interceptor/DuplicatedInterceptor.java → ...nfig/duplicate/DuplicatedInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
src/main/java/com/asap/server/presentation/config/filter/HttpRequestConfig.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<included> | ||
<appender name="ROLLING-FILE-LOGGING" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | ||
<fileNamePattern>/logs/info-%d{yyyy-MM-dd}.log</fileNamePattern> | ||
<maxHistory>30</maxHistory> | ||
</rollingPolicy> | ||
<encoder> | ||
<pattern>${LOG_PATTERN}</pattern> | ||
</encoder> | ||
</appender> | ||
</included> |