Skip to content

Commit 5cfc289

Browse files
authored
Merge pull request #188 from DevKor-github/feat/metric
Feat: cloudwatch api별 요청 횟수 지표 추가
2 parents da6dd4b + 043eceb commit 5cfc289

File tree

6 files changed

+166
-12
lines changed

6 files changed

+166
-12
lines changed

build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ dependencies {
9999
// actuator
100100
implementation 'org.springframework.boot:spring-boot-starter-actuator'
101101

102-
// prometheus
103-
implementation 'io.micrometer:micrometer-registry-prometheus'
102+
// cloudwatch
103+
implementation 'io.micrometer:micrometer-registry-cloudwatch2'
104+
implementation 'software.amazon.awssdk:cloudwatch'
104105
}
105106
dependencyManagement {
106107
imports {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package devkor.com.teamcback.global.config;
2+
3+
import devkor.com.teamcback.infra.cloudwatch.MetricsInterceptor;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8+
9+
@Configuration
10+
public class WebConfig implements WebMvcConfigurer {
11+
@Autowired
12+
private MetricsInterceptor metricsInterceptor;
13+
14+
@Override
15+
public void addInterceptors(InterceptorRegistry registry) {
16+
registry.addInterceptor(metricsInterceptor);
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package devkor.com.teamcback.infra.cloudwatch;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import software.amazon.awssdk.regions.Region;
6+
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient;
7+
8+
@Configuration
9+
public class CloudWatchConfig {
10+
11+
@Bean
12+
public CloudWatchAsyncClient cloudWatchAsyncClient() {
13+
return CloudWatchAsyncClient
14+
.builder()
15+
.region(Region.AP_NORTHEAST_2)
16+
.build();
17+
}
18+
}
19+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package devkor.com.teamcback.infra.cloudwatch;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.stereotype.Component;
5+
import jakarta.servlet.http.HttpServletRequest;
6+
import jakarta.servlet.http.HttpServletResponse;
7+
import org.springframework.web.method.HandlerMethod;
8+
import org.springframework.web.servlet.HandlerInterceptor;
9+
import org.springframework.web.servlet.HandlerMapping;
10+
11+
import java.util.List;
12+
13+
@Component
14+
@RequiredArgsConstructor
15+
public class MetricsInterceptor implements HandlerInterceptor {
16+
17+
private final MetricsService metricsService;
18+
19+
@Override
20+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
21+
if(handler instanceof HandlerMethod) {
22+
String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
23+
if(shouldTrack(pattern)) {
24+
metricsService.recordApiRequest(pattern);
25+
}
26+
return true;
27+
}
28+
return true;
29+
}
30+
31+
private boolean shouldTrack(String uri) {
32+
return List.of(
33+
"/api/bookmarks",
34+
"/api/categories",
35+
"/api/routes",
36+
"/api/users/login",
37+
"/api/users/login/release",
38+
"/api/users/mypage",
39+
"/api/search",
40+
"/api/search/buildings",
41+
"/api/search/buildings/{buildingId}",
42+
"/api/search/buildings/{buildingId}/facilities",
43+
"/api/search/facilities",
44+
"/api/search/place/{placeId}"
45+
).contains(uri);
46+
}
47+
}
48+
49+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package devkor.com.teamcback.infra.cloudwatch;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.scheduling.annotation.Scheduled;
6+
import org.springframework.stereotype.Service;
7+
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient;
8+
import software.amazon.awssdk.services.cloudwatch.model.*;
9+
import software.amazon.awssdk.services.cloudwatch.model.Dimension;
10+
11+
import java.time.Instant;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.Objects;
15+
import java.util.concurrent.ConcurrentHashMap;
16+
import java.util.concurrent.atomic.AtomicInteger;
17+
18+
@Service
19+
@RequiredArgsConstructor
20+
public class MetricsService {
21+
@Value("${metrics.environment}")
22+
private String environment;
23+
24+
private final String TARGET = "dev"; // 테스트 후 prod로 변경
25+
26+
private final CloudWatchAsyncClient cloudWatchAsyncClient;
27+
private final Map<String, AtomicInteger> uriCountMap = new ConcurrentHashMap<>();
28+
29+
public void recordApiRequest(String uri) {
30+
if(TARGET.equalsIgnoreCase(environment)) {
31+
uriCountMap.computeIfAbsent(uri, k -> new AtomicInteger(0)).incrementAndGet();
32+
}
33+
}
34+
35+
@Scheduled(fixedRate = 60_000)
36+
public void sendMetricsToCloudWatch() {
37+
if (TARGET.equalsIgnoreCase(environment)) {
38+
List<MetricDatum> metricDataList = uriCountMap.entrySet().stream()
39+
.map(entry -> {
40+
String uri = entry.getKey();
41+
int count = entry.getValue().getAndSet(0);
42+
43+
if (count > 0) {
44+
return MetricDatum.builder()
45+
.metricName("ApiRequestCount")
46+
.dimensions(
47+
Dimension.builder().name("URI").value(uri).build()
48+
)
49+
.unit(StandardUnit.COUNT)
50+
.value((double) count)
51+
.timestamp(Instant.now())
52+
.build();
53+
}
54+
return null;
55+
})
56+
.filter(Objects::nonNull)
57+
.toList();
58+
59+
if (!metricDataList.isEmpty()) {
60+
PutMetricDataRequest request = PutMetricDataRequest.builder()
61+
.namespace("Kodaero/Metrics")
62+
.metricData(metricDataList)
63+
.build();
64+
65+
cloudWatchAsyncClient.putMetricData(request).whenComplete((resp, err) -> {
66+
if (err != null) {
67+
System.err.println("Failed to send metric: " + err.getMessage());
68+
}
69+
});
70+
}
71+
}
72+
}
73+
}

src/main/resources/application.yml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@ spring:
3030
port: 6379
3131
password: ${REDIS_PASSWORD}
3232

33-
management:
34-
endpoints:
35-
web:
36-
exposure:
37-
include: prometheus, health, info, metrics
38-
prometheus:
39-
metrics:
40-
export:
41-
enabled: true
4233

4334
logging:
4435
level:
@@ -111,4 +102,7 @@ date:
111102
holiday:
112103
end-point: ${HOLIDAY_API_END_POINT}
113104
encoded-key: ${HOLIDAY_API_ENCODED_KEY}
114-
decoded-key: ${HOLIDAY_API_DECODED_KEY}
105+
decoded-key: ${HOLIDAY_API_DECODED_KEY}
106+
107+
metrics:
108+
environment: dev

0 commit comments

Comments
 (0)