
์๋น์ค๋ฅผ ์ด์ํ ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ CPU, ๋ฉ๋ชจ๋ฆฌ, ์ปค๋ฅ์ ์ฌ์ฉ, ๊ณ ๊ฐ ์์ฒญ์์ ๊น์ ์ ๋ง์ ์งํ๋ฅผ ํ์ธํ๋ ๊ฒ์ด ๋งค์ฐ ์ค์ํ๋ค. ๊ทธ๋์ผ ์ด๋์ ์ด๋ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋์ง ์ฌ์ ์ ๋์ฒ๊ฐ ๊ฐ๋ฅํ๊ณ , ๋ฌธ์ ํ์ ์ ์ฝ๊ฒ ํ ์์๋ค. ์๋ฅผ ๋ค์ด ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๊ฐ๋ ์ฐผ๋ค๋ฉด, ๋ฉ๋ชจ๋ฆฌ ๋ฌธ์ ์ ๊ด๋ จ์๋ ๊ณณ์ ๋น ๋ฅด๊ฒ ์ฐพ์์ ๋์ ๊ฐ๋ฅํ๋ค.
์ธ์์๋ ์ ๋ง์ ๋ชจ๋ํฐ๋ง ํด(๊ทธ๋ผํ๋, ํํฌ์ธํธ)์ด ์กด์ฌํ๋ค. ํ์ง๋ง ์ค๊ฐ์ ์ฌ์ฉํ๋ ๋ชจ๋ํฐ๋ง ํด์ ๋ณ๊ฒฝํ๋ฉด ์๋นํ ๊ฐ๋ฐ์ ์ ์ฅ์์๋ ๋ถํธํด์ง๋ค. ๋ฐ๋ผ์ ์ด๋ฐ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ฒ์ด ๋ฐ๋ก ๋ง์ดํฌ๋ก๋ฏธํฐ(Micrometer)๋ค.

๋ง์ดํฌ๋ก๋ฏธํฐ๊ฐ ์ง์ํ๋ ๋ชจ๋ํฐ๋ง ํด
AppOptics, Atlas, CloudWatch, Datadog, Dynatrace, Elastic, Ganglia, Graphite, Humio, Influx, Instana, JMX, KairosDB, New Relic, Prometheus, SignalFx, Stackdriver, StatsD, Wavefront
@AutoConfiguration ๋ฅผ ํตํด ์๋ ๋ฑ๋ก ํด์ค๋ค.metrics ์๋ํฌ์ธํธ๋ฅผ ์ฌ์ฉํ๋ฉด ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณต๋๋ ๋ฉํธ๋ฆญ๋ค์ ํ์ธ ๊ฐ๋ฅhttp://localhost:8080/actuator/metricsmetrics ์๋ํฌ์ธํธ๋ ๋ค์๊ณผ ๊ฐ์ ํจํด์ ์ฌ์ฉํด์ ๋ ์์ธํ ํ์ธ ๊ฐ๋ฅhttp://localhost:8080/actuator/metrics/{name}๐ค ์ฌ์ฉ์ ์ ์ ๋ฉํธ๋ฆญ
์ฌ์ฉ์๊ฐ ์ง์ ๋ฉํธ๋ฆญ์ ์ ์ ํ ์๋ ์๋ค. ์๋ฅผ ๋ค์ด ์ฃผ๋ฌธ์, ์ทจ์์๋ฅผ ๋ฉํธ๋ฆญ์ผ๋ก ๋ง๋ค ์ ์๋ค.
์ ํ๋ฆฌ์ผ์ด์ ์์ ๋ฐ์ํ ๋ฉํธ๋ฆญ์ ๊ทธ ์๊ฐ๋ง ํ์ธํ๋ ๊ฒ์ด ์๋๋ผ ๊ณผ๊ฑฐ ์ด๋ ฅ๊น์ง ํจ๊ป ํ์ธํ๋ ค๋ฉด ๋ฉํธ๋ฆญ์ ๋ณด๊ดํ๋ DB๊ฐ ํ์ํ๋ค. ์ด๋ ๊ฒ ํ๋ ค๋ฉด ์ด๋์ ๊ฐ ๋ฉํธ๋ฆญ์ ์ง์ํด์ ์์งํ๊ณ DB์ ์ ์ฅํด์ผํ๋ค. ์ด ์ญํ ์ ํ๋ก๋ฉํ ์ฐ์ค(Prometheus)๊ฐ ์ํ ํ๋ค.

์ค์น
https://github.com/prometheus/prometheus/releases/download/v2.42.0/
ํ๋ก๋ฉํ ์ฐ์ค๊ฐ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฉํธ๋ฆญ์ ๊ฐ์ ธ๊ฐ๋ ค๋ฉด ํ๋ก๋ฉํ ์ฐ์ค๊ฐ ์ฌ์ฉํ๋ ํฌ์ผ์ ๋ง์ถ์ด ๋ฉํธ๋ฆญ์ ๋ง๋ค์ด์ผ ํ๋ค. ์ฐธ๊ณ ๋ก ํ๋ก๋ฉํ ์ฐ์ค๋ ํฌ๋ฉง(JSON)์ ์ดํดํ์ง ๋ชปํ๋ค. ํ์ง๋ง ๋ง์ดํฌ๋ก๋ฏธํฐ๊ฐ ์ด๋ฅผ ํด๊ฒฐํด์ค๋ค.
build.gradle
implementation 'io.micrometer:micrometer-registry-prometheus'
/actuator/prometheus -> ์๋์ผ๋ก ์ถ๊ฐhttp://localhost:8080/actuator/prometheusprometheus.yml ํ์ผ ์์ , #์ถ๊ฐ ๋ถ๋ถ๋ง ์์ฑํ๋ฉด ๋๋ค. ๋๋จธ์ง๋ ์กด์ฌํ ๊ฒ์ด๋ค.prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
rule_files:
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
# ์ถ๊ฐ
- job_name: "spring-actuator"
metrics_path: '/actuator/prometheus'
scrape_interval: 1s
static_configs:
- targets: ['localhost:8080']
http://localhost:8080/actuator/prometheus๋ฅผ 1์ด์ฉ ํธ์ถํ์ฌ ์ ํ๋ฆฌ์ผ์ด์
์ ๋ฉํธ๋ฆญ์ ์์งํ๋ค.http://localhost:9090 ๋ฅผ ํตํด ํ๋ก๋ฉํ
์ฐ์ค ์๋ ํ์ธ
prometheus.yml ํ์ธStatus -> Configuration Status -> Targets

State๊ฐ UP์ผ๋ก ๋์ด ์์ผ๋ฉด ์ ์์ด๊ณ , DOWN ์ผ๋ก ๋์ด ์์ผ๋ฉด ์ฐ๋ ์ฑ๊ณต
prometheus : ํ๋ก๋ฉํ
์ฐ์ค ์์ฒด์์ ์ ๊ณตํ๋ ๋ฉํธ๋ฆญ ์ ๋ณด
spring-actuator : ์ฐ๋ฆฌ๊ฐ ์ฐ๋ํ ์ ํ๋ฆฌ์ผ์ด์
์ ๋ฉํธ๋ฆญ ์ ๋ณด
โ๏ธ ๋ฐ๋์ ์๋ฒ๊ฐ ๋์๊ฐ์ผ์ง ์ฐ๋์ด ๋๋ค.
jvm_info

http_server_requests_seconds_count

๋์ ์ซ์๋ ํด๋น ๋ฉํธ๋ฆญ ๊ฐ, uri="/actuator/prometheus" ๋ 1์ด๋ง๋ค ์๋์ผ๋ก ํธ์ถํด์ ์ซ์๊ฐ ํฐ ๊ฒ์ ํ์ธ ๊ฐ๋ฅํ๋ค.
Table -> Evaluation time ์ ์์ ํด์ ๊ณผ๊ฑฐ ์๊ฐ ์กฐํ ๊ฐ๋ฅ
Graph : ๋ฉํธ๋ฆญ์ ๊ทธ๋ํ๋ก ์กฐํ ๊ฐ๋ฅ
= : ์ ๊ณต๋ ๋ฌธ์์ด๊ณผ ์ ํํ ๋์ผํ ๋ ์ด๋ธ ์ ํ!= : ์ ๊ณต๋ ๋ฌธ์์ด๊ณผ ๊ฐ์ง ์์ ๋ ์ด๋ธ ์ ํ=~ : ์ ๊ณต๋ ๋ฌธ์์ด๊ณผ ์ ๊ท์ ์ผ์นํ๋ ๋ ์ด๋ธ ์ ํ!~ : ์ ๊ณต๋ ๋ฌธ์์ด๊ณผ ์ ๊ท์ ์ผ์นํ์ง ์๋ ๋ ์ด๋ธ ์ ํuri=/log , method=GET ์กฐ๊ฑด์ผ๋ก ํํฐ http_server_requests_seconds_count{uri="/log", method="GET"}system_cpu_usage

http_server_requests_seconds_count{uri="/log"}

๐ค ์ฆ๊ฐ๋ง ํ๋ ๊ทธ๋ํ์์๋ ํน์ ์๊ฐ์ ์ผ๋ง๋ ๊ณ ๊ฐ์ ์์ฒญ์ด ๋ค์ด์๋์ง ํ๋์ ํ์ธํ๊ธฐ ๋งค์ฐ ์ด๋ ต๋ค. ์ด๋ฐ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด
increase(),rate()๊ฐ์ ํจ์๋ฅผ ์ง์๋ฅผ ์ง์ํ๋ค.
increase(http_server_requests_seconds_count{uri="/log"}[1m])

rate(http_server_requests_seconds_count{uri="/log"}[1m])
๋๋ฌด ๋ณต์กํ๊ฒ ์๊ฐํ๊ธฐ ๋ณด๋ค๋ ์ด๋น ์ผ๋ง๋ ์ฆ๊ฐํ๋์ง ๋ํ๋ด๋ ์งํ๋ก ๋ณด๋ฉด ๋๋ค.

https://grafana.com/grafana/download
๊ทธ๋ผํ๋๋ฅผ ์ค์นํ ํhttp://localhost:3000์ผ๋ก ํ์ธํด๋ณด์
๋ก๊ทธ์ธ ํ๋ฉด์ด ๋์ค๋ฉด
email or username : admin
Password : admin
์ผ๋ก ํ๊ณ ๊ทธ ๋ค์์๋ SKIP์ ํ์

์ด๋ฐ ํ๋ฉด์ด ๋์์ผํ๋ค.
Add data source ๋ฅผ ์ ํํ๋ค.Prometheus๋ฅผ ์ ํํ๋ค.โ๏ธ ๋ฐ๋์ ํ๋ก๋ฉํ ์ฐ์ค, ๊ทธ๋ผํ๋, ์ ํ๋ฆฌ์ผ์ด์ 3๊ฐ๊ฐ ๋ค ์ผ์ ธ์์ด์ผํ๋ค.
Save & test ๋ฅผ ์ ํ

๋์๋ณด๋ ์ ์ฅ
New ๋ฒํผ ์ ํ New Dashboard ์ ํSave dashboard ์ ์ฅ ๋ฒํผ(disk ๋ชจ์) ์ ํ๋์๋ณด๋ ํ์ธ
๋์๋ณด๋์ ํจ๋ ๋ง๋ค๊ธฐ
์ค๋ฅธ์ชฝ ์๋จ์ Add panel ๋ฒํผ(์ฐจํธ ๋ชจ์) ์ ํ

Add a new panel ๋ฉ๋ด ์ ํ
ํจ๋์ ์ ๋ณด๋ฅผ ์ ๋ ฅํ ์ ์๋ ํ๋ฉด์ด ๋ํ๋๋ค.
์๋์ ๋ณด๋ฉด Run queries ๋ฒํผ ์ค๋ฅธ์ชฝ์ Builder , Code ๋ผ๋ ๋ฒํผ์ด ๋ณด์ด๋๋ฐ, Code ๋ฅผ ์ ํ
Enter a PromQL query... ์ด๋ผ๋ ๋ถ๋ถ์ ๋ฉํธ๋ฆญ์ ์ ๋ ฅํ๋ฉด ๋๋ค.
system_cpu_usage : ์์คํ
์ CPU ์ฌ์ฉ๋process_cpu_usage : JVM ํ๋ก์ธ์ค CPU ์ฌ์ฉ๋PromQL ์ system_cpu_usage ๋ฅผ ์
๋ ฅํ๊ณ Run queries ํด๋ฆญ
+ Query ๋ฒํผ๋ฅผ ํตํด process_cpu_usage ์ถ๊ฐ

Options -> Legend ๋ฅผ ์ ํํ๊ณ Custom์ ์ ํ -> Cpu ์ฌ์ฉ๋ ๋ฅผ ์
๋ ฅ ์ค๋ฅธ์ชฝ์ ๋ณด๋ฉด Panel options -> Title ์์
Standard options -> Unit-> Data -> bytes(SI) ๋ฅผ ์ ํStandard options -> Min- > 0 ์ ์ ํhttps://grafana.com/grafana/dashboards/11378-justai-system-monitor/
Copy Id to clipboard ๋ฅผ ํตํด id ์นดํผ๋์๋ณด๋ ๋ถ๋ฌ์ค๊ธฐ
New ๋ฒํผ ์ ํ -> Import ์ ํ11378)๋ฅผ ์
๋ ฅํ๊ณ Load ๋ฒํผ ์ ํImport ๋ฒํผ ์ ํ
Make editable ์ ํTitle์ Tomcat Statistics ๋ก ์์
Thread Config Max ํจ๋ ์ค์ ๋ณ๊ฒฝ
jetty_threads_config_max -> tomcat_threads_config_max_threads
Thread ํจ๋ ์ค์ ๋ณ๊ฒฝ
jetty_threads_current -> tomcat_threads_current_threads
jetty_threads_busy-> tomcat_threads_busy_threads
jetty_threads_idle -> ์ ๊ฑฐ
jetty_threads_jobs -> ์ ๊ฑฐ
๋ง์ดํฌ๋ก๋ฏธํฐ ๋์๋ณด๋๋ JVM ์ฑ๋ฅ๊ณผ ๋ฆฌ์์ค ํ์ฉ ์ํ๋ฅผ ์ค์๊ฐ์ผ๋ก ๋ชจ๋ํฐ๋งํ๊ณ ์ต์ ํ๋ฅผ ์ง์ํ๋ ๋ฐ ์ ์ฉ
https://grafana.com/grafana/dashboards/4701-jvm-micrometer/

docker-compose.yml
version: '3.7'
services:
prometheus:
container_name: prometheus
image: prom/prometheus:latest
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- 9090:9090
networks:
- es-bridge
grafana:
container_name: grafana
image: grafana/grafana:latest
ports:
- 3000:3000
depends_on:
- prometheus
networks:
- es-bridge
networks:
es-bridge:
driver: bridge
์์ ์ ์ค์ ํ prometheus.yml๋ฅผ ์ง์ ๋ง๋ค์ด์ ๊ฐ์ ๋๋ ํฐ๋ฆฌ์ ์์ฑํด์ค์ผํ๋ค.
โ๏ธ ํ์ง๋ง ์์ ๋ก์ปฌ์์ ์ค์ ํ prometheus.yml๋ฅผ ๊ทธ๋๋ก ์ค์ ํ๋ฉด
http://localhost:8080/actuator/prometheus๊ฐ ์ ์๋ํ๋๋ผ๋,Prometheus์ปจํ ์ด๋ ๋ด๋ถ์์๋ ํด๋น ํ๊ฒ์ ์ฐพ์ง ๋ชปํด ์ํ๊ฐ ๋ค์ด(Down)์ผ๋ก ํ์๋๋ค.

localhost: Docker ์ปจํ
์ด๋๋ ๋
๋ฆฝ์ ์ธ ๋คํธ์ํฌ ๋ค์์คํ์ด์ค๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์, ์ปจํ
์ด๋ ๋ด๋ถ์์์ localhost๋ ํด๋น ์ปจํ
์ด๋ ๋ด์์๋ง ์ ๊ทผ ๊ฐ๋ฅํ ์ฃผ์๋ค.localhost:8080์ผ๋ก ์ ๊ทผํ ์ ์์ผ๋ฏ๋ก ์ํ๊ฐ ๋ค์ด์ผ๋ก ๋ํ๋๋ค.๋ฐ๋ผ์ ์ด๋ ๊ฒ ์ค์ ํด์ผํ๋ค.
prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: [ "localhost:9090" ]
- job_name: "spring-actuator"
metrics_path: '/actuator/prometheus'
scrape_interval: 1s
static_configs:
- targets: [ "host.docker.internal:8080" ]
docker-compose up -d
localhost:9090๋ก ์ค์ ์ ํ๋ค๋ฉด ๋ฐ์ ์ฒ๋ผ ์ค๋ฅ๊ฐ ๋ฐ์ํ ์๊ฐ ์๋ค.
localhost:9090 ๋์ http://prometheus:9090 ๊ฐ์ ์์ผ๋ก ์ปจํ
์ด๋(ํน์ ์๋น์ค) ์ด๋ฆ์ ํธ์คํธ๋ก ์จ์ผํ๋ค.โ๏ธ Docker ์ปจํ ์ด๋ ๋ด๋ถ์์๋ localhost๊ฐ ํด๋น ์ปจํ ์ด๋ ์์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ฏ๋ก, ๊ฐ์ ๋คํธ์ํฌ์ ๋ค๋ฅธ ์ปจํ ์ด๋์ ์ ๊ทผํ๋ ค๋ฉด ์ปจํ ์ด๋ ์ด๋ฆ์ ์ฌ์ฉํด์ผ ํ๋ค. ๋ฐ๋๋ก Docker ์ปจํ ์ด๋ ๋ด๋ถ์์ ๋ก์ปฌ(ํธ์คํธ) ๋จธ์ ์ ์ ๊ทผํ๋ ค๋ฉด
host.docker.internal์ ์ฌ์ฉ
์ฐธ๊ณ :