第五章:Exporter 配置

详细介绍各类 Prometheus Exporter 的配置和使用,包括官方 Exporter 和自定义 Exporter 开发

最后更新: 2024-01-01
页面目录

第五章:Exporter 配置

5.1 Exporter 概述

Exporter 是 Prometheus 生态中的关键组件,负责从目标系统采集指标并暴露为 Prometheus 可抓取的格式。

5.1.1 工作原理

┌──────────────┐      HTTP /metrics       ┌─────────────────┐
│   Target     │ ─────────────────────────▶│    Exporter     │
│   (服务)      │                           │   (转换层)       │
└──────────────┘                           └────────┬────────┘
                                                     │ HTTP
                                            ┌─────────────────┐
                                            │   Prometheus    │
                                            │   Server        │
                                            └─────────────────┘

5.1.2 Exporter 分类

类型 说明 示例
官方 Exporter Prometheus 官方维护 node_exporter, blackbox_exporter
数据库 Exporter 各类数据库监控 mysqld_exporter, postgres_exporter
消息队列 Exporter 消息中间件监控 kafka_exporter, rabbitmq_exporter
硬件 Exporter 硬件设备监控 ipmi_exporter, snmp_exporter
自定义 Exporter 用户自行开发 自定义业务指标

5.2 Node Exporter

5.2.1 安装部署

# 下载
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
tar xzf node_exporter-1.7.0.linux-amd64.tar.gz
cd node_exporter-1.7.0.linux-amd64

# 运行
./node_exporter

# Docker 部署
docker run -d \
  --name node_exporter \
  -p 9100:9100 \
  --net="host" \
  prom/node-exporter:latest

5.2.2 常用采集器

采集器 说明 启用方式
cpu CPU 统计 默认启用
meminfo 内存信息 默认启用
diskstats 磁盘统计 默认启用
filesystem 文件系统 默认启用
netdev 网络设备 默认启用
loadavg 系统负载 默认启用
pressure 压力统计 --collector.pressure
textfile 文本文件 --collector.textfile.directory
systemd Systemd --collector.systemd

5.2.3 高级配置

# 启用所有采集器
./node_exporter \
  --collector.all \
  --collector.systemd \
  --collector.processes \
  --collector.textfile.directory=/var/lib/node_exporter/textfile_collector

5.2.4 常用指标

# CPU 使用率
100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

# 内存使用率
100 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100)

# 磁盘使用率
100 - (node_filesystem_avail_bytes / node_filesystem_size_bytes * 100)

# 磁盘 I/O
rate(node_disk_read_bytes_total[5m])
rate(node_disk_written_bytes_total[5m])

# 网络流量
rate(node_network_receive_bytes_total[5m])
rate(node_network_transmit_bytes_total[5m])

5.3 MySQL Exporter

5.3.1 安装配置

# 下载
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.0/mysqld_exporter-0.15.0.linux-amd64.tar.gz
tar xzf mysqld_exporter-0.15.0.linux-amd64.tar.gz

# 创建数据库用户
mysql -u root -p <<EOF
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'your_password';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
FLUSH PRIVILEGES;
EOF

# 配置数据源
cat > ~/.my.cnf <<EOF
[client]
user=exporter
password=your_password
EOF

# 运行
./mysqld_exporter --config.my-cnf=/path/to/.my.cnf

5.3.2 常用指标

# QPS
rate(mysql_global_status_questions[5m])

# 连接数
mysql_global_status_threads_connected

# 连接使用率
100 * mysql_global_status_threads_connected / mysql_global_variables_max_connections

# 查询缓存命中率
mysql_global_status_qcache_hits / (mysql_global_status_qcache_hits + mysql_global_status_qcache_inserts)

# InnoDB 缓冲池使用率
100 * (mysql_global_status_innodb_buffer_pool_pages_data / mysql_global_status_innodb_buffer_pool_pages_total)

5.4 Redis Exporter

5.4.1 安装部署

# 下载
wget https://github.com/oliver006/redis_exporter/releases/download/v1.57.0/redis_exporter-v1.57.0.linux-amd64.tar.gz
tar xzf redis_exporter-v1.57.0.linux-amd64.tar.gz

# 运行
./redis_exporter redis://localhost:6379

# 多个 Redis 实例
./redis_exporter \
  -redis.addr="redis://localhost:6379,redis://localhost:6380" \
  -redis.password="password"

5.4.2 常用指标

# 连接数
redis_connected_clients

# 内存使用
redis_memory_used_bytes

# 命令 QPS
rate(redis_commands_processed_total[5m])

# 键数量
redis_db_keys

# 缓存命中率
rate(redis_keyspace_hits_total[5m]) / 
  (rate(redis_keyspace_hits_total[5m]) + rate(redis_keyspace_misses_total[5m]))

5.5 Blackbox Exporter

5.5.1 配置

# blackbox.yml
modules:
  http_2xx:
    prober: http
    timeout: 10s
    http:
      valid_status_codes: [200, 201, 202, 204]
      method: GET

  http_post_2xx:
    prober: http
    http:
      method: POST
      headers:
        Content-Type: application/json
      body: '{"key": "value"}'

  tcp_connect:
    prober: tcp
    timeout: 5s

  dns:
    prober: dns
    dns:
      query_name: prometheus.io
      query_type: A

5.5.2 Prometheus 配置

scrape_configs:
  - job_name: 'blackbox-http'
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets:
          - https://prometheus.io
          - https://grafana.com
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: blackbox-exporter:9115

  - job_name: 'blackbox-tcp'
    metrics_path: /probe
    params:
      module: [tcp_connect]
    static_configs:
      - targets:
          - localhost:22
          - localhost:3306
          - localhost:6379
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: blackbox-exporter:9115

5.5.3 常用指标

# HTTP 可用性
probe_success{job="blackbox-http"}

# DNS 可用性
probe_success{job="blackbox-dns"}

# 响应时间
probe_duration_seconds{job="blackbox-http"}

5.6 自定义 Exporter

5.6.1 Go 语言示例

package main

import (
    "net/http"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
    httpRequests = prometheus.NewCounterVec(
        prometheus.CounterOpts{
            Name: "myapp_http_requests_total",
            Help: "Total HTTP requests",
        },
        []string{"method", "status"},
    )
    
    httpDuration = prometheus.NewHistogramVec(
        prometheus.HistogramOpts{
            Name:    "myapp_http_request_duration_seconds",
            Help:    "HTTP request duration",
            Buckets: prometheus.DefBuckets,
        },
        []string{"method"},
    )
)

func init() {
    prometheus.MustRegister(httpRequests)
    prometheus.MustRegister(httpDuration)
}

func main() {
    http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
        promhttp.Handler().ServeHTTP(w, r)
    })
    
    http.HandleFunc("/api/inc", func(w http.ResponseWriter, r *http.Request) {
        httpRequests.WithLabelValues("GET", "200").Inc()
    })
    
    http.ListenAndServe(":8080", nil)
}

5.6.2 Python 语言示例

from prometheus_client import Counter, Histogram, generate_latest, start_http_server

REQUEST_COUNT = Counter(
    'myapp_requests_total',
    'Total request count',
    ['method', 'status']
)

REQUEST_LATENCY = Histogram(
    'myapp_request_duration_seconds',
    'Request latency',
    buckets=[0.1, 0.5, 1.0, 5.0]
)

if __name__ == '__main__':
    start_http_server(8080)
    
    while True:
        # 业务逻辑
        pass

5.6.3 Pushgateway 用于短任务

# 安装 Pushgateway
docker run -d -p 9091:9091 prom/pushgateway

# 推送指标
echo "my_batch_job_duration_seconds 2.5" | curl --data-binary @- http://localhost:9091/metrics/job/my_batch_job

# Prometheus 配置
scrape_configs:
  - job_name: 'pushgateway'
    static_configs:
      - targets:
          - pushgateway:9091
    metrics_path: /metrics

5.7 Exporter 最佳实践

5.7.1 命名规范

规范 正确示例 错误示例
使用下划线 http_requests_total httpRequestsTotal
单位后缀 http_request_duration_seconds http_request_duration
基数名词 http_requests_total http_request_total
标签优于指标 http_requests_total{status="200"} http_requests_2xx_total

5.7.2 性能优化

scrape_configs:
  - job_name: 'optimized-exporter'
    # 减少抓取频率
    scrape_interval: 30s
    # 启用 HTTP/2
    scheme: https
    # 启用压缩
    http_server_config:
      enable_http2: true

5.7.3 安全配置

scrape_configs:
  - job_name: 'secure-exporter'
    scheme: https
    tls_config:
      ca_file: /etc/prometheus/ca.crt
      cert_file: /etc/prometheus/client.crt
      key_file: /etc/prometheus/client.key
      insecure_skip_verify: false

5.8 本章小结

本章介绍了 Prometheus Exporter 的配置和使用:

  1. Exporter 概述 - 工作原理和分类
  2. Node Exporter - 系统监控指标
  3. MySQL Exporter - 数据库监控
  4. Redis Exporter - 缓存监控
  5. Blackbox Exporter - 黑盒探测
  6. 自定义 Exporter - Go 和 Python 示例
  7. 最佳实践 - 命名规范和安全配置

📖 下一步