Skip to content

Commit

Permalink
Merge pull request #32 from k-twitter/feat/#29-ELK
Browse files Browse the repository at this point in the history
[로그] ELK 도입
  • Loading branch information
jojaeng2 authored Oct 14, 2023
2 parents 465605c + 174fd59 commit b99885a
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 3 deletions.
3 changes: 3 additions & 0 deletions sns_service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("io.micrometer:micrometer-registry-prometheus")
implementation ("net.logstash.logback:logstash-logback-encoder:7.3")
implementation ("io.github.microutils:kotlin-logging:3.0.5")

compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
Expand Down
44 changes: 44 additions & 0 deletions sns_service/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,44 @@ services:
MYSQL_ROOT_PASSWORD: 1234
command: [ '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci' ]

logstash:
image: "docker.elastic.co/logstash/logstash:8.10.2"
container_name: sns-logstash
ports:
- "5000:5000"
volumes:
- ./infra/logstash/pipeline:/usr/share/logstash/pipeline
- ./infra/logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml
depends_on:
- elasticsearch

elasticsearch:
image: "docker.elastic.co/elasticsearch/elasticsearch:8.10.2"
container_name: sns-elasticsearch
ports:
- "9200:9200"
- "9300:9300"
environment:
discovery.type: single-node
deploy:
resources:
limits:
memory: 4GB
volumes:
- elasticsearch-volume:/usr/share/elasticsearch/data

kibana:
image: "docker.elastic.co/kibana/kibana:8.10.2"
container_name: sns-kibana
ports:
- "5601:5601"
depends_on:
- elasticsearch
volumes:
- kibana-volume:/usr/share/kibana/data
- ./infra/kibana/config/kibana.yaml:/usr/share/kibana/config/kibana.yml


prometheus:
image: "prom/prometheus:v2.47.0"
container_name: prometheus
Expand All @@ -34,3 +72,9 @@ services:
volumes:
- ./grafana/volume:/var/lib/grafana
restart: always

volumes:
elasticsearch-volume:
external: false
kibana-volume:
external: false
14 changes: 14 additions & 0 deletions sns_service/infra/kibana/config/kibana.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
## Default Kibana configuration from Kibana base image.
## https://github.com/elastic/kibana/blob/master/src/dev/build/tasks/os_packages/docker_generator/templates/kibana_yml.template.ts
#
server.name: kibana
server.host: "0.0.0.0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
monitoring.ui.container.elasticsearch.enabled: true

## X-Pack security credentials
#
elasticsearch.username: kibana_system
elasticsearch.password: NDwqpPLYFycZkaZOWrWX
9 changes: 9 additions & 0 deletions sns_service/infra/logstash/config/logstash.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
http.host: "0.0.0.0"
xpack.monitoring.elasticsearch.hosts: [ "http://elasticsearch:9200" ]

## X-Pack security credentials
#
xpack.monitoring.enabled: true
xpack.monitoring.elasticsearch.username: elastic
xpack.monitoring.elasticsearch.password: LhgKhY3ii22buWAiOMas
17 changes: 17 additions & 0 deletions sns_service/infra/logstash/pipeline/logstash.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
input {
tcp {
port => 5000
codec => json_lines
}
}

output {
stdout{}
elasticsearch {
# TODO 각자의 서버에 맞게 IP 변경
hosts => "elasticsearch:9200"
user => "elastic"
password => "LhgKhY3ii22buWAiOMas"
index => "logstash-%{+YYYY.MM.dd}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ import joryu.sns_service.profile.dto.request.ProfileUpdateRequest
import joryu.sns_service.profile.dto.response.AllProfileResponse
import joryu.sns_service.profile.dto.response.ProfileInfoResponse
import joryu.sns_service.profile.service.ProfileService
import lombok.RequiredArgsConstructor
import mu.KLogger
import mu.KotlinLogging
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/profile")
@RequiredArgsConstructor
class ProfileApiController(
private val profileService: ProfileService
private val profileService: ProfileService,
) {
val logger: KLogger = KotlinLogging.logger {}

@PostMapping()
fun createProfile(@RequestBody profileCreateRequest: ProfileCreateRequest): ResponseEntity<ProfileInfoResponse> {
val profileInfo = profileService.create(profileCreateRequest)
logger.info { "Hello World" }
logger.error { "Error" }
logger.warn { "Warn" }
return ResponseEntity(profileInfo, HttpStatus.OK)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import joryu.sns_service.profile.exception.ProfileBaseException
import joryu.sns_service.profile.exception.ProfileExceptionEnums
import joryu.sns_service.profile.repository.ProfileRepository
import lombok.RequiredArgsConstructor
import mu.KLogger
import mu.KotlinLogging
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand All @@ -19,6 +21,8 @@ class ProfileService(
private val profileRepository: ProfileRepository,
private val followRepository: FollowRepository
) {
val log: KLogger = KotlinLogging.logger {}


@Transactional
fun create(profileCreateRequest: ProfileCreateRequest): ProfileInfoResponse {
Expand Down
22 changes: 22 additions & 0 deletions sns_service/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="30 seconds">

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}:%-4relative] [%thread] %highlight(%-5level) %boldGreen([%C.%M:%boldRed(%L)]) - %boldYellow(%msg%n)</pattern>
</encoder>
</appender>

<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>localhost:5000</destination>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
</encoder>
</appender>


<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="LOGSTASH"/>
</root>
</configuration>

0 comments on commit b99885a

Please sign in to comment.