Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: ./deployment/
target: /home/ubuntu/deployment
target: /home/ubuntu/dearbelly

deploy:
name: Deploy
Expand Down
3 changes: 3 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from app.core.lifespan import lifespan
from app.api.endpoints import predictions
from app.core.logging_config import setup_logging
from prometheus_fastapi_instrumentator import Instrumentator

setup_logging()
app = FastAPI(
Expand All @@ -11,6 +12,8 @@
lifespan=lifespan
)

instrumentator = Instrumentator().instrument(app)
instrumentator.expose(app, include_in_schema=False, endpoint="/actuator/prometheus")
app.include_router(predictions.router, prefix="/api/v1", tags=["Prediction"])

@app.get("/health")
Expand Down
4 changes: 2 additions & 2 deletions deployment/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ fi

echo "Pulling new image"
# docker pull
docker compose pull app-${AFTER_COMPOSE_COLOR}
docker compose up -d --no-deps --force-recreate app-${AFTER_COMPOSE_COLOR}
docker compose -f docker-compose.yml pull app-${AFTER_COMPOSE_COLOR}
docker compose -f docker-compose.yml up -d --no-deps --force-recreate app-${AFTER_COMPOSE_COLOR}


# μƒˆ μ»¨ν…Œμ΄λ„ˆκ°€ running 될 λ•ŒκΉŒμ§€ λŒ€κΈ°
Expand Down
33 changes: 33 additions & 0 deletions deployment/docker-compose.monitoring.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
services:
prometheus:
user: ${PROMETHEUS_USER}
image: prom/prometheus:latest

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

prom/prometheus:latest와 같이 :latest νƒœκ·Έλ₯Ό μ‚¬μš©ν•˜λŠ” 것은 ν”„λ‘œλ•μ…˜ ν™˜κ²½μ—μ„œ 예기치 μ•Šμ€ 버전 λ³€κ²½μœΌλ‘œ μΈν•œ 문제λ₯Ό μΌμœΌν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€. μ•ˆμ •μ μΈ μš΄μ˜μ„ μœ„ν•΄ νŠΉμ • 버전(예: v2.51.2)을 λͺ…μ‹œν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.

    image: prom/prometheus:v2.51.2

container_name: prometheus
volumes:
- /home/ubuntu/dearbelly/deployment/prometheus.yml:/etc/prometheus/prometheus.yml:ro
- /home/ubuntu/prometheus/volume:/prometheus/data
Comment on lines +7 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Prometheus λ³Όλ₯¨ κ²½λ‘œκ°€ /home/ubuntu/...와 같이 μ ˆλŒ€ 경둜둜 ν•˜λ“œμ½”λ”©λ˜μ–΄ μžˆμ–΄ λ‹€λ₯Έ ν™˜κ²½μ—μ„œμ˜ μž¬μ‚¬μš©μ„± 및 이식성이 λ–¨μ–΄μ§‘λ‹ˆλ‹€. μ„€μ • νŒŒμΌμ€ μƒλŒ€ 경둜 (./prometheus.yml)λ₯Ό μ‚¬μš©ν•˜κ³ , 데이터 λ³Όλ₯¨μ€ Docker의 named volume을 μ‚¬μš©ν•˜λŠ” 것을 ꢌμž₯ν•©λ‹ˆλ‹€. 파일 ν•˜λ‹¨μ— volumes μ„Ήμ…˜μ„ μΆ”κ°€ν•˜μ—¬ prometheus-data named volume을 μ •μ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€.

      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus-data:/prometheus/data

ports:
- 9090:9090
command:
- '--web.enable-lifecycle'
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.retention.time=15d'
restart: always
networks:
- mynetwork

grafana:
user: ${GRAFANA_USER}
image: grafana/grafana:latest

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

grafana/grafana:latest와 같이 :latest νƒœκ·Έλ₯Ό μ‚¬μš©ν•˜λŠ” 것은 ν”„λ‘œλ•μ…˜ ν™˜κ²½μ—μ„œ 예기치 μ•Šμ€ 버전 λ³€κ²½μœΌλ‘œ μΈν•œ 문제λ₯Ό μΌμœΌν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€. μ•ˆμ •μ μΈ μš΄μ˜μ„ μœ„ν•΄ νŠΉμ • 버전(예: 11.0.0)을 λͺ…μ‹œν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.

    image: grafana/grafana:11.0.0

container_name: grafana
ports:
- 3000:3000
volumes:
- /home/ubuntu/grafana/volume:/var/lib/grafana

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Grafana 데이터 λ³Όλ₯¨ κ²½λ‘œκ°€ /home/ubuntu/grafana/volume와 같이 μ ˆλŒ€ 경둜둜 ν•˜λ“œμ½”λ”©λ˜μ–΄ μžˆμ–΄ 이식성이 λ–¨μ–΄μ§‘λ‹ˆλ‹€. Docker의 named volume을 μ‚¬μš©ν•˜μ—¬ κ΄€λ¦¬ν•˜λŠ” 것을 ꢌμž₯ν•©λ‹ˆλ‹€. 파일 ν•˜λ‹¨μ— volumes μ„Ήμ…˜μ„ μΆ”κ°€ν•˜μ—¬ grafana-data named volume을 μ •μ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€.

      - grafana-data:/var/lib/grafana

restart: always
networks:
- mynetwork

networks:
mynetwork:
driver: bridge
17 changes: 17 additions & 0 deletions deployment/prometheus-rule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
groups:
- name: example
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)"
34 changes: 34 additions & 0 deletions deployment/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
global:
scrape_interval: 15s
scrape_timeout: 15s
evaluation_interval: 2m
external_labels:
monitor: 'codelab-monitor'
query_log_file: query_log_file.log
rule_files:
- "prometheus-rule.yml"
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 10s
scrape_timeout: 10s
metrics_path: '/metrics'
honor_labels: false
honor_timestamps: false
scheme: 'http'
static_configs:
- targets: ['${REMOTE_HOST}:9090']
labels:
service: 'monitor-1'
- job_name: 'node'
static_configs:
- targets: ['${REMOTE_HOST}:9090']
Comment on lines +22 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

node job의 targetsκ°€ Prometheus μžμ‹ μ„ κ°€λ¦¬ν‚€λŠ” '${REMOTE_HOST}:9090'으둜 μ„€μ •λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. nodeλΌλŠ” μ΄λ¦„μœΌλ‘œ 보아 Node Exporter λ©”νŠΈλ¦­μ„ μˆ˜μ§‘ν•˜λ €λŠ” μ˜λ„λ‘œ λ³΄μ΄μ§€λ§Œ, ν¬νŠΈκ°€ 잘λͺ»λ˜μ—ˆμŠ΅λ‹ˆλ‹€. Node ExporterλŠ” 보톡 9100 포트λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. λ§Œμ•½ Node Exporterλ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄ 이 job은 λΆˆν•„μš”ν•˜κ²Œ Prometheus λ©”νŠΈλ¦­μ„ 쀑볡 μˆ˜μ§‘ν•˜λ―€λ‘œ μ œκ±°ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.

  - job_name: 'node'
    static_configs:
      - targets: ['${REMOTE_HOST}:9100']

- job_name: 'fastapi-actuator-blue'
metrics_path: '/actuator/prometheus'
scrape_interval: 1m
static_configs:
- targets: [ 'app-blue:8000' ]
- job_name: 'fastapi-actuator-green'
metrics_path: '/actuator/prometheus'
scrape_interval: 1m
static_configs:
- targets: [ 'app-green:8001' ]
Comment on lines +25 to +34

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

fastapi-actuator-blue와 fastapi-actuator-green job이 app-blue:8000, app-green:8001을 νƒ€κ²ŸμœΌλ‘œ ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ docker-compose.monitoring.ymlκ³Ό docker-compose.yml이 λ³„λ„μ˜ 파일둜 κ΄€λ¦¬λ˜κ³  μžˆμ–΄, Prometheus μ»¨ν…Œμ΄λ„ˆμ™€ μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ»¨ν…Œμ΄λ„ˆλ“€μ΄ μ„œλ‘œ λ‹€λ₯Έ Docker λ„€νŠΈμ›Œν¬μ— μ†ν•˜κ²Œ 될 κ°€λŠ₯성이 λ†’μŠ΅λ‹ˆλ‹€. 이 경우 Prometheusκ°€ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ λ©”νŠΈλ¦­μ„ μˆ˜μ§‘ν•  수 μ—†μŠ΅λ‹ˆλ‹€. λͺ¨λ“  μ»¨ν…Œμ΄λ„ˆκ°€ λ™μΌν•œ Docker λ„€νŠΈμ›Œν¬μ— μžˆλ„λ‘ docker-compose.ymlκ³Ό docker-compose.monitoring.yml의 λ„€νŠΈμ›Œν¬ 섀정을 λ§žμΆ°μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, 곡톡 μ™ΈλΆ€ λ„€νŠΈμ›Œν¬(external network)λ₯Ό μ‚¬μš©ν•˜λ„λ‘ μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ Pillow==11.3.0
dotenv
openai
timm
logging
logging
prometheus-client==0.19.0
prometheus-fastapi-instrumentator==6.1.0