main:重构指标系统并切换为 Redis 方案
变更内容: - 重构指标系统实现,支持基于 Redis 的多实例指标管理。 - 替换原有的 Pushgateway 和 Redis Exporter 方案。 - 更新 Prometheus 配置,适配新的指标抓取方式。 - 添加 Redis 指标相关配置和告警规则文件。 - 更新 Dockerfile 和 docker-compose 文件,移除多余服务,精简配置。 - 编写 `metrics_unified.py` 模块及单元测试。 - 修复部分代码中的冗余和格式问题。
This commit is contained in:
@@ -9,12 +9,15 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
|
||||
# 复制依赖文件
|
||||
COPY requirements.txt .
|
||||
COPY requirements-dev.txt .
|
||||
|
||||
# 安装 Python 依赖
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
RUN pip install --no-cache-dir -r requirements-dev.txt
|
||||
|
||||
# 复制应用代码
|
||||
# 复制应用代码和配置
|
||||
COPY src/ ./src/
|
||||
COPY config/ ./config/
|
||||
|
||||
# 创建非 root 用户
|
||||
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app
|
||||
|
||||
@@ -11,19 +11,19 @@ services:
|
||||
- APP_ENV=development
|
||||
- LOG_LEVEL=INFO
|
||||
- METRICS_ENABLED=true
|
||||
# 方案1:Pushgateway 配置
|
||||
- PUSHGATEWAY_URL=pushgateway:9091
|
||||
- METRICS_JOB_NAME=functional_scaffold
|
||||
# 方案2:Redis 配置
|
||||
# Redis 指标存储配置
|
||||
- REDIS_HOST=redis
|
||||
- REDIS_PORT=6379
|
||||
- REDIS_METRICS_DB=0
|
||||
- REDIS_DB=0
|
||||
# 指标配置文件路径
|
||||
- METRICS_CONFIG_PATH=config/metrics.yaml
|
||||
volumes:
|
||||
- ../src:/app/src
|
||||
- ../config:/app/config
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
- redis
|
||||
- pushgateway
|
||||
redis:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/healthz')"]
|
||||
interval: 30s
|
||||
@@ -31,11 +31,11 @@ services:
|
||||
retries: 3
|
||||
start_period: 5s
|
||||
|
||||
# Redis - 用于集中式指标存储(方案2)
|
||||
# Redis - 用于集中式指标存储
|
||||
redis:
|
||||
image: redis:7-alpine
|
||||
ports:
|
||||
- "6379:6379"
|
||||
- "6380:6379"
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
command: redis-server --appendonly yes
|
||||
@@ -46,47 +46,20 @@ services:
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
|
||||
# Pushgateway - 用于短生命周期任务的指标推送(方案1,推荐)
|
||||
pushgateway:
|
||||
image: prom/pushgateway:latest
|
||||
ports:
|
||||
- "9091:9091"
|
||||
restart: unless-stopped
|
||||
command:
|
||||
- '--persistence.file=/data/pushgateway.data'
|
||||
- '--persistence.interval=5m'
|
||||
volumes:
|
||||
- pushgateway_data:/data
|
||||
|
||||
# Redis Exporter - 将 Redis 指标导出为 Prometheus 格式(方案2需要)
|
||||
redis-exporter:
|
||||
build:
|
||||
context: ..
|
||||
dockerfile: deployment/Dockerfile.redis-exporter
|
||||
ports:
|
||||
- "8001:8001"
|
||||
environment:
|
||||
- REDIS_HOST=redis
|
||||
- REDIS_PORT=6379
|
||||
- REDIS_METRICS_DB=0
|
||||
depends_on:
|
||||
- redis
|
||||
restart: unless-stopped
|
||||
|
||||
prometheus:
|
||||
image: prom/prometheus:latest
|
||||
ports:
|
||||
- "9090:9090"
|
||||
volumes:
|
||||
- ../monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
|
||||
- ../monitoring/alerts:/etc/prometheus/rules
|
||||
- prometheus_data:/prometheus
|
||||
command:
|
||||
- '--config.file=/etc/prometheus/prometheus.yml'
|
||||
- '--storage.tsdb.path=/prometheus'
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
- pushgateway
|
||||
- redis-exporter
|
||||
- app
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana:latest
|
||||
@@ -105,4 +78,3 @@ volumes:
|
||||
prometheus_data:
|
||||
grafana_data:
|
||||
redis_data:
|
||||
pushgateway_data:
|
||||
|
||||
Reference in New Issue
Block a user