1 基本原理
Prometheus的基本原理是通過HTTP協(xié)議周期性抓取被監(jiān)控組件target的狀態(tài),任意組件只要提供相應的HTTP接口就可以接入監(jiān)控。不需任何SDK或者其他的集成過程。
輸出監(jiān)控信息的HTTP接口叫exporter潮改,目前開源組件大都有exporter可以直接使用,比如Haproxy、Nginx凝果、MySQL、redis睦尽、rabbitmq器净、Linux系統(tǒng)信息(包括磁盤、內存当凡、CPU山害、網(wǎng)絡等)。
Server 主要負責數(shù)據(jù)采集和存儲宁玫,提供PromQL查詢語言的支持粗恢。
Alertmanager 警告管理器,用來進行報警欧瘪。支持Prometheus的查詢語句眷射,提供靈活的報警方式(待完善)。
Push Gateway 支持臨時性Job主動推送指標的中間網(wǎng)關佛掖。
Prometheus Federation聯(lián)邦機制:一般分為Cross-service federation與Hierarchical federation妖碉。
prometheus.yml
scrape_configs:
- job_name: 'federate'
scrape_interval: 15s
honor_labels: true #避免監(jiān)控指標沖突
metrics_path: '/federate'
params:
'match[]':
- '{job="prometheus"}'
- '{__name__=~"job:.*"}'
- '{job=~"prometheus.*"}'
- '{job="docker"}'
- '{job="node"}'
static_configs:
- targets:
- 'source-prometheus-1:9090'
- 'source-prometheus-2:9090'
grafana UI:http://127.0.0.1:3000
prometheus UI:http://127.0.0.1:9090
pushgateway UI:http://127.0.0.1:9091
2 特點
- 多維度時序數(shù)據(jù)模型,名稱metric+key/value芥被。
- PromQL靈活的查詢語言欧宜。
- 不依賴分布式存儲,單服務器節(jié)點是自主的。
- 基于HTTP的pull方式采集時序數(shù)據(jù)清焕。
- 可以通過中間網(wǎng)關進行時序列數(shù)據(jù)推送溯街。
- 通過服務發(fā)現(xiàn)或者靜態(tài)配置來發(fā)現(xiàn)target目標服務對象。
- 支持多種圖表和界面展示夏漱,比如Grafana等。
3 Metric類型:
- Counter: 累加metric顶捷,如請求的個數(shù)挂绰,錯誤數(shù)等
- Gauge: 常規(guī)metric,可任意加減服赎。其為瞬時的葵蒂,與時間沒有關系交播,可以任意變化。
- Histogram: 柱狀圖践付,用于觀察結果采樣秦士,分組及統(tǒng)計,如:請求持續(xù)時間荔仁,響應大小伍宦。
- Summary: 類似Histogram,用于表示一段時間內數(shù)據(jù)采樣結果乏梁,其直接存儲quantile數(shù)據(jù)次洼,而不是根據(jù)統(tǒng)計區(qū)間計算出來的。不需要計算遇骑,直接存儲結果卖毁。
4 PromQL (Prometheus Query Language)
Prometheus 自己開發(fā)的數(shù)據(jù)查詢 DSL 語言。
查詢結果類型:
- 瞬時數(shù)據(jù) (Instant vector): 包含一組時序落萎,每個時序只有一個點亥啦,例如:http_requests_total
- 區(qū)間數(shù)據(jù) (Range vector): 包含一組時序,每個時序有多個點练链,例如:http_requests_total[5m]
- 純量數(shù)據(jù) (Scalar): 純量只有一個數(shù)字翔脱,沒有時序,例如:count(http_requests_total)
標簽查詢:logback_events_total{level=~"in.*"}
QPS計算:rate(http_requests_total[5m])
或irate(http_requests_total[5m])
媒鼓,后者適用變化率大場景届吁。
其他函數(shù):count
,sum
,svg
等
5 配置
啟動時,可以加載運行參數(shù)-config.file指定配置文件绿鸣, 默認為程序根目錄下prometheus.yml疚沐。
全局配置global主要有四個屬性:
scrape_interval: 拉取 targets 的默認時間間隔。
scrape_timeout: 拉取一個 target 的超時時間潮模。
evaluation_interval: 執(zhí)行 rules 的時間間隔亮蛔。
external_labels: 額外的屬性,會添加到拉取的數(shù)據(jù)并存到數(shù)據(jù)庫中擎厢。
prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# metrics collect
remote_write:
- url: "http://10.0.0.153:9201/write"
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
- "myrule.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
# 動態(tài)加載模式究流,新增target無需重啟服務
- job_name: 'linux'
file_sd_configs:
# - refresh_interval: 1m
- files:
- ./conf/node-linux-*.json
- job_name: 'windows'
static_configs:
- targets: ['10.0.0..89:9182']
- targets: ['10.0.0.95:9182']
- job_name: 'mysql'
scrape_interval: 1m
file_sd_configs:
- files:
- ./conf-mysql/*.json
myrule.yml
groups:
- name: zt_custom
rules:
- record: mysql_csc_slowquery_irate_2m
expr: irate(mysql_csc_slowquery [2m])
./conf/node-linux-finance.json
[
{
"targets": ["10.0.0.80:9100"],
"labels":{
"env":"pro",
"region":"dbjf",
"instance":"finance-80"
}
},
{
"targets": ["10.0.0.90:9100"],
"labels":{
"env":"pro",
"region":"dbjf",
"instance":"finance-90"
}
}
]
6 exporter
詳見https://prometheus.io/docs/instrumenting/exporters/#exporters-and-integrations
linux系統(tǒng)、mysql动遭、redis等等都有相應的exporter梯嗽,按照說明運行代理即可。