Skip to content

性能优化 ​

指标优化 ​

减少时间序列数量 ​

yaml
# 避免
http_requests_total{user_id="12345"}  # 高基数

# 推荐
http_requests_total{user_type="premium"}  # 低基数

合理使用标签 ​

yaml
# 好的实践
http_requests_total{method="GET", status="200"}

# 避免过多标签组合
# 10个标签,每个10个值 → 10^10 时间序列

Histogram 优化 ​

go
// 合理设置桶
prometheus.NewHistogram(prometheus.HistogramOpts{
    Name:    "http_request_duration_seconds",
    Help:    "HTTP request duration",
    Buckets: []float64{.01, .05, .1, .5, 1, 5},  // 不要太多
})

抓取优化 ​

抓取间隔 ​

yaml
global:
  scrape_interval: 15s  # 默认,根据需求调整

scrape_configs:
  - job_name: 'critical'
    scrape_interval: 5s   # 重要服务高频
  
  - job_name: 'batch'
    scrape_interval: 60s  # 批处理低频

超时设置 ​

yaml
scrape_configs:
  - job_name: 'app'
    scrape_timeout: 10s
    scrape_interval: 15s

样本限制 ​

yaml
scrape_configs:
  - job_name: 'app'
    sample_limit: 50000  # 限制每个抓取的样本数

存储优化 ​

Retention 设置 ​

yaml
# prometheus.yml
global:
  retention: 15d

# 或启动参数
prometheus --storage.tsdb.retention.time=15d

数据压缩 ​

Prometheus 自动压缩数据。

bash
# 监控压缩
prometheus_tsdb_compactions_total
prometheus_tsdb_compaction_duration_seconds

存储空间 ​

bash
# 监控存储
prometheus_tsdb_storage_blocks_bytes
prometheus_tsdb_head_series

查询优化 ​

避免范围查询过大 ​

promql
# 不推荐:大范围
rate(http_requests_total[1h])

# 推荐:适当范围
rate(http_requests_total[5m])

使用 Recording Rules ​

预计算复杂查询。

yaml
groups:
  - name: example
    interval: 30s
    rules:
      - record: job:http_requests:rate5m
        expr: sum by (job) (rate(http_requests_total[5m]))

查询示例优化 ​

promql
# 慢查询
sum(rate(http_requests_total[5m])) by (job) / 
sum(rate(http_requests_total[5m] offset 1h)) by (job)

# 优化:使用 recording rule
# 预计算 rate
- record: job:http_requests:rate5m
  expr: sum by (job) (rate(http_requests_total[5m]))

# 查询使用预计算结果
job:http_requests:rate5m / 
job:http_requests:rate5m offset 1h

内存优化 ​

内存配置 ​

bash
# 启动参数
prometheus \
  --storage.tsdb.retention.time=15d \
  --storage.tsdb.head-chunks-write-queue-size=1000

监控内存 ​

promql
# Go 内存
go_memstats_heap_inuse_bytes
go_memstats_heap_alloc_bytes

# 时间序列数量
prometheus_tsdb_head_series

并发优化 ​

Recording Rules ​

yaml
groups:
  - name: instance_rules
    concurrency: 2  # 并发评估
    rules:
      - record: ...

查询并发 ​

bash
# 启动参数
prometheus --query.max-concurrency=20

远程存储优化 ​

批量写入 ​

yaml
remote_write:
  - url: "http://remote-storage/write"
    queue_config:
      max_samples_per_send: 1000
      capacity: 2500
      max_shards: 200

监控远程写入 ​

promql
# 写入队列
prometheus_remote_storage_queue_pending_samples

# 写入失败
prometheus_remote_storage_failed_samples_total

# 写入延迟
prometheus_remote_storage_sent_batch_duration_seconds

性能监控 ​

关键指标 ​

promql
# 目标数量
prometheus_targets_active

# 抓取延迟
prometheus_target_interval_length_seconds

# 规则评估时间
prometheus_rule_evaluation_duration_seconds

# 查询延迟
prometheus_engine_query_duration_seconds

Dashboard 示例 ​

# Prometheus 自身监控
- 目标状态:up
- 抓取延迟:scrape_duration_seconds
- 内存使用:process_resident_memory_bytes
- CPU 使用:process_cpu_seconds_total
- 磁盘使用:prometheus_tsdb_storage_blocks_bytes

最佳实践 ​

  1. 控制时间序列数量:< 1000万
  2. 合理设置抓取间隔:根据重要性调整
  3. 使用 Recording Rules:预计算复杂查询
  4. 监控 Prometheus 自己:关键指标告警
  5. 长期存储:使用 Thanos/Cortex/VictoriaMetrics
  6. 资源规划:
    • CPU: 2-4核
    • 内存: 8-16GB
    • 磁盘: SSD,根据保留时间规划

参考:https://prometheus.io/docs/practices/