
๋ก๊ทธ์ ์งํ๋ฅผ ์ ๋ถ CloudWatch์ ์๋ค ๋ณด๋ ๋น์ฉ์ด ๊ฐ๋นํ๊ธฐ ์ด๋ ค์์ก๋ค. ๊ทธ๋์ Prometheus์ Loki์ Grafana๋ฅผ Docker Compose๋ก ํ ์๋ฒ์ ๋์ ๋ชจ๋ํฐ๋ง์ ์ง์ ๊พธ๋ ธ๋ค. Compose ์ค์ ๋ถํฐ Spring ์ ํ๋ฆฌ์ผ์ด์ ์ด ์งํ์ ๋ก๊ทธ๋ฅผ ๋ณด๋ด๊ฒ ํ๋ ์ค์ ๊น์ง ์ ๋ฆฌํด ๋ณธ๋ค.

| ๊ตฌ์ฑ | ํ๋ ์ผ |
|---|---|
| Prometheus | ์ ํ๋ฆฌ์ผ์ด์
์ /actuator/prometheus๋ฅผ ์ฃผ๊ธฐ์ ์ผ๋ก ๊ธ์ด ์ ์งํ๋ฅผ ์ ์ฅํ๋ค |
| Loki | ์ ํ๋ฆฌ์ผ์ด์ ์ด ๋ฐ์ด ๋ฃ๋ ๋ก๊ทธ๋ฅผ ๋ผ๋ฒจ๋ก ๋๋ ์ ์ฅํ๋ค |
| Grafana | Prometheus์ Loki๋ฅผ ๋ฐ์ดํฐ ์์ค๋ก ๋ถ์ฌ ๋์๋ณด๋๋ก ๋ณด์ฌ ์ค๋ค |
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml # ์ค์ ํ์ผ ๋ง์ดํธ
command:
- --config.file=/etc/prometheus/prometheus.yml
networks:
- prometheus
loki:
image: grafana/loki:latest
container_name: loki
user: "$UID:$GID" # ๋ง์ดํธํ ๋๋ ํฐ๋ฆฌ ๊ถํ ๋ฌธ์ ๋ฅผ ํผํ๋ค
ports:
- "3100:3100"
volumes:
- ./loki/loki.yml:/etc/loki/local-config.yaml # ์ค์ ํ์ผ ๋ง์ดํธ
- ./loki:/var/loki # ๋ฐ์ดํฐ ๋๋ ํฐ๋ฆฌ
command: -config.file=/etc/loki/local-config.yaml
networks:
- loki
grafana:
image: grafana/grafana:latest
container_name: grafana
user: "$UID:$GID"
ports:
- "3000:3000"
volumes:
- ./grafana:/var/lib/grafana # ๋์๋ณด๋์ ์ค์ ๋ณด์กด
depends_on:
- prometheus
- loki
networks: # ๋ ๋คํธ์ํฌ์ ๋ชจ๋ ๋ถ๋๋ค
- prometheus
- loki
networks:
prometheus:
driver: bridge
loki:
driver: bridge
prometheus์ loki ๋ ๋คํธ์ํฌ์ ๋ชจ๋ ๋ถ์ด์ ์๋น์ค ์ด๋ฆ(http://prometheus:9090 http://loki:3100)์ผ๋ก ๋์ ๋ถ๋ฅธ๋คlatest ํ๊ทธ๋ ๋ค์ ๋ฐ์ ๋๋ง๋ค ๋ฒ์ ์ด ๋ฐ๋ ์ ์๋ค. ์ด์์์๋ ๋ฒ์ ์ ๊ณ ์ ํด ๋๋ ํธ์ด ์์ ํ๋ค| ๋ช ๋ น | ํ๋ ์ผ |
|---|---|
docker compose up -d | ์ ์ํ ์๋น์ค๋ฅผ ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ๋์ด๋ค. ๋ฐ๋ ์๋น์ค๋ง ๋ค์ ๋ง๋ ๋ค |
docker compose down | ์ปจํ
์ด๋์ ๋คํธ์ํฌ๋ฅผ ๋ด๋ฆฌ๊ณ ์ง์ด๋ค. ๋ณผ๋ฅจ์ ๋จ๋๋ค (--volumes๋ฅผ ์ฃผ๋ฉด ๋ณผ๋ฅจ๋ ์ง์ด๋ค) |
// build.gradle
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'
# application.yml
management:
endpoints:
web:
exposure:
include: prometheus
์ด๋ ๊ฒ ๋๋ฉด /actuator/prometheus์์ Prometheus ํ์์ ์งํ๊ฐ ๋์จ๋ค.
// build.gradle
implementation 'com.github.loki4j:loki-logback-appender:1.5.1'
<!-- logback-spring.xml: WARN ์ด์๋ง Loki๋ก ๋ณด๋ธ๋ค -->
<appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<http>
<url>http://{Loki ์๋ฒ ์ฃผ์}:3100/loki/api/v1/push</url>
</http>
<format>
<!-- ๋ผ๋ฒจ: Loki์์ ๋ก๊ทธ๋ฅผ ๋๋๊ณ ์ฐพ๋ ๊ธฐ์ค -->
<label>
<pattern>appName=my-app-dev,host=${HOSTNAME},level=%level</pattern>
</label>
<!-- ๋ฉ์์ง: ์ค์ ๋ก๊ทธ ๋ด์ฉ -->
<message>
<pattern>[%thread] %-5level %logger{36} - %msg%n</pattern>
</message>
</format>
</appender>
๐ ์ฌ๊ธฐ์ ๋ถ์ธ appName ๋ผ๋ฒจ๋ก Grafana์์ {appName="$app"}์ฒ๋ผ ์๋น์ค๋ณ ๋ก๊ทธ๋ฅผ ๊ณจ๋ผ ๋ณธ๋ค.
global:
scrape_interval: 15s # ์งํ๋ฅผ ๊ธ์ด ์ค๋ ์ฃผ๊ธฐ (๊ธฐ๋ณธ๊ฐ 1m)
evaluation_interval: 15s # ์๋ฆผ ๋ฃฐ์ ํ๊ฐํ๋ ์ฃผ๊ธฐ (๊ธฐ๋ณธ๊ฐ 1m)
scrape_configs:
# ์ฃผ์๊ฐ ๊ณ ์ ๋ ๋์
- job_name: "test-one"
metrics_path: "/actuator/prometheus"
scheme: https
static_configs:
- targets: ["test.application.com"]
relabel_configs:
- source_labels: ['__address__']
target_label: 'instance'
replacement: '์ํํ
์คํธ(์ฒซ๋ฒ์งธ)'
# ECS Fargate์ฒ๋ผ IP๊ฐ ๊ณ์ ๋ฐ๋๋ ๋์
- job_name: "test-two"
metrics_path: "/actuator/prometheus"
scheme: http
dns_sd_configs:
- names:
- 'test-two-api.test-two.dev' # ECS ์๋น์ค ๋์ค์ปค๋ฒ๋ฆฌ(Cloud Map) ๋๋ฉ์ธ
type: 'A'
port: 80
relabel_configs:
- source_labels: ['__address__']
target_label: 'instance'
| ๋ฐฉ์ | ๋์ ์ฐพ๋ ๋ฒ | ์ด์ธ๋ฆฌ๋ ๊ณณ |
|---|---|---|
โ
static_configs | IP๋ ๋๋ฉ์ธ์ ์ง์ ์ ๋๋ค | ์ฃผ์๊ฐ ๊ณ ์ ๋ EC2๋ ๋ก๋ ๋ฐธ๋ฐ์ ๋ค์ ์๋น์ค. ๋์์ด ์ ๊ณ ์ ์ ๋ฐ๋ ๋ |
โ
dns_sd_configs | DNS A ๋ ์ฝ๋๋ฅผ ์กฐํํด์ ์ฐพ๋๋ค | ECS ์๋น์ค ๋์ค์ปค๋ฒ๋ฆฌ๋ Kubernetes์ฒ๋ผ ๋์์ด ๋๊ณ ์ค์ด๋๋ ๊ณณ. ํ์คํฌ๊ฐ ๋ฐ๋์ด๋ ์๋์ผ๋ก ๋ฐ๋ผ๊ฐ๋ค |
โ ๏ธ ์์ง ์ฃผ๊ธฐ๋ฅผ 1์ด์ฒ๋ผ ์์ฃผ ์งง๊ฒ ์ก์ผ๋ฉด Prometheus์ ์ ํ๋ฆฌ์ผ์ด์ ๋ชจ๋ ๋ถ๋ด์ด ์ปค์ง๋ค. ์ฒ์์๋ 15์ด ์ ๋๋ก ์์ํ๋ ๊ฑธ ๊ถํ๋ค.
auth_enabled: false # ๋ด๋ถ ๋คํธ์ํฌ์์๋ง ์ธ ๋
server:
http_listen_port: 3100
common:
instance_addr: 127.0.0.1
path_prefix: /var/loki
storage:
filesystem:
chunks_directory: /var/loki/chunks
rules_directory: /var/loki/rules
replication_factor: 1 # ๋จ์ผ ๋
ธ๋
ring:
kvstore:
store: inmemory
query_range:
results_cache:
cache:
embedded_cache:
enabled: true
max_size_mb: 100
schema_config:
configs:
- from: 2020-10-24
store: tsdb
object_store: s3 # ์ฒญํฌ๋ S3์ ์ ์ฅ
schema: v12
index:
prefix: index_
period: 24h
storage_config:
aws:
s3: {S3 ๋ฒํท ์ด๋ฆ}
region: ap-northeast-2
โ ๏ธ ์๋๋ storage_config.aws์ ์ก์ธ์ค ํค์ ์ํฌ๋ฆฟ ํค๋ฅผ ์ง์ ์ ์๋ค. ์ค์ ํ์ผ์ ํค๋ฅผ ๋๋ฉด ์ ์ฅ์๋ ์๋ฒ๋ฅผ ํตํด ์์ด ๋๊ฐ๊ธฐ ์ฝ๋ค. EC2์ IAM ์ญํ (์ธ์คํด์ค ํ๋กํ์ผ)์ ๋ถ์ด๋ฉด ํค ์์ด S3์ ์ ๊ทผํ ์ ์๋ค.
http://{์๋ฒ ์ฃผ์}:3000์ผ๋ก ๋ค์ด๊ฐ๋ฉด Grafana ์ฒซ ํ๋ฉด์ด ๋์จ๋ค.

๋์๋ณด๋ ๋ง๋ค๊ธฐ๋ Prometheus ํธ๊ณผ Loki ํธ์ ์ด์ด์ ์ ๋ฆฌํ๋ค ๐