4、Exporter原理与部署:Node Exporter、Blackbox Exporter、自定义Exporter开发思路
Exporter 是 Prometheus 生态的“数据采集代理”,负责将目标系统的非原生指标格式转换为 Prometheus 可抓取的 Metrics 端点。本章深入剖析 Exporter 的核心原理,并覆盖 Node Exporter、Blackbox Exporter 的部署与配置,最后给出自定义 Exporter 的开发思路与最佳实践。
4.1 Exporter 工作原理
Exporter 本质上是一个 HTTP 服务,监听在特定端口(默认 9100、9115 等),当 Prometheus Server 发起 GET /metrics 请求时,Exporter 实时采集目标数据,并按 Prometheus 文本格式返回指标集合。
核心流程: 目标系统 → 采集器(Collector)→ 指标注册表(Registry)→ HTTP Handler → Prometheus Server
4.2 Node Exporter:主机指标采集
Node Exporter 是 Prometheus 官方维护的硬件和操作系统指标采集器,支持 Linux、Windows、macOS。它通过读取 /proc、/sys 等内核接口获取 CPU、内存、磁盘、网络等指标。
4.2.1 部署与启动
# 下载并解压
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
tar xvf node_exporter-1.7.0.linux-amd64.tar.gz
cd node_exporter-1.7.0.linux-amd64
# 启动(默认端口 9100)
./node_exporter
# 验证
curl http://localhost:9100/metrics | head -20
4.2.2 常用 Collector 与启用/禁用
| Collector 名称 | 采集内容 | 默认状态 |
|---|---|---|
cpu |
CPU 使用率、各模式时间 | 启用 |
meminfo |
内存总量、已用、缓存 | 启用 |
diskstats |
磁盘 I/O 读写速率、IOPS | 启用 |
netstat |
网络连接状态统计 | 启用 |
filesystem |
文件系统使用率、inode | 启用 |
systemd |
Systemd 服务状态 | 禁用 |
textfile |
读取自定义文本文件指标 | 启用 |
生产建议: 使用
--collector.disable-defaults 禁用所有默认 Collector,再按需启用,减少资源开销。例如:--collector.disable-defaults --collector.cpu --collector.meminfo --collector.filesystem
4.2.3 关键指标示例
# HELP node_cpu_seconds_total Seconds the cpus spent in each mode.
# TYPE node_cpu_seconds_total counter
node_cpu_seconds_total{cpu="0",mode="idle"} 123456.78
# HELP node_memory_MemTotal_bytes Memory information field MemTotal_bytes.
# TYPE node_memory_MemTotal_bytes gauge
node_memory_MemTotal_bytes 16759785472
# HELP node_disk_io_time_seconds_total Total seconds spent doing I/Os.
# TYPE node_disk_io_time_seconds_total counter
node_disk_io_time_seconds_total{device="sda"} 2345.67
4.3 Blackbox Exporter:网络探测与黑盒监控
Blackbox Exporter 通过发起 HTTP、HTTPS、DNS、TCP、ICMP 等探测请求,从外部视角判断目标是否可达、响应时间是否正常。它适用于无法安装 Agent 的场景,如第三方 API、CDN、公网服务。
4.3.1 部署与配置
# 下载
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.24.0/blackbox_exporter-0.24.0.linux-amd64.tar.gz
tar xvf blackbox_exporter-0.24.0.linux-amd64.tar.gz
cd blackbox_exporter-0.24.0.linux-amd64
# 启动(默认端口 9115)
./blackbox_exporter --config.file=blackbox.yml
4.3.2 模块配置示例(blackbox.yml)
modules:
http_2xx:
prober: http
timeout: 5s
http:
valid_http_versions: ["HTTP/1.1", "HTTP/2"]
valid_status_codes: [200, 301, 302]
method: GET
follow_redirects: true
icmp_ping:
prober: icmp
timeout: 10s
icmp:
preferred_ip_protocol: "ip4"
tcp_connect:
prober: tcp
timeout: 5s
tcp:
query_response:
- expect: "^SSH-2.0-"
4.3.3 Prometheus 配置关联
scrape_configs:
- job_name: 'blackbox-http'
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- https://example.com
- https://api.example.com/health
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115
注意: Blackbox Exporter 的
/probe 端点需要 target 参数,Prometheus 通过 relabel 将目标地址传递过去。ICMP 探测需要 root 权限或设置 CAP_NET_RAW。
4.4 自定义 Exporter 开发思路
当官方 Exporter 无法满足需求时(如采集业务指标、中间件自定义指标),需要开发自定义 Exporter。推荐使用 Prometheus 官方客户端库,支持 Go、Python、Java、Ruby 等语言。
4.4.1 开发步骤(以 Python 为例)
- 安装依赖:
pip install prometheus_client - 定义指标: 使用
Gauge、Counter、Histogram、Summary类型 - 实现采集逻辑: 在回调函数中更新指标值
- 启动 HTTP 服务: 暴露
/metrics端点
4.4.2 代码示例
from prometheus_client import start_http_server, Gauge
import time
import psutil
# 定义指标
cpu_usage = Gauge('myapp_cpu_usage_percent', 'Current CPU usage percent')
memory_usage = Gauge('myapp_memory_usage_bytes', 'Current memory usage in bytes')
def collect_metrics():
"""采集函数,每次 scrape 时调用"""
cpu_usage.set(psutil.cpu_percent(interval=1))
memory_usage.set(psutil.virtual_memory().used)
if __name__ == '__main__':
# 启动 HTTP 服务,端口 8000
start_http_server(8000)
print("Custom Exporter running on http://0.0.0.0:8000/metrics")
while True:
collect_metrics()
time.sleep(15) # 采集间隔
4.4.3 指标类型选择指南
| 指标类型 | 适用场景 | 示例 |
|---|---|---|
| Counter | 只增不减的累计值 | 请求总数、错误总数 |
| Gauge | 可增可减的瞬时值 | CPU 使用率、连接数 |
| Histogram | 请求延迟分布统计 | API 响应时间分位数 |
| Summary | 预计算的分位数 | 请求延迟 P99 |
4.4.4 最佳实践
- 指标命名: 使用
应用名_模块名_单位格式,如myapp_db_query_duration_seconds - 标签(Label)控制: 避免高基数标签(如 UserID、IP),防止内存爆炸
- 采集性能: 采集逻辑不要阻塞 HTTP 响应,使用异步或缓存
- 注册多个 Collector: 实现
Collector接口,用REGISTRY.register()注册
4.5 本章小结
- Exporter 原理: 采集器 + 注册表 + HTTP Handler 三层架构,通过
/metrics端点暴露指标。 - Node Exporter: 主机级指标采集,支持按需启用 Collector,适合基础设施监控。
- Blackbox Exporter: 网络探测黑盒监控,支持 HTTP/HTTPS/DNS/TCP/ICMP,适合外部视角探测。
- 自定义 Exporter: 使用客户端库快速开发,注意指标类型选择、标签基数控制、采集性能优化。