导出器概述 #
一、什么是导出器 #
1.1 导出器定义 #
text
导出器定义:
┌─────────────────────────────────────────────┐
│ Exporter(导出器) │
├─────────────────────────────────────────────┤
│ • 将现有系统指标转换为Prometheus格式 │
│ • 暴露/metrics端点 │
│ • 提供HTTP服务供Prometheus拉取 │
│ • 连接被监控系统与Prometheus │
└─────────────────────────────────────────────┘
工作流程:
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ 被监控系统 │ ──> │ Exporter │ <── │ Prometheus │
│ │ │ /metrics │ │ Server │
└─────────────┘ └─────────────┘ └─────────────┘
│ │ │
│ 获取指标 │ 暴露指标 │ 拉取指标
▼ ▼ ▼
1.2 导出器类型 #
text
导出器分类:
┌─────────────────────────────────────────────┐
│ 1. 直接暴露型 │
├─────────────────────────────────────────────┤
│ • 应用内置/metrics端点 │
│ • 无需额外组件 │
│ • 示例:Prometheus、Grafana、Etcd │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 2. 独立导出器 │
├─────────────────────────────────────────────┤
│ • 独立运行的程序 │
│ • 连接被监控系统获取指标 │
│ • 示例:Node Exporter、MySQL Exporter │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 3. 客户端库集成 │
├─────────────────────────────────────────────┤
│ • 应用代码中集成客户端库 │
│ • 直接暴露应用指标 │
│ • 示例:Go、Java、Python客户端库 │
└─────────────────────────────────────────────┘
二、指标格式 #
2.1 文本格式 #
text
Prometheus文本格式:
# HELP 指标说明
# TYPE 指标类型
指标名称{标签} 值 [时间戳]
示例:
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="GET",status="200"} 1000
http_requests_total{method="POST",status="201"} 500
# HELP node_memory_MemTotal_bytes Total memory in bytes.
# TYPE node_memory_MemTotal_bytes gauge
node_memory_MemTotal_bytes 16777216000
# HELP http_request_duration_seconds HTTP request duration in seconds.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.1"} 100
http_request_duration_seconds_bucket{le="0.5"} 300
http_request_duration_seconds_bucket{le="1.0"} 450
http_request_duration_seconds_bucket{le="+Inf"} 600
http_request_duration_seconds_sum 250.5
http_request_duration_seconds_count 600
2.2 格式规范 #
text
格式规范:
┌─────────────────────────────────────────────┐
│ 注释 │
├─────────────────────────────────────────────┤
│ # HELP <指标名> <说明> │
│ # TYPE <指标名> <类型> │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 指标行 │
├─────────────────────────────────────────────┤
│ <指标名>[{<标签>}]] <值> [时间戳] │
│ │
│ 指标名:[a-zA-Z_:][a-zA-Z0-9_:]* │
│ 标签名:[a-zA-Z_][a-zA-Z0-9_]* │
│ 标签值:任意UTF-8字符 │
│ 值:浮点数 │
│ 时间戳:可选,Unix时间戳(毫秒) │
└─────────────────────────────────────────────┘
三、常用导出器 #
3.1 基础设施导出器 #
text
基础设施导出器:
┌─────────────────────────────────────────────┐
│ Node Exporter │
├─────────────────────────────────────────────┤
│ • 主机监控 │
│ • CPU、内存、磁盘、网络 │
│ • 端口:9100 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ SNMP Exporter │
├─────────────────────────────────────────────┤
│ • 网络设备监控 │
│ • 路由器、交换机 │
│ • 端口:9116 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ IPMI Exporter │
├─────────────────────────────────────────────┤
│ • 硬件监控 │
│ • 温度、风扇、电源 │
│ • 端口:9290 │
└─────────────────────────────────────────────┘
3.2 数据库导出器 #
text
数据库导出器:
┌─────────────────────────────────────────────┐
│ MySQL Exporter │
├─────────────────────────────────────────────┤
│ • MySQL监控 │
│ • 连接数、查询数、慢查询 │
│ • 端口:9104 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ PostgreSQL Exporter │
├─────────────────────────────────────────────┤
│ • PostgreSQL监控 │
│ • 连接数、事务数、锁 │
│ • 端口:9187 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ MongoDB Exporter │
├─────────────────────────────────────────────┤
│ • MongoDB监控 │
│ • 连接数、操作数、内存 │
│ • 端口:9216 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ Redis Exporter │
├─────────────────────────────────────────────┤
│ • Redis监控 │
│ • 内存、连接数、命令数 │
│ • 端口:9121 │
└─────────────────────────────────────────────┘
3.3 消息队列导出器 #
text
消息队列导出器:
┌─────────────────────────────────────────────┐
│ Kafka Exporter │
├─────────────────────────────────────────────┤
│ • Kafka监控 │
│ • 消息数、分区、消费者 │
│ • 端口:9308 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ RabbitMQ Exporter │
├─────────────────────────────────────────────┤
│ • RabbitMQ监控 │
│ • 队列、消息、连接 │
│ • 端口:9419 │
└─────────────────────────────────────────────┘
四、配置导出器 #
4.1 Prometheus配置 #
yaml
# prometheus.yml
scrape_configs:
# Node Exporter
- job_name: 'node-exporter'
static_configs:
- targets:
- 'node1:9100'
- 'node2:9100'
- 'node3:9100'
labels:
env: 'production'
# MySQL Exporter
- job_name: 'mysql-exporter'
static_configs:
- targets:
- 'mysql-exporter:9104'
# Redis Exporter
- job_name: 'redis-exporter'
static_configs:
- targets:
- 'redis-exporter:9121'
# 应用内置指标
- job_name: 'myapp'
static_configs:
- targets:
- 'app1:8080'
- 'app2:8080'
4.2 服务发现配置 #
yaml
# Kubernetes服务发现
scrape_configs:
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
target_label: __address__
# Consul服务发现
scrape_configs:
- job_name: 'consul-services'
consul_sd_configs:
- server: 'consul:8500'
services: []
relabel_configs:
- source_labels: [__meta_consul_tags]
regex: '.*,prometheus,.*'
action: keep
五、导出器最佳实践 #
5.1 部署建议 #
text
部署建议:
┌─────────────────────────────────────────────┐
│ 1. 资源隔离 │
├─────────────────────────────────────────────┤
│ • 导出器独立部署 │
│ • 避免与业务应用竞争资源 │
│ • 设置资源限制 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 2. 高可用部署 │
├─────────────────────────────────────────────┤
│ • 关键导出器多实例部署 │
│ • 使用负载均衡 │
│ • 健康检查 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 3. 安全配置 │
├─────────────────────────────────────────────┤
│ • 限制访问IP │
│ • 使用TLS加密 │
│ • 添加认证 │
└─────────────────────────────────────────────┘
5.2 性能优化 #
text
性能优化:
┌─────────────────────────────────────────────┐
│ 1. 控制指标数量 │
├─────────────────────────────────────────────┤
│ • 过滤不需要的指标 │
│ • 使用metric_relabel_configs │
│ • 避免高基数标签 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 2. 调整采集频率 │
├─────────────────────────────────────────────┤
│ • 根据需求设置scrape_interval │
│ • 重要指标高频采集 │
│ • 历史指标低频采集 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 3. 缓存优化 │
├─────────────────────────────────────────────┤
│ • 导出器内部缓存 │
│ • 减少对被监控系统的压力 │
│ • 合理设置缓存时间 │
└─────────────────────────────────────────────┘
六、总结 #
导出器要点:
| 类型 | 说明 |
|---|---|
| 直接暴露型 | 应用内置/metrics |
| 独立导出器 | 独立程序转换指标 |
| 客户端库 | 代码集成暴露指标 |
常用导出器:
| 导出器 | 端口 | 用途 |
|---|---|---|
| Node Exporter | 9100 | 主机监控 |
| MySQL Exporter | 9104 | MySQL监控 |
| Redis Exporter | 9121 | Redis监控 |
下一步,让我们学习Node Exporter!
最后更新:2026-03-27