-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] AOP 기반 페이지 조회 카운터 메트릭 수집 기능 구현 #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Walkthrough모니터링 기능 도입을 위해 Prometheus 및 Micrometer 기반의 메트릭 수집 기능이 추가되었습니다. 주요 API 엔드포인트에 페이지 뷰 카운트와 카운터 메트릭이 적용되었으며, 관련 의존성 및 AOP 구성, 환경설정 파일 무시 규칙이 확장되었습니다. Kafka 및 CQRS 관련 사항은 포함되지 않았습니다. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant CafeController
participant PageViewAspect
participant CafeService
participant MeterRegistry
Client->>CafeController: findAllCafes() 호출
CafeController->>PageViewAspect: @PageViewed Advice 실행 (페이지 뷰 카운트)
PageViewAspect->>MeterRegistry: "page.view.count" 카운터 증가
CafeController->>CafeService: invoke() 호출
CafeService->>MeterRegistry: @Counted Advice 실행 ("cafe.find" 카운터 증가)
CafeService-->>CafeController: 결과 반환
CafeController-->>Client: 응답 반환
Assessment against linked issues
Suggested reviewers
Poem
Tip ⚡️ Faster reviews with caching
Enjoy the performance boost—your workflow just got faster. ✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/main/kotlin/com/coffee/api/common/aop/PageViewed.kt (1)
1-6: 페이지 조회 추적을 위한 애노테이션 정의가 잘 되었습니다.
PageViewed애노테이션은 AOP 기반 페이지 조회 메트릭 수집을 위한 적절한 정의입니다. 함수 레벨에 적용되도록 타겟이 설정되었고, 런타임에 사용 가능하도록 유지되며, 페이지명을 지정할 수 있는name파라미터를 가지고 있습니다.한 가지 제안하자면, 가독성과 문서화를 위해 애노테이션에 문서화 주석(KDoc)을 추가하는 것이 좋을 것 같습니다.
package com.coffee.api.common.aop +/** + * 페이지 조회 메트릭을 수집하기 위한 애노테이션입니다. + * 이 애노테이션이 적용된 메소드가 호출될 때마다 해당 페이지의 조회수가 증가합니다. + * + * @property name 페이지 식별을 위한 고유한 이름 + */ @Target(AnnotationTarget.FUNCTION) @Retention(AnnotationRetention.RUNTIME) annotation class PageViewed(val name: String)src/main/kotlin/com/coffee/api/config/aop/CafeConfig.kt (1)
1-14: AOP 메트릭 설정 클래스 구현메트릭 수집을 위한 AOP 설정 클래스를 구현했습니다.
CountedAspect빈을 등록하여@Counted애노테이션이 작동할 수 있도록 설정하였습니다.다만, 클래스 이름이
CafeConfig보다는MetricsConfig또는MonitoringConfig와 같이 기능을 더 명확하게 나타내는 이름이 좋을 것 같습니다.-package com.coffee.api.config.aop +package com.coffee.api.config.aop import io.micrometer.core.aop.CountedAspect import io.micrometer.core.instrument.MeterRegistry import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration @Configuration -class CafeConfig { +class MetricsConfig { @Bean fun countedAspect(registry: MeterRegistry): CountedAspect = CountedAspect(registry) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
.gitignore(1 hunks)build.gradle.kts(2 hunks)buildSrc/src/main/kotlin/Dependency.kt(1 hunks)src/main/kotlin/com/coffee/api/cafe/application/CafeService.kt(2 hunks)src/main/kotlin/com/coffee/api/cafe/presentation/adapter/in/restapi/CafeController.kt(4 hunks)src/main/kotlin/com/coffee/api/common/aop/PageViewAspect.kt(1 hunks)src/main/kotlin/com/coffee/api/common/aop/PageViewed.kt(1 hunks)src/main/kotlin/com/coffee/api/config/ApiControllerAdvice.kt(1 hunks)src/main/kotlin/com/coffee/api/config/aop/CafeConfig.kt(1 hunks)
🔇 Additional comments (10)
buildSrc/src/main/kotlin/Dependency.kt (1)
18-19: 모니터링 기능을 위한 의존성 추가가 적절합니다.Spring Actuator와 Prometheus 의존성을 추가하여 페이지 조회 메트릭 수집 기능을 구현하기 위한 기반을 마련했습니다. 이는 PR 목표와 일치하며 모니터링 시스템 구축에 필수적인 요소입니다.
.gitignore (1)
44-46: 환경별 설정 파일 무시 규칙 추가가 적절합니다.환경별 설정 파일(application.yml, application-local.yml, application-prod.yml)을 Git에서 무시하도록 설정한 것은 보안 및 환경 분리 측면에서 좋은 관행입니다. 환경별 설정에는 모니터링 엔드포인트, 접근 제어 등 민감한 정보가 포함될 수 있으므로 이러한 파일을 버전 관리에서 제외하는 것이 적절합니다.
src/main/kotlin/com/coffee/api/config/ApiControllerAdvice.kt (1)
11-13: 컨트롤러 어드바이스 범위 제한이 적절합니다.
@RestControllerAdvice애노테이션에basePackages속성을 추가하여 특정 패키지(com.coffee.api.cafe.presentation.adapter.in.restapi)에만 적용되도록 제한한 것은 좋은 접근 방식입니다. 이렇게 하면 해당 패키지의 컨트롤러에만 예외 처리가 적용되어 다른 패키지의 컨트롤러와 충돌할 가능성이 줄어듭니다.다만, 나중에 다른 패키지에도 이 어드바이스를 적용해야 할 경우 확장이 필요할 수 있으니 참고하시기 바랍니다.
src/main/kotlin/com/coffee/api/cafe/presentation/adapter/in/restapi/CafeController.kt (3)
11-11: 적절한 커스텀 애노테이션 가져오기PageViewed 애노테이션을 적절하게 가져왔습니다. 이 애노테이션은 컨트롤러 메서드에 페이지 조회 메트릭을 적용하기 위한 용도로 가져와졌습니다.
29-29: 페이지 뷰 측정 애노테이션 적용카페 목록 조회 API에
@PageViewed("cafe_list")애노테이션을 추가하여 페이지 조회 메트릭을 수집할 수 있도록 하였습니다. 이름이 명확하게 지정되어 메트릭 식별이 용이합니다.
40-40: 일관된 페이지 뷰 메트릭 구현모든 주요 엔드포인트에
@PageViewed애노테이션을 일관되게 적용하여 엔드포인트별 페이지 조회수를 추적할 수 있도록 구현하였습니다. API 엔드포인트의 목적에 맞게 태그 이름을 지정하여 메트릭의 가독성이 좋습니다.Also applies to: 50-50, 57-57
src/main/kotlin/com/coffee/api/cafe/application/CafeService.kt (2)
6-6: Micrometer 의존성 추가메트릭 수집을 위한 Micrometer의 Counted 애노테이션을 적절하게 가져왔습니다.
16-16: 서비스 메서드 호출 카운터 구현
@Counted애노테이션을 사용하여 카페 목록 조회 서비스 메서드의 호출 횟수를 추적하는 메트릭을 구현했습니다. 메트릭 이름과 설명이 명확하여 모니터링 대시보드에서 식별하기 쉽습니다.build.gradle.kts (2)
83-87: 모니터링 관련 의존성 추가Spring Actuator와 Prometheus 의존성을 추가하여 애플리케이션 모니터링 및 메트릭 수집을 지원하도록 구성했습니다. 의존성을 명확하게 구분하여 추가한 점이 좋습니다.
127-129: Spring Boot 빌드 정보 활성화
springBoot.buildInfo()설정을 추가하여 빌드 메타데이터 정보를 생성하도록 했습니다. 이는 Actuator의 info 엔드포인트를 통해 빌드 정보를 확인할 수 있게 해주어 유용합니다.
| package com.coffee.api.common.aop | ||
|
|
||
| import io.micrometer.core.instrument.MeterRegistry | ||
| import org.aspectj.lang.JoinPoint | ||
| import org.aspectj.lang.annotation.Aspect | ||
| import org.aspectj.lang.annotation.Before | ||
| import org.springframework.stereotype.Component | ||
|
|
||
| @Aspect | ||
| @Component | ||
| class PageViewAspect( | ||
| private val meterRegistry: MeterRegistry | ||
| ) { | ||
|
|
||
| @Before("@annotation(pageViewed)") | ||
| fun countPageView(joinPoint: JoinPoint, pageViewed: PageViewed) { | ||
| val counterName = "page.view.count" | ||
| val tagName = pageViewed.name | ||
|
|
||
| meterRegistry | ||
| .counter(counterName, "page", tagName) | ||
| .increment() | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
페이지 뷰 측정을 위한 AOP 구현
페이지 뷰 메트릭을 수집하기 위한 AOP 클래스를 구현했습니다. @PageViewed 애노테이션이 적용된 메서드 실행 전에 메트릭을 증가시키는 방식으로 구현되었습니다.
하지만 몇 가지 개선할 점이 있습니다:
- 에러 처리 코드가 없어 메트릭 등록 중 오류가 발생해도 이를 처리하는 방법이 없습니다.
- "page.view.count"라는 카운터 이름이 하드코딩되어 있어 향후 변경이 필요할 경우 코드 수정이 필요합니다.
package com.coffee.api.common.aop
import io.micrometer.core.instrument.MeterRegistry
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
+import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
@Aspect
@Component
class PageViewAspect(
- private val meterRegistry: MeterRegistry
+ private val meterRegistry: MeterRegistry,
+ private val counterName: String = "page.view.count"
) {
+ private val logger = LoggerFactory.getLogger(javaClass)
@Before("@annotation(pageViewed)")
fun countPageView(joinPoint: JoinPoint, pageViewed: PageViewed) {
- val counterName = "page.view.count"
val tagName = pageViewed.name
- meterRegistry
- .counter(counterName, "page", tagName)
- .increment()
+ try {
+ meterRegistry
+ .counter(counterName, "page", tagName)
+ .increment()
+ } catch (e: Exception) {
+ logger.error("페이지 뷰 메트릭 증가 중 오류 발생: {}", e.message, e)
+ }
}
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| package com.coffee.api.common.aop | |
| import io.micrometer.core.instrument.MeterRegistry | |
| import org.aspectj.lang.JoinPoint | |
| import org.aspectj.lang.annotation.Aspect | |
| import org.aspectj.lang.annotation.Before | |
| import org.springframework.stereotype.Component | |
| @Aspect | |
| @Component | |
| class PageViewAspect( | |
| private val meterRegistry: MeterRegistry | |
| ) { | |
| @Before("@annotation(pageViewed)") | |
| fun countPageView(joinPoint: JoinPoint, pageViewed: PageViewed) { | |
| val counterName = "page.view.count" | |
| val tagName = pageViewed.name | |
| meterRegistry | |
| .counter(counterName, "page", tagName) | |
| .increment() | |
| } | |
| } | |
| package com.coffee.api.common.aop | |
| import io.micrometer.core.instrument.MeterRegistry | |
| import org.aspectj.lang.JoinPoint | |
| import org.aspectj.lang.annotation.Aspect | |
| import org.aspectj.lang.annotation.Before | |
| import org.slf4j.LoggerFactory | |
| import org.springframework.stereotype.Component | |
| @Aspect | |
| @Component | |
| class PageViewAspect( | |
| private val meterRegistry: MeterRegistry, | |
| private val counterName: String = "page.view.count" | |
| ) { | |
| private val logger = LoggerFactory.getLogger(javaClass) | |
| @Before("@annotation(pageViewed)") | |
| fun countPageView(joinPoint: JoinPoint, pageViewed: PageViewed) { | |
| val tagName = pageViewed.name | |
| try { | |
| meterRegistry | |
| .counter(counterName, "page", tagName) | |
| .increment() | |
| } catch (e: Exception) { | |
| logger.error("페이지 뷰 메트릭 증가 중 오류 발생: {}", e.message, e) | |
| } | |
| } | |
| } |
관련 이슈
close #69
주요 변경 사항
기타
모니터링용 서버를 분리하는게 더 나을지 고민 중입니다.
추후 @PageViewed를 다른 API에도 확장 적용 가능
Summary by CodeRabbit
신규 기능
환경설정
local,prod) 설정 파일이.gitignore에 추가되어 버전 관리에서 제외됩니다.버그 수정