From 2a453f77d6fe4bfdb3667ad393f295bb13fab036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=ED=95=B8=EB=AA=A8?= Date: Sun, 24 Sep 2023 18:12:27 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=94=84=EB=A1=9C=EB=A9=94=ED=85=8C?= =?UTF-8?q?=EC=9A=B0=EC=8A=A4,=20=EA=B7=B8=EB=9D=BC=ED=8C=8C=EB=82=98=20?= =?UTF-8?q?=EC=BB=B4=ED=8F=AC=EC=A6=88=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sns_service/.gitignore | 7 +++ sns_service/build.gradle.kts | 46 ++++++++++--------- sns_service/docker-compose.yaml | 24 ++++++++++ sns_service/prometheus/config/prometheus.yaml | 24 ++++++++++ sns_service/prometheus/config/rule.yaml | 18 ++++++++ .../src/main/resources/application.yaml | 6 +++ 6 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 sns_service/prometheus/config/prometheus.yaml create mode 100644 sns_service/prometheus/config/rule.yaml diff --git a/sns_service/.gitignore b/sns_service/.gitignore index e7900dd..ef6ef2a 100644 --- a/sns_service/.gitignore +++ b/sns_service/.gitignore @@ -46,3 +46,10 @@ out/ # Ignore redis binary dump (dump.rdb) files *.rdb + +### prometheus ### +prometheus/config/**.log +prometheus/volume/data/ + +### grafana ### +grafana/ diff --git a/sns_service/build.gradle.kts b/sns_service/build.gradle.kts index 0a87005..d16f435 100644 --- a/sns_service/build.gradle.kts +++ b/sns_service/build.gradle.kts @@ -1,47 +1,49 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - id("org.springframework.boot") version "3.1.2" - id("io.spring.dependency-management") version "1.1.2" - kotlin("jvm") version "1.8.22" - kotlin("plugin.spring") version "1.8.22" + id("org.springframework.boot") version "3.1.2" + id("io.spring.dependency-management") version "1.1.2" + kotlin("jvm") version "1.8.22" + kotlin("plugin.spring") version "1.8.22" } group = "2joryu" version = "0.0.1-SNAPSHOT" java { - sourceCompatibility = JavaVersion.VERSION_17 + sourceCompatibility = JavaVersion.VERSION_17 } configurations { - compileOnly { - extendsFrom(configurations.annotationProcessor.get()) - } + compileOnly { + extendsFrom(configurations.annotationProcessor.get()) + } } repositories { - mavenCentral() + mavenCentral() } dependencies { - implementation("org.springframework.boot:spring-boot-starter-web") - implementation("com.fasterxml.jackson.module:jackson-module-kotlin") - implementation("org.jetbrains.kotlin:kotlin-reflect") - implementation ("org.springframework.boot:spring-boot-starter-data-jpa") - compileOnly("org.projectlombok:lombok") - annotationProcessor("org.projectlombok:lombok") - testImplementation("org.springframework.boot:spring-boot-starter-test") - runtimeOnly("com.mysql:mysql-connector-j") + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin") + implementation("org.jetbrains.kotlin:kotlin-reflect") + implementation("org.springframework.boot:spring-boot-starter-data-jpa") + implementation("org.springframework.boot:spring-boot-starter-actuator") + implementation("io.micrometer:micrometer-registry-prometheus") + compileOnly("org.projectlombok:lombok") + annotationProcessor("org.projectlombok:lombok") + testImplementation("org.springframework.boot:spring-boot-starter-test") + runtimeOnly("com.mysql:mysql-connector-j") } tasks.withType { - kotlinOptions { - freeCompilerArgs += "-Xjsr305=strict" - jvmTarget = "17" - } + kotlinOptions { + freeCompilerArgs += "-Xjsr305=strict" + jvmTarget = "17" + } } tasks.withType { - useJUnitPlatform() + useJUnitPlatform() } diff --git a/sns_service/docker-compose.yaml b/sns_service/docker-compose.yaml index 12ee62a..628e570 100644 --- a/sns_service/docker-compose.yaml +++ b/sns_service/docker-compose.yaml @@ -10,3 +10,27 @@ services: MYSQL_DATABASE: sns-db MYSQL_ROOT_PASSWORD: 1234 command: [ '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci' ] + + prometheus: + image: "prom/prometheus:v2.47.0" + container_name: prometheus + volumes: + - ./prometheus/config:/etc/prometheus + - ./prometheus/volume:/prometheus + ports: + - "9091:9090" + command: + - '--web.enable-lifecycle' # api 재시작없이 설정파일들을 reload 할 수 있게 해줌 + - '--config.file=/etc/prometheus/prometheus.yaml' + - '--web.console.libraries=/etc/prometheus/console_libraries' + - '--web.console.templates=/etc/prometheus/consoles' + restart: always + + grafana: + image: "grafana/grafana:9.4.7" + container_name: grafana + ports: + - "3001:3000" + volumes: + - ./grafana/volume:/var/lib/grafana + restart: always diff --git a/sns_service/prometheus/config/prometheus.yaml b/sns_service/prometheus/config/prometheus.yaml new file mode 100644 index 0000000..d7d1cd0 --- /dev/null +++ b/sns_service/prometheus/config/prometheus.yaml @@ -0,0 +1,24 @@ +global: + scrape_interval: 15s # scrap target의 기본 interval을 15초로 변경 / default = 1m + scrape_timeout: 15s # scrap request 가 timeout 나는 길이 / default = 10s + evaluation_interval: 2m # rule 을 얼마나 빈번하게 검증하는지 / default = 1m + + external_labels: + monitor: 'ktwitter-monitor' # 기본적으로 붙여줄 라벨 + query_log_file: query_log_file.log # prometheus의 쿼리 로그들을 기록, 없으면 기록안함 + +rule_files: + - "rule.yml" # 파일 위치는 prometheus.yaml 이 있는 곳과 동일 위치 + +scrape_configs: + - job_name: "prometheus" + static_configs: + - targets: + - "localhost:9090" + - job_name: "springboot" + metrics_path: "/actuator/prometheus" + scheme: 'http' + scrape_interval: 5s + static_configs: + - targets: + - "localhost:8080" diff --git a/sns_service/prometheus/config/rule.yaml b/sns_service/prometheus/config/rule.yaml new file mode 100644 index 0000000..8742c05 --- /dev/null +++ b/sns_service/prometheus/config/rule.yaml @@ -0,0 +1,18 @@ +groups: + - name: ktwitter + rules: + - alert: InstanceDown + expr: up == 0 + for: 5m + labels: + severity: page + annotations: + summary: "Instance {{ $labels.instance }} down" + description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes." + + - alert: APIHighRequestLatency + expr: api_http_request_latencies_second{quantile="0.5"} > 1 + for: 10m + annotations: + summary: "High request latency on {{ $labels.instance }}" + description: "{{ $labels.instance }} has a median request latency above 1s (current value: {{ $value }}s)" diff --git a/sns_service/src/main/resources/application.yaml b/sns_service/src/main/resources/application.yaml index 2de5bc9..32c79a0 100644 --- a/sns_service/src/main/resources/application.yaml +++ b/sns_service/src/main/resources/application.yaml @@ -13,3 +13,9 @@ spring: show_sql: true format_sql: true dialect: org.hibernate.dialect.MySQL8Dialect + +management: + endpoints: + web: + exposure: + include: "prometheus"