Skip to content

Commit 7698340

Browse files
committed
feat: 프로메테우스 의존성 추가
- 메트릭 데이터 수집을 위한 프로메테우스 의존성 추가 - 관련 actuator 엔드포인트 설정을 yml에 추가 - log 기록을 위한 logback.xml 추가 - aop를 이용한 로그 기록을 위해 loggingAspect 클래스 추가
1 parent c2d013a commit 7698340

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

build.gradle

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ dependencies {
6565
compileOnly 'org.projectlombok:lombok'
6666
annotationProcessor 'org.projectlombok:lombok'
6767
implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.6.0'
68+
69+
// Monitoring
6870
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '3.3.3'
71+
implementation 'io.micrometer:micrometer-registry-prometheus'
6972

7073
//elastic-search
7174
implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
@@ -124,7 +127,8 @@ def jacocoExcludePatterns = [
124127
'**/controller/**',
125128
'**/fixture/*',
126129
'**/*Factory*',
127-
'**/event/**'
130+
'**/event/**',
131+
'**/*Aspect*'
128132
]
129133

130134
def jacocoExcludePatternsForVerify = [
@@ -142,7 +146,8 @@ def jacocoExcludePatternsForVerify = [
142146
'*.controller.*',
143147
'*.fixture.*',
144148
'*.*Factory*',
145-
'*.*event*.*'
149+
'*.*event*.*',
150+
'*.*Aspect*'
146151
]
147152

148153
jacocoTestReport {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.somemore.global.aspect;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.aspectj.lang.ProceedingJoinPoint;
6+
import org.aspectj.lang.annotation.Around;
7+
import org.aspectj.lang.annotation.Aspect;
8+
import org.aspectj.lang.annotation.Pointcut;
9+
import org.springframework.stereotype.Component;
10+
11+
@Slf4j
12+
@Aspect
13+
@Component
14+
public class LoggingAspect {
15+
16+
private final ObjectMapper objectMapper = new ObjectMapper();
17+
18+
@Pointcut("execution(* com.somemore.domains.*.controller..*.*(..))")
19+
private void pointCut(){}
20+
21+
@Around("pointCut()")
22+
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
23+
24+
String methodName = joinPoint.getSignature().toShortString();
25+
String args = convertArgsToJson(joinPoint.getArgs());
26+
log.info("엔드포인트 호출: {} \n- 파라미터: {}", methodName, args);
27+
28+
long startTime = System.currentTimeMillis();
29+
30+
try {
31+
Object result = joinPoint.proceed();
32+
long elapsedTime = System.currentTimeMillis() - startTime;
33+
34+
log.debug("성공: {} \n- 응답: {} \n- 실행 시간: {}ms",
35+
methodName,
36+
convertResultToJson(result),
37+
elapsedTime);
38+
39+
return result;
40+
} catch (Exception e) {
41+
long elapsedTime = System.currentTimeMillis() - startTime;
42+
43+
log.warn("에러 발생: {} \n- 에러 타입: {} \n- 에러 메세지: {} \n- 실행 시간: {}ms",
44+
methodName,
45+
e.getClass().getSimpleName(),
46+
e.getMessage(),
47+
elapsedTime);
48+
49+
throw e;
50+
}
51+
}
52+
53+
private String convertArgsToJson(Object[] args) {
54+
try {
55+
return objectMapper.writeValueAsString(args != null ? args : new Object[]{});
56+
} catch (Exception e) {
57+
log.warn("파라미터 변환 실패", e);
58+
return "[파라미터 변환 실패]";
59+
}
60+
}
61+
62+
private String convertResultToJson(Object result) {
63+
try {
64+
return objectMapper.writeValueAsString(result != null ? result : "null");
65+
} catch (Exception e) {
66+
log.warn("응답 변환 실패", e);
67+
return "[응답 변환 실패]";
68+
}
69+
}
70+
}

src/main/resources/application.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ spring:
3232
hibernate:
3333
format_sql: true
3434
highlight_sql: true
35-
generate_statistics: true
35+
generate_statistics: false
3636
show-sql: true
3737

3838
data:
@@ -125,3 +125,12 @@ management:
125125
health:
126126
elasticsearch:
127127
enabled: false
128+
129+
endpoints:
130+
web:
131+
exposure:
132+
include: prometheus
133+
134+
endpoint:
135+
prometheus:
136+
enabled: true
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<configuration>
2+
<springProfile name="default">
3+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
6+
<charset>UTF-8</charset>
7+
</encoder>
8+
</appender>
9+
10+
<appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
11+
<appender-ref ref="FILE" />
12+
<queueSize>5000</queueSize>
13+
<discardingThreshold>0</discardingThreshold>
14+
</appender>
15+
16+
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
17+
<file>${LOG_DIR:-logs}/application.log</file>
18+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
19+
<fileNamePattern>${LOG_DIR:-logs}/application.%d{yyyy-MM-dd}.log</fileNamePattern>
20+
<maxHistory>30</maxHistory>
21+
</rollingPolicy>
22+
<encoder>
23+
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
24+
<charset>UTF-8</charset>
25+
</encoder>
26+
</appender>
27+
28+
<logger name="com.somemore" level="DEBUG" additivity="false">
29+
<appender-ref ref="CONSOLE" />
30+
<appender-ref ref="ASYNC_FILE" />
31+
</logger>
32+
33+
<root level="INFO">
34+
<appender-ref ref="CONSOLE" />
35+
<appender-ref ref="ASYNC_FILE" />
36+
</root>
37+
</springProfile>
38+
</configuration>

0 commit comments

Comments
 (0)