Skip to content

Commit

Permalink
feat: #546 비즈니스 로직 호출 메트릭 추가 (#549)
Browse files Browse the repository at this point in the history
* feat: 비즈니스 로직 호출 Counter 메트릭 AOP 추가

* refactor: 패키지 위치 및 클래스 네이밍 변경

* fix: Counter 메트릭에서 주요 비즈니스 로직이 아닌 api는 제외하도록 변경
  • Loading branch information
apptie authored and swonny committed Oct 6, 2023
1 parent ba1dd28 commit 2d0c4bc
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.ddang.ddang.configuration.metric;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.RequiredArgsConstructor;
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.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;

@Aspect
@Component
@RequiredArgsConstructor
public class LogicMetricAop {

private final MeterRegistry registry;

@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
private void restControllerAnnotatedClass() {
}

@Pointcut("within(com.ddang.ddang.image.presentation.ImageController)")
private void imageControllerClass() {
}

@Pointcut("within(com.ddang.ddang.region.presentation.RegionController)")
private void regionControllerClass() {
}

@Pointcut("within(com.ddang.ddang.category.presentation.CategoryController)")
private void categoryControllerClass() {
}

@Pointcut("within(com.ddang.ddang.report.presentation.ReportController)")
private void reportControllerClass() {
}

@Pointcut("within(com.ddang.ddang.device.presentation.DeviceTokenController)")
private void deviceTokenControllerClass() {
}

@Pointcut("within(com.ddang.ddang.authentication.presentation.AuthenticationController)")
private void authenticationControllerClass() {
}

@Around("restControllerAnnotatedClass() && !imageControllerClass() && !regionControllerClass() && "
+ "!categoryControllerClass() && !reportControllerClass() && !deviceTokenControllerClass() && "
+ "!authenticationControllerClass()")
public Object doLog(final ProceedingJoinPoint joinPoint) throws Throwable {
if (isRequestScope()) {
final String className = findClassSimpleName(joinPoint);
final String methodName = findMethodName(joinPoint);

Counter.builder("logic")
.tag("class", className)
.tag("method", methodName)
.tag("class.method", formattedTag(className, methodName))
.description("비즈니스 로직 메트릭")
.register(registry)
.increment();
}

return joinPoint.proceed();
}

private boolean isRequestScope() {
return RequestContextHolder.getRequestAttributes() != null;
}

private String findClassSimpleName(final ProceedingJoinPoint joinPoint) {
final Class<?> clazz = joinPoint.getTarget().getClass();

return clazz.getSimpleName();
}

private String findMethodName(final ProceedingJoinPoint joinPoint) {
return joinPoint.getSignature().getName();
}

private String formattedTag(final String className, final String methodName) {
return className + "." + methodName;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.ddang.ddang.configuration;
package com.ddang.ddang.configuration.metric;

import org.springframework.boot.actuate.web.exchanges.HttpExchangeRepository;
import org.springframework.boot.actuate.web.exchanges.InMemoryHttpExchangeRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MonitoringConfiguration {
public class MetricConfiguration {

@Bean
public HttpExchangeRepository httpExchangeRepository() {
Expand Down

0 comments on commit 2d0c4bc

Please sign in to comment.