Skip to content

Commit

Permalink
Merge pull request #22 from kagong-sillok/feat/metric
Browse files Browse the repository at this point in the history
feat: 스프링 액츄에이터를 사용한 프로메테우스 메트릭 수집설정, 그라파나 대시보드 구축
  • Loading branch information
yxxnghwan authored Sep 27, 2023
2 parents 5bb469a + 3790eef commit 77de8e6
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 8 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ dependencies {
implementation('com.slack.api:bolt-servlet:1.28.0')
implementation('com.slack.api:bolt-jetty:1.28.0')

implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.kafka:spring-kafka-test'
testCompileOnly 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.prography.kagongsillok.common.config;

import io.micrometer.core.aop.CountedAspect;
import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MicrometerConfig {

@Bean
public TimedAspect timedAspect(final MeterRegistry meterRegistry) {
return new TimedAspect(meterRegistry);
}

@Bean
public CountedAspect countedAspect(final MeterRegistry meterRegistry) {
return new CountedAspect(meterRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
"/admin/**",
"/swagger-ui/**",
"/swagger-ui.**",
"/v3/api-docs/**"
"/v3/api-docs/**",
"/actuator/**"
)
.permitAll()
.anyRequest()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.prography.kagongsillok.member.application;

import lombok.RequiredArgsConstructor;
import org.prography.kagongsillok.auth.infrastructure.JwtAuthTokenProvider;
import org.prography.kagongsillok.common.resolver.dto.LoginMemberDto;
import org.prography.kagongsillok.member.application.dto.MemberDto;
import org.prography.kagongsillok.member.application.exception.NotFoundMemberException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.prography.kagongsillok.place.application;

import io.micrometer.core.annotation.Counted;
import io.micrometer.core.annotation.Timed;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -20,9 +22,11 @@
import org.prography.kagongsillok.review.domain.Review;
import org.prography.kagongsillok.review.domain.ReviewRepository;
import org.prography.kagongsillok.review.domain.ReviewTag;
import org.prography.kagongsillok.review.domain.ReviewTagMapping;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Timed("timer.place")
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand All @@ -40,6 +44,7 @@ public PlaceDto createPlace(final PlaceCreateCommand placeCreateCommand) {
return PlaceDto.of(savedPlace, getImages(savedPlace));
}

@Counted("counter.place")
public PlaceDto getPlace(final Long id) {
final Place place = placeRepository.findById(id)
.orElseThrow(() -> new NotFoundPlaceException(id));
Expand All @@ -50,6 +55,7 @@ public PlaceDto getPlace(final Long id) {
return PlaceDto.of(place, getImages(place));
}

@Counted("counter.place")
public PlaceDto getPlaceWithTags(final Long placeId) {
final Place place = placeRepository.findById(placeId)
.orElseThrow(() -> new NotFoundPlaceException(placeId));
Expand All @@ -66,6 +72,7 @@ public PlaceDto getPlaceWithTags(final Long placeId) {
);
}

@Counted("counter.place")
public List<PlaceDto> searchPlacesLocationAround(final PlaceLocationAroundSearchCondition searchCondition) {
final List<Place> places = placeRepository.findByLocationAround(
Location.of(searchCondition.getLatitude(), searchCondition.getLongitude()),
Expand All @@ -76,6 +83,7 @@ public List<PlaceDto> searchPlacesLocationAround(final PlaceLocationAroundSearch
return getPlaceDtos(places);
}

@Counted("counter.place")
public List<PlaceDto> searchPlacesByName(final String name) {
final List<Place> places = placeRepository.findByNameContains(name);

Expand Down Expand Up @@ -103,12 +111,11 @@ public void deletePlace(final Long id) {
place.delete();
}

@Counted("counter.place")
public List<PlaceDto> searchPlacesByTags(final List<Long> reviewTagIds) {
final List<Review> reviews = reviewRepository.findByReviewTagIds(reviewTagIds);
final List<Long> placeIds = reviews.stream()
.map(review -> review.getPlaceId())
.collect(Collectors.toSet())
.stream()
.map(Review::getPlaceId)
.toList();

final Map<Long, List<Review>> placeIdReviewsMap = reviews.stream()
Expand Down Expand Up @@ -152,7 +159,7 @@ private List<ReviewTag> getReviewTagsRelatedToPlace(final List<Review> reviews)
.flatMap(review -> review.getTagMappings()
.getValues()
.stream()
.map(reviewTagMapping -> reviewTagMapping.getReviewTag())
.map(ReviewTagMapping::getReviewTag)
)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.prography.kagongsillok.record.application;

import io.micrometer.core.annotation.Counted;
import io.micrometer.core.annotation.Timed;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -20,6 +22,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Timed("timer.studyRecord")
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand All @@ -29,6 +32,7 @@ public class StudyRecordService {
private final PlaceRepository placeRepository;
private final ImageRepository imageRepository;

@Counted("counter.studyRecord")
@Transactional
public StudyRecordDto createStudyRecord(final StudyRecordCreateCommand command) {
final Long placeId = command.getPlaceId();
Expand All @@ -42,12 +46,14 @@ public StudyRecordDto createStudyRecord(final StudyRecordCreateCommand command)
return StudyRecordDto.of(savedStudyRecord, getImages(savedStudyRecord));
}

@Counted("counter.studyRecord")
public List<StudyRecordDto> getMemberStudyRecords(final Long memberId) {
final List<StudyRecord> studyRecords = studyRecordRepository.findMemberRecordByMemberId(memberId);

return getStudyRecordDtos(studyRecords);
}

@Counted("counter.studyRecord")
public List<StudyRecordDto> getMemberStudyRecordsByYearMonth(final Long memberId, final int year,
final int month) {
final List<StudyRecord> studyRecords = studyRecordRepository.findMemberRecordByMemberIdAndYearMonth(
Expand All @@ -56,6 +62,7 @@ public List<StudyRecordDto> getMemberStudyRecordsByYearMonth(final Long memberId
return getStudyRecordDtos(studyRecords);
}

@Counted("counter.studyRecord")
public List<PlaceDto> getMemberStudyPlaces(final Long memberId) {
final List<StudyRecord> studyRecords = studyRecordRepository.findMemberRecordByMemberId(memberId);
final List<Long> placeIds = studyRecords
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.prography.kagongsillok.review.application;

import io.micrometer.core.annotation.Counted;
import io.micrometer.core.annotation.Timed;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -26,7 +28,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Timed("timer.review")
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand All @@ -38,6 +40,7 @@ public class ReviewService {
private final ImageRepository imageRepository;
private final PlaceRepository placeRepository;

@Counted("counter.review")
@Transactional
public ReviewDto createReview(final ReviewCreateCommand reviewCreateCommand) {
final Map<Long, ReviewTag> reviewTagIds
Expand Down Expand Up @@ -73,6 +76,7 @@ public ReviewDto getReview(final Long id) {
return ReviewDto.of(review, member, images);
}

@Counted("counter.review")
public List<ReviewDto> getAllReviewsByMemberId(final Long memberId) {
final List<Review> reviews = reviewRepository.findAllByMemberId(memberId);

Expand All @@ -92,6 +96,7 @@ public List<ReviewDto> getAllReviewsByMemberId(final Long memberId) {
return toReviewDtos(reviews, memberMap, placeMap, imageMap, reviewIdImageIdsMap);
}

@Counted("counter.review")
public List<ReviewDto> getAllReviewsByPlaceId(final Long placeId) {
final List<Review> reviews = reviewRepository.findAllByPlaceId(placeId);
final List<Long> reviewedMemberIds = getReviewedMemberIds(reviews);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.prography.kagongsillok.review.application;

import io.micrometer.core.annotation.Timed;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.prography.kagongsillok.common.utils.CustomListUtils;
Expand All @@ -11,6 +12,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Timed("timer.reviewTag")
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand Down
19 changes: 19 additions & 0 deletions src/main/resources/application-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,22 @@ oauth:

server:
host: ${SERVER_HOST}
tomcat:
mbeanregistry:
enabled: true

management:
server:
port: 9292
info:
java:
enabled: true
os:
enabled: true
endpoint:
health:
show-components: ALWAYS
endpoints:
web:
exposure:
include: "*"
18 changes: 18 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,21 @@ server:
host: ${SERVER_HOST}
tomcat:
connection-timeout: 10000
mbeanregistry:
enabled: true

management:
server:
port: 9292
info:
java:
enabled: true
os:
enabled: true
endpoint:
health:
show-components: ALWAYS
endpoints:
web:
exposure:
include: "*"
19 changes: 19 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,22 @@ oauth:

server:
host: 'http://localhost:8080'
tomcat:
mbeanregistry:
enabled: true

management:
server:
port: 9292
info:
java:
enabled: true
os:
enabled: true
endpoint:
health:
show-components: ALWAYS
endpoints:
web:
exposure:
include: "*"

0 comments on commit 77de8e6

Please sign in to comment.