Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dependencies {
// Test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter:1.1.11")

// Rest Docs & Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
Expand Down
42 changes: 29 additions & 13 deletions src/main/java/com/debatetimer/aop/logging/ClientLoggingAspect.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.debatetimer.aop.logging;

import com.debatetimer.exception.custom.DTOAuthClientException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
Expand All @@ -12,22 +14,22 @@
@Component
public class ClientLoggingAspect extends LoggingAspect {

private static final String CLIENT_REQUEST_TIME_KEY = "clientRequestTime";

@Pointcut("@within(com.debatetimer.aop.logging.LoggingClient)")
public void loggingClients() {
}

@Around("loggingClients()")
public Object loggingControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
setMdc(CLIENT_REQUEST_TIME_KEY, System.currentTimeMillis());
logClientRequest(proceedingJoinPoint);

Object responseBody = proceedingJoinPoint.proceed();

logClientResponse(proceedingJoinPoint);
removeMdc(CLIENT_REQUEST_TIME_KEY);
return responseBody;
try {
logClientRequest(proceedingJoinPoint);
Object responseBody = proceedingJoinPoint.proceed();
logClientResponse(proceedingJoinPoint);
return responseBody;
} catch (DTOAuthClientException exception) {
logClientErrorRequest(proceedingJoinPoint);
logClientErrorResponse(exception.getErrorResponse());
throw exception;
}
}

private void logClientRequest(ProceedingJoinPoint joinPoint) {
Expand All @@ -39,9 +41,23 @@ private void logClientRequest(ProceedingJoinPoint joinPoint) {
private void logClientResponse(ProceedingJoinPoint joinPoint) {
String clientName = joinPoint.getSignature().getDeclaringType().getSimpleName();
String methodName = joinPoint.getSignature().getName();
long latency = getLatency(CLIENT_REQUEST_TIME_KEY);
log.info("Client Response Logging - Client Name: {} | MethodName: {} | Latency: {}ms",
clientName, methodName, latency);
log.info("Client Response Logging - Client Name: {} | MethodName: {}", clientName, methodName);
}

private void logClientErrorRequest(ProceedingJoinPoint proceedingJoinPoint) {
HttpServletRequest request = getHttpServletRequest();
String requestParameters = getRequestParameters(proceedingJoinPoint);
String uri = request.getRequestURI();
String httpMethod = request.getMethod();
log.info("Client Request Error Logging: {} {} parameters - {}", httpMethod, uri, requestParameters);
}

private void logClientErrorResponse(String responseBody) {
HttpServletRequest request = getHttpServletRequest();
String uri = request.getRequestURI();
String httpMethod = request.getMethod();
log.info("Client Response Error Logging - Client Name: {} | MethodName: {} | response_body: {}",
httpMethod, uri, responseBody);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@


import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
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.CodeSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Slf4j
@Aspect
@Component
public class ControllerLoggingAspect extends LoggingAspect {

private static final String REQUEST_ID_KEY = "requestId";
private static final String START_TIME_KEY = "startTime";

@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
public void allController() {
Expand All @@ -31,13 +24,12 @@ public void allController() {
@Around("allController()")
public Object loggingControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
setMdc(REQUEST_ID_KEY, UUID.randomUUID().toString());
setMdc(START_TIME_KEY, System.currentTimeMillis());
logControllerRequest(proceedingJoinPoint);

Object responseBody = proceedingJoinPoint.proceed();

logControllerResponse(responseBody);
removeMdc(START_TIME_KEY);
removeMdc(REQUEST_ID_KEY);
return responseBody;
}

Expand All @@ -53,23 +45,6 @@ private void logControllerResponse(Object responseBody) {
HttpServletRequest request = getHttpServletRequest();
String uri = request.getRequestURI();
String httpMethod = request.getMethod();
long latency = getLatency(START_TIME_KEY);
log.info("Response Logging: {} {} Body: {} latency - {}ms", httpMethod, uri, responseBody, latency);
}

private HttpServletRequest getHttpServletRequest() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
return requestAttributes.getRequest();
}

private String getRequestParameters(JoinPoint joinPoint) {
CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();
String[] parameterNames = codeSignature.getParameterNames();
Object[] args = joinPoint.getArgs();
Map<String, Object> params = new HashMap<>();
for (int i = 0; i < parameterNames.length; i++) {
params.put(parameterNames[i], args[i]);
}
return params.toString();
log.info("Response Logging: {} {} Body: {}", httpMethod, uri, responseBody);
}
}
24 changes: 21 additions & 3 deletions src/main/java/com/debatetimer/aop/logging/LoggingAspect.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.debatetimer.aop.logging;

import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.CodeSignature;
import org.slf4j.MDC;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Slf4j
public abstract class LoggingAspect {
Expand All @@ -14,8 +21,19 @@ protected final void removeMdc(String key) {
MDC.remove(key);
}

protected final long getLatency(String startTimeKey) {
long startTime = Long.parseLong(MDC.get(startTimeKey));
return System.currentTimeMillis() - startTime;
protected HttpServletRequest getHttpServletRequest() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
return requestAttributes.getRequest();
}

protected String getRequestParameters(JoinPoint joinPoint) {
CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();
String[] parameterNames = codeSignature.getParameterNames();
Object[] args = joinPoint.getArgs();
Map<String, Object> params = new HashMap<>();
for (int i = 0; i < parameterNames.length; i++) {
params.put(parameterNames[i], args[i]);
}
return params.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoggingClient {

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class DiscordNotifier implements ErrorNotifier {

private static final String NOTIFICATION_PREFIX = ":rotating_light: [**Error 발생!**]\n";
private static final String STACK_TRACE_AFFIX = "\n```\n";
private static final String STACK_TRACE_AFFIX = "\n```\n";
private static final String DISCORD_LINE_SEPARATOR = "\n";
private static final int STACK_TRACE_LENGTH = 10;

Expand All @@ -29,6 +29,7 @@ private JDA initializeJda(String token) {
try {
return JDABuilder.createDefault(token).build().awaitReady();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new DTInitializationException(InitializationErrorCode.JDA_INITIALIZATION_FAIL);
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/debatetimer/client/oauth/OAuthClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.debatetimer.dto.member.MemberCreateRequest;
import com.debatetimer.dto.member.MemberInfo;
import com.debatetimer.dto.member.OAuthToken;
import com.debatetimer.exception.handler.OAuthClientErrorHandler;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;
Expand All @@ -17,8 +19,8 @@ public class OAuthClient {
private final RestClient restClient;
private final OAuthProperties oauthProperties;

public OAuthClient(OAuthProperties oauthProperties) {
this.restClient = RestClient.create();
public OAuthClient(RestClient.Builder restClientBuilder, OAuthProperties oauthProperties) {
this.restClient = restClientBuilder.build();
this.oauthProperties = oauthProperties;
}

Expand All @@ -28,6 +30,7 @@ public OAuthToken requestToken(MemberCreateRequest request) {
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(oauthProperties.createTokenRequestBody(request))
.retrieve()
.onStatus(HttpStatusCode::isError, new OAuthClientErrorHandler())
.body(OAuthToken.class);
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/debatetimer/config/NotifierConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import com.debatetimer.client.notifier.DiscordNotifier;
import com.debatetimer.client.notifier.DiscordProperties;
import com.debatetimer.client.notifier.ErrorNotifier;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
public class NotifierConfig {

@Profile({"dev", "prod"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthMember {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -39,7 +40,7 @@ public TableResponses getTables(@AuthMember Member member) {
}

@PostMapping("/api/member")
public ResponseEntity<MemberCreateResponse> createMember(@RequestBody MemberCreateRequest request) {
public ResponseEntity<MemberCreateResponse> createMember(@Validated @RequestBody MemberCreateRequest request) {
MemberInfo memberInfo = authService.getMemberInfo(request);
MemberCreateResponse memberCreateResponse = memberService.createMember(memberInfo);
JwtTokenResponse jwtToken = authManager.issueToken(memberInfo);
Expand Down

This file was deleted.

Loading
Loading