架構(gòu)
Prometheus 是一個(gè)開源的監(jiān)控框架,它通過不同的組件完成數(shù)據(jù)的采集丸冕,數(shù)據(jù)的存儲(chǔ)嚎研,告警筒繁,其中Prometheus server只提供了數(shù)據(jù)存儲(chǔ)(time series data)许饿,數(shù)據(jù)的處理(提供了豐富的查詢語(yǔ)法[查詢阳欲,統(tǒng)計(jì),聚合等等]),數(shù)據(jù)則通過眾多的插件(prometheus稱之為exporters)來暴露一個(gè)http服務(wù)的接口給Prometheus定時(shí)抓取球化, 告警則通過Altermanger秽晚。
組件介紹:
- Prometheus server 包含數(shù)據(jù)采集scrapes job, stores time series data;
- push gateway : Prometheus server的一個(gè)代理節(jié)點(diǎn), 當(dāng)一些節(jié)點(diǎn)沒有提供HTTP endpoint時(shí)赊窥,可將數(shù)據(jù)push到代理節(jié)點(diǎn)爆惧,Prometheus會(huì)去代理節(jié)點(diǎn)獲取數(shù)據(jù);
- exporters: 數(shù)據(jù)采集插件, 暴露一個(gè)http服務(wù)的接口給Prometheus server定時(shí)抓取;
- alertmanager: 報(bào)警插件狸页;
部署
Note that Prometheus by default uses around 3GB in memory. If you have a smaller machine, you can tune Prometheus to use less memory. For details, see the memory usage documentation.
docker run -p 9090:9090 -d -v ~/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -v ~/prometheus/data:/prometheus prom/prometheus
# 容器中默認(rèn)的啟動(dòng)命令
CMD [ "--config.file=/etc/prometheus/prometheus.yml", \
"--storage.tsdb.path=/prometheus", \
"--web.console.libraries=/usr/share/prometheus/console_libraries", \
"--web.console.templates=/usr/share/prometheus/consoles" ]
Prometheus 數(shù)據(jù)模型
Prometheus所有的存儲(chǔ)都是按時(shí)間序列去實(shí)現(xiàn)的锨能,相同的metrics
和label
組成一條時(shí)間序列,不同的label
表示不同的時(shí)間序列芍耘。為了支持一些查詢址遇,有時(shí)還會(huì)臨時(shí)產(chǎn)生一些時(shí)間序列存儲(chǔ)。
<metric name>{<label name>=<label value>, ...} float64
example:
api_http_requests_total{method="POST", handler="/messages"} 10
metrics 和 labels 命名最佳實(shí)踐
jobs and instances
在 Prometheus 的術(shù)語(yǔ)中斋竞,一個(gè)可以 scrape 的節(jié)點(diǎn)成為一個(gè) instance, 一個(gè) job 中有多個(gè) instance倔约,例如:
job: api-server
instance1: 1.2.3.4:5670
instance2: 1.2.3.4:5671
job, instance 會(huì)自動(dòng)地以 label 的形式添加到時(shí)間序列的數(shù)據(jù)中:
up{job="<job-name>", instance="<instance-id>"}: 1
同時(shí) prometheus 的 target 實(shí)例都會(huì)包含一些默認(rèn)的標(biāo)簽:
一般來說,Target以
__
作為前置的標(biāo)簽是在系統(tǒng)內(nèi)部使用的坝初,因此這些標(biāo)簽不會(huì)被寫入到樣本數(shù)據(jù)中
-
__address__
: prometheus scrape target 的地址<host>:<port>
-
__scheme__
: scrape 的協(xié)議 -
__metrics_path__
: scrape 的路徑 -
__param_<name>
:scrape 的請(qǐng)求參數(shù)
Prometheus Metrics 的類型
counter
- 用于累計(jì)浸剩,如:請(qǐng)求次數(shù),任務(wù)完成數(shù)鳄袍,錯(cuò)誤發(fā)生次數(shù)绢要;
- 只增加,不減少
- 重啟進(jìn)程后拗小,會(huì)被重置
Gauge
- 常規(guī)數(shù)值重罪,例如:溫度變化,內(nèi)存使用變化哀九;
- 可增加剿配,可減少
- 重啟進(jìn)程后,會(huì)被重置
Histogram
柱狀圖阅束,常用于跟蹤事件發(fā)生的規(guī)模呼胚,例如:請(qǐng)求耗時(shí),相應(yīng)大小息裸。它會(huì)對(duì)記錄的內(nèi)容進(jìn)行分組, 提供 count 和 sum 全部值的功能蝇更。如果定于一個(gè) metrics 類型為 Histogram, 則 Prometheus 系統(tǒng)會(huì)自動(dòng)生成三個(gè)對(duì)應(yīng)的指標(biāo):
- [metric_name]_bucket{le="上邊界"},這個(gè)值為小于等于上邊界的所有采樣點(diǎn)數(shù)量界牡。
- [metric_name]_sum
- [metric_name]_count
下面來看一個(gè) prometheus metrics 中的一個(gè) histogram 類型的數(shù)據(jù):
# HELP prometheus_tsdb_compaction_chunk_range Final time range of chunks on their first compaction
# TYPE prometheus_tsdb_compaction_chunk_range histogram
prometheus_tsdb_compaction_chunk_range_bucket{le="100"} 0 # prometheus_tsdb_compaction_chunk_range 小于或等于100的次數(shù)
prometheus_tsdb_compaction_chunk_range_bucket{le="400"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="1600"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="6400"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="25600"} 80
prometheus_tsdb_compaction_chunk_range_bucket{le="102400"} 974
prometheus_tsdb_compaction_chunk_range_bucket{le="409600"} 1386
prometheus_tsdb_compaction_chunk_range_bucket{le="1.6384e+06"} 112157
prometheus_tsdb_compaction_chunk_range_bucket{le="6.5536e+06"} 821535
prometheus_tsdb_compaction_chunk_range_bucket{le="2.62144e+07"} 821545
prometheus_tsdb_compaction_chunk_range_bucket{le="+Inf"} 821545
prometheus_tsdb_compaction_chunk_range_sum 1.334532601458e+12
prometheus_tsdb_compaction_chunk_range_count 821545
這個(gè)直方圖的 X 軸為 le 的值簿寂,Y 軸為次數(shù);如果需要得到百分位數(shù)宿亡,可以使用 histogram_quantile() 函數(shù):
# 查詢 prometheus_tsdb_compaction_chunk_range 95 百分位
histogram_quantile(0.95, prometheus_tsdb_compaction_chunk_range_bucket)
Summary
- 跟Histogram類似常遂,常用于跟蹤事件發(fā)生的規(guī)模;
- 它提供一個(gè)quantiles的功能挽荠,可以按%比劃分跟蹤的結(jié)果克胳。例如:quantile取值0.95平绩,表示取采樣值里面的95%數(shù)據(jù)。
還是來看一個(gè) prometheus metrics 中的一個(gè) Summary 類型的數(shù)據(jù), 它與 histogram 的不同點(diǎn)是直接返回了 百分位的值:
# HELP prometheus_tsdb_head_gc_duration_seconds Runtime of garbage collection in the head block.
# TYPE prometheus_tsdb_head_gc_duration_seconds summary
prometheus_tsdb_head_gc_duration_seconds{quantile="0.5"} 0.03043428
prometheus_tsdb_head_gc_duration_seconds{quantile="0.9"} 0.03043428
prometheus_tsdb_head_gc_duration_seconds{quantile="0.99"} 0.03043428
prometheus_tsdb_head_gc_duration_seconds_sum 0.70902013
prometheus_tsdb_head_gc_duration_seconds_count 11
Prometheus Recoding rules
Prometheus 支持兩種不同類型的 rule, 一種是 Recoding rules漠另, 另一種是 Alert rules
Recoding rules 用來優(yōu)化性能捏雌,通常在使用聯(lián)邦集群時(shí),中心的 Prometheus 接收各個(gè) Prometheus 聚合后的數(shù)據(jù)笆搓,詳情見:https://www.robustperception.io/federation-what-is-it-good-for
但某些 PromQL 比較復(fù)雜且計(jì)算量較大時(shí)性湿,直接使用 PromQL 可能會(huì)導(dǎo)致 Prometheus 響應(yīng)超時(shí)的情況,這是就使用 Recoding rules 在后臺(tái)將計(jì)算聚合過的數(shù)據(jù)存入數(shù)據(jù)庫(kù)中满败,查詢時(shí)直接使用這些聚合后的數(shù)據(jù)肤频。
groups:
- name: example
rules:
- record: job:http_inprogress_requests:sum
expr: sum(http_inprogess_requests) by (job)
計(jì)算的頻率在 global.evaluation_interval
中定義:
global:
[ evaluation_interval: <duration> | default = 1m ]
Prometheus alert rules
ALERT <alert name>
IF <expression> # PromQL 查詢的值
[ FOR <duration> ] # 觸發(fā)告警的持續(xù)時(shí)間
[ LABELS <label set> ]
[ ANNOTATIONS <label set> ]
example:
# Alert for any instance that is unreachable for >5 minutes.
ALERT InstanceDown
IF up == 0
FOR 5m
LABELS { severity = "page" }
ANNOTATIONS {
summary = "Instance {{ $labels.instance }} down",
description = "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes.",
}
# Alert for any instance that have a median request latency >1s.
ALERT APIHighRequestLatency
IF api_http_request_latencies_second{quantile="0.5"} > 1
FOR 1m
ANNOTATIONS {
summary = "High request latency on {{ $labels.instance }}",
description = "{{ $labels.instance }} has a median request latency above 1s (current value: {{ $value }}s)",
}
reload
允許通過 Web 的方式:--web.enable-lifecycle
POST /-/reload