Skip to content

Commit

Permalink
Merge pull request #339 from ASAP-as-soon-as-possible/feat/#338
Browse files Browse the repository at this point in the history
#338 [feat] Observability 개선
  • Loading branch information
KWY0218 authored Jan 19, 2025
2 parents 20d091f + 617c66d commit 5582718
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 141 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ repositories {
dependencies {
implementation "org.redisson:redisson:3.29.0"

// p6spy
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.10.0'

// QueryDSL Implementation
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ protected ErrorResponse handleInternalErrorException(final InternalErrorExceptio
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
protected ErrorResponse handleException(final Exception error, final HttpServletRequest request) throws IOException {
log.error("================================================NEW===============================================");
log.error(error.getMessage(), error);
slackUtil.sendAlert(error, request);
return ErrorResponse.error(Error.INTERNAL_SERVER_ERROR);
Expand Down
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(", ", "[", "]"));
}
}
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();
}
}
};
}
}
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.asap.server.presentation.config;

import com.asap.server.presentation.config.interceptor.DuplicatedInterceptor;
import com.asap.server.presentation.config.duplicate.DuplicatedInterceptor;
import com.asap.server.presentation.config.resolver.meeting.MeetingPathVariableResolver;
import com.asap.server.presentation.config.resolver.user.UserIdResolver;
import lombok.RequiredArgsConstructor;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
import java.util.concurrent.Executor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskDecorator;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class AsyncConfig {
@Bean
public Executor threadPoolTaskExecutor() {
public Executor threadPoolTaskExecutor(TaskDecorator taskDecorator) {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(1);
taskExecutor.setQueueCapacity(10);
taskExecutor.setMaxPoolSize(3);
taskExecutor.setTaskDecorator(taskDecorator);
return taskExecutor;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.asap.server.presentation.common.wrapper;
package com.asap.server.presentation.config.duplicate;

import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import lombok.Getter;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import lombok.Getter;

@Getter
public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.asap.server.presentation.config.filter;
package com.asap.server.presentation.config.duplicate;

import com.asap.server.presentation.common.wrapper.CustomHttpServletRequestWrapper;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;

import java.io.IOException;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(2)
@Component
public class CustomServletWrappingFilter implements Filter {

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = new CustomHttpServletRequestWrapper((HttpServletRequest) servletRequest);
filterChain.doFilter(request, servletResponse);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.asap.server.presentation.config.interceptor;
package com.asap.server.presentation.config.duplicate;


import com.asap.server.presentation.common.wrapper.CustomHttpServletRequestWrapper;
import com.asap.server.common.exception.Error;
import com.asap.server.common.exception.model.TooManyRequestException;
import jakarta.servlet.http.HttpServletRequest;
Expand Down

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/resources/file-error-appender.xml

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/resources/file-info-appender.xml

This file was deleted.

8 changes: 3 additions & 5 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<configuration>
<timestamp key="BY_DATE" datePattern="yyyy-MM-dd"/>
<property name="LOG_PATTERN"
value="[%d{yyyy-MM-dd'T'HH:mm:ss}:%-4relative] %green([%thread]) %highlight(%-5level) %boldWhite([%C.%M:%yellow(%L)]) - %msg%n"/>
value="[%d{yyyy-MM-dd'T'HH:mm:ss}:%-4relative] - [%X{traceId}] %green([%thread]) %highlight(%-5level) %boldWhite([%C.%M:%yellow(%L)]) - %msg%n"/>
<springProfile name="local">
<include resource="console-appender.xml"/>

Expand All @@ -11,12 +11,10 @@
</springProfile>

<springProfile name="!local">
<include resource="file-info-appender.xml"/>
<include resource="file-error-appender.xml"/>
<include resource="prod-rolling-file-appender.xml"/>

<root level="INFO">
<appender-ref ref="FILE-INFO"/>
<appender-ref ref="FILE-ERROR"/>
<appender-ref ref="ROLLING-FILE-LOGGING"/>
</root>
</springProfile>

Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/prod-rolling-file-appender.xml
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>

0 comments on commit 5582718

Please sign in to comment.