exporter

何爲 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 這篇寫得很是全面 
相關文章
相關標籤/搜索