From ca295f437412ca90bd9fe7ae51d19c6673ff95e7 Mon Sep 17 00:00:00 2001 From: pricelees Date: Sun, 5 Oct 2025 00:19:46 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20AOP=EB=A5=BC=20=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=ED=95=9C=20=EC=84=9C=EB=B9=84=EC=8A=A4=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=EC=97=90=EC=84=9C=EC=9D=98=20=ED=8A=B8=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=8B=B1=20Observation=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/asepct/ServiceObservationAspect.kt | 25 +++++++++++ .../common/web/config/ObservationConfig.kt | 41 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 common/web/src/main/kotlin/com/sangdol/common/web/asepct/ServiceObservationAspect.kt create mode 100644 common/web/src/main/kotlin/com/sangdol/common/web/config/ObservationConfig.kt diff --git a/common/web/src/main/kotlin/com/sangdol/common/web/asepct/ServiceObservationAspect.kt b/common/web/src/main/kotlin/com/sangdol/common/web/asepct/ServiceObservationAspect.kt new file mode 100644 index 00000000..1324e5a6 --- /dev/null +++ b/common/web/src/main/kotlin/com/sangdol/common/web/asepct/ServiceObservationAspect.kt @@ -0,0 +1,25 @@ +package com.sangdol.common.web.asepct + +import io.micrometer.observation.Observation +import io.micrometer.observation.ObservationRegistry +import org.aspectj.lang.ProceedingJoinPoint +import org.aspectj.lang.annotation.Around +import org.aspectj.lang.annotation.Aspect +import org.aspectj.lang.annotation.Pointcut + +@Aspect +class ServiceObservationAspect( + private val observationRegistry: ObservationRegistry +) { + + @Pointcut("execution(* com.sangdol..business..*Service*.*(..))") + fun allServices() {} + + @Around("allServices()") + fun runWithObserve(joinPoint: ProceedingJoinPoint): Any? { + val methodName: String = joinPoint.signature.toShortString() + + return Observation.createNotStarted(methodName, observationRegistry) + .observe { joinPoint.proceed() } + } +} diff --git a/common/web/src/main/kotlin/com/sangdol/common/web/config/ObservationConfig.kt b/common/web/src/main/kotlin/com/sangdol/common/web/config/ObservationConfig.kt new file mode 100644 index 00000000..68d8ed99 --- /dev/null +++ b/common/web/src/main/kotlin/com/sangdol/common/web/config/ObservationConfig.kt @@ -0,0 +1,41 @@ +package com.sangdol.common.web.config + +import com.sangdol.common.web.asepct.ServiceObservationAspect +import io.micrometer.observation.ObservationPredicate +import io.micrometer.observation.ObservationRegistry +import io.micrometer.observation.aop.ObservedAspect +import jakarta.servlet.http.HttpServletRequest +import org.springframework.beans.factory.annotation.Value +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.http.server.observation.ServerRequestObservationContext + +@Configuration +class ObservationConfig( + @Value("\${management.endpoints.web.base-path}") private val actuatorPath: String +) { + + @Bean + fun observedAspect(observationRegistry: ObservationRegistry): ObservedAspect { + return ObservedAspect(observationRegistry) + } + + @Bean + fun serviceObservationAspect(observationRegistry: ObservationRegistry): ServiceObservationAspect { + return ServiceObservationAspect(observationRegistry) + } + + @Bean + fun excludeActuatorPredicate(): ObservationPredicate { + return ObservationPredicate { _, context -> + if (context !is ServerRequestObservationContext) { + return@ObservationPredicate true + } + + val servletRequest: HttpServletRequest = context.carrier + val requestUri = servletRequest.requestURI + + !requestUri.contains(actuatorPath) + } + } +}