何為 Prometheus Exporter?
Prometheus 監控基於一個很簡單的模型: 主動抓取目標的指標接口(HTTP 協議)獲取監控指標, 再存儲到本地或遠端的時序數據庫. Prometheus 對於指標接口有一套固定的格式要求, 格式大致如下:
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027
http_requests_total{method="post",code="400"} 3
而這樣的代理服務, 就稱作 Prometheus Exporter, 對於上面那些常見的情形, 社區早就寫好了成熟的 Exporter, 它們就是 node_exporter, redis_exporter 和 snmp_exporter。
為什么要寫 Exporter?
寫 exporter 可以把監控信息接進 Prometheus, 那為什么非要接進 Prometheus 呢?
集成到 Prometheus 監控之后, 借助 PromQL 強大的表達能力和 Alertmanager, Grafana 的強大生態, 我們不僅能實現所有監控信息的整合打通, 還能獲得更豐富的報警選擇和更強的看板能力.
如何為中間件開發Exporter
Prometheus 為開發這提供了客戶端工具,用於為自己的中間件開發Exporter,對接Prometheus 。
目前支持的客戶端
Go
Java
Python
Ruby
exporter demo:
一個簡單的 exporter
下面我將用 golang 實現一個簡單的 sample_exporter.go, 其代碼大致為:
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, exportData)
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
var exportData string = `# HELP sample_http_requests_total The total number of HTTP requests.
# TYPE sample_http_requests_total counter
sample_http_requests_total{method="post",code="200"} 1027 1395066363000
sample_http_requests_total{method="post",code="400"} 3 1395066363000
# Escaping in label values:
sample_msdos_file_access_time_seconds{path="C:\\DIR\\FILE.TXT",error="Cannot find file:\n\"FILE.TXT\""} 1.458255915e9
# Minimalistic line:
sample_metric_without_timestamp_and_labels 12.47
# A histogram, which has a pretty complex representation in the text format:
# HELP sample_http_request_duration_seconds A histogram of the request duration.
# TYPE sample_http_request_duration_seconds histogram
sample_http_request_duration_seconds_bucket{le="0.05"} 24054
sample_http_request_duration_seconds_bucket{le="0.1"} 33444
sample_http_request_duration_seconds_bucket{le="0.2"} 100392
sample_http_request_duration_seconds_bucket{le="0.5"} 129389
sample_http_request_duration_seconds_bucket{le="1"} 133988
sample_http_request_duration_seconds_bucket{le="+Inf"} 144320
sample_http_request_duration_seconds_sum 53423
sample_http_request_duration_seconds_count 144320
# Finally a summary, which has a complex representation, too:
# HELP sample_rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE sample_rpc_duration_seconds summary
sample_rpc_duration_seconds{quantile="0.01"} 3102
sample_rpc_duration_seconds{quantile="0.05"} 3272
sample_rpc_duration_seconds{quantile="0.5"} 4773
sample_rpc_duration_seconds{quantile="0.9"} 9001
sample_rpc_duration_seconds{quantile="0.99"} 76656
sample_rpc_duration_seconds_sum 1.7560473e+07
sample_rpc_duration_seconds_count 2693
`
當運行此程序,你訪問
http://localhost:8080/metrics, 將看到這樣的頁面:
與 Prometheus 集成
我們可以利用 Prometheus 的 static_configs 來收集 sample_exporter 的數據。
打開 prometheus.yml 文件, 在 scrape_configs 中添加如下配置:
- job_name: "sample"
static_configs:
- targets: ["127.0.0.1:8080"]
重啟加載配置,然后到 Prometheus Console 查詢,你會看到 simple_exporter 的數據。
Prometheus 官方文檔中 Writing Exporter 這篇寫得非常全面