架構
Prometheus 是一個開源的監控框架,它通過不同的組件完成數據的采集,數據的存儲,告警,其中Prometheus server只提供了數據存儲(time series data),數據的處理(提供了豐富的查詢語法[查詢,統計,聚合等等]),數據則通過眾多的插件(prometheus稱之為exporters)來暴露一個http服務的接口給Prometheus定時抓取, 告警則通過Altermanger。
組件介紹:
- Prometheus server 包含數據采集scrapes job, stores time series data;
- push gateway : Prometheus server的一個代理節點, 當一些節點沒有提供HTTP endpoint時,可將數據push到代理節點,Prometheus會去代理節點獲取數據;
- exporters: 數據采集插件, 暴露一個http服務的接口給Prometheus server定時抓取;
- alertmanager: 報警插件;
部署
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
# 容器中默認的啟動命令
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 數據模型
Prometheus所有的存儲都是按時間序列去實現的,相同的metrics
和label
組成一條時間序列,不同的label
表示不同的時間序列。為了支持一些查詢,有時還會臨時產生一些時間序列存儲。
<metric name>{<label name>=<label value>, ...} float64
example:
api_http_requests_total{method="POST", handler="/messages"} 10
metrics 和 labels 命名最佳實踐
jobs and instances
在 Prometheus 的術語中,一個可以 scrape 的節點成為一個 instance, 一個 job 中有多個 instance,例如:
job: api-server
instance1: 1.2.3.4:5670
instance2: 1.2.3.4:5671
job, instance 會自動地以 label 的形式添加到時間序列的數據中:
up{job="<job-name>", instance="<instance-id>"}: 1
同時 prometheus 的 target 實例都會包含一些默認的標簽:
一般來說,Target以
__
作為前置的標簽是在系統內部使用的,因此這些標簽不會被寫入到樣本數據中
-
__address__
: prometheus scrape target 的地址<host>:<port>
-
__scheme__
: scrape 的協議 -
__metrics_path__
: scrape 的路徑 -
__param_<name>
:scrape 的請求參數
Prometheus Metrics 的類型
counter
- 用于累計,如:請求次數,任務完成數,錯誤發生次數;
- 只增加,不減少
- 重啟進程后,會被重置
Gauge
- 常規數值,例如:溫度變化,內存使用變化;
- 可增加,可減少
- 重啟進程后,會被重置
Histogram
柱狀圖,常用于跟蹤事件發生的規模,例如:請求耗時,相應大小。它會對記錄的內容進行分組, 提供 count 和 sum 全部值的功能。如果定于一個 metrics 類型為 Histogram, 則 Prometheus 系統會自動生成三個對應的指標:
- [metric_name]_bucket{le="上邊界"},這個值為小于等于上邊界的所有采樣點數量。
- [metric_name]_sum
- [metric_name]_count
下面來看一個 prometheus metrics 中的一個 histogram 類型的數據:
# 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的次數
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
這個直方圖的 X 軸為 le 的值,Y 軸為次數;如果需要得到百分位數,可以使用 histogram_quantile() 函數:
# 查詢 prometheus_tsdb_compaction_chunk_range 95 百分位
histogram_quantile(0.95, prometheus_tsdb_compaction_chunk_range_bucket)
Summary
- 跟Histogram類似,常用于跟蹤事件發生的規模;
- 它提供一個quantiles的功能,可以按%比劃分跟蹤的結果。例如:quantile取值0.95,表示取采樣值里面的95%數據。
還是來看一個 prometheus metrics 中的一個 Summary 類型的數據, 它與 histogram 的不同點是直接返回了 百分位的值:
# 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 用來優化性能,通常在使用聯邦集群時,中心的 Prometheus 接收各個 Prometheus 聚合后的數據,詳情見:https://www.robustperception.io/federation-what-is-it-good-for
但某些 PromQL 比較復雜且計算量較大時,直接使用 PromQL 可能會導致 Prometheus 響應超時的情況,這是就使用 Recoding rules 在后臺將計算聚合過的數據存入數據庫中,查詢時直接使用這些聚合后的數據。
groups:
- name: example
rules:
- record: job:http_inprogress_requests:sum
expr: sum(http_inprogess_requests) by (job)
計算的頻率在 global.evaluation_interval
中定義:
global:
[ evaluation_interval: <duration> | default = 1m ]
Prometheus alert rules
ALERT <alert name>
IF <expression> # PromQL 查詢的值
[ FOR <duration> ] # 觸發告警的持續時間
[ 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