Prometheus是什么
Prometheus是一個開源的系統監控和報警工具,特點是
- 多維數據模型(時序列數據由metric名和一組key/value組成)
- 在多維度上靈活的查詢語言(PromQl)
- 不依賴分布式存儲,單主節點工作.
- 通過基於HTTP的pull方式采集時序數據
- 可以通過push gateway進行時序列數據推送(pushing)
- 可以通過服務發現或者靜態配置去獲取要采集的目標服務器
- 多種可視化圖表及儀表盤支持
pull方式
Prometheus采集數據是用的pull也就是拉模型,通過HTTP協議去采集指標,只要應用系統能夠提供HTTP接口就可以接入監控系統,相比於私有協議或二進制協議來說開發、簡單。
push方式
對於定時任務這種短周期的指標采集,如果采用pull模式,可能造成任務結束了,Prometheus還沒有來得及采集,這個時候可以使用加一個中轉層,客戶端推數據到Push Gateway緩存一下,由Prometheus從push gateway pull指標過來。(需要額外搭建Push Gateway,同時需要新增job去從gateway采數據
)
組成及架構
- Prometheus server 主要負責數據采集和存儲,提供PromQL查詢語言的支持
- 客戶端sdk 官方提供的客戶端類庫有go、java、scala、python、ruby,其他還有很多第三方開發的類庫,支持nodejs、php、erlang等
- Push Gateway 支持臨時性Job主動推送指標的中間網關
- PromDash 使用rails開發的dashboard,用於可視化指標數據
-
exporters 支持其他數據源的指標導入到Prometheus,支持數據庫、硬件、消息中間件、存儲系統、http服務器、jmx等
-
alertmanager 實驗性組件、用來進行報警
- prometheus_cli 命令行工具
- 其他輔助性工具
架構圖如下:
默認配置
docker exec -it a9bd827a1d18 less /etc/prometheus/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). # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor' # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first.rules" # - "second.rules" # 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']
-
scrape_interval 這里是指每隔15秒鍾去抓取數據(
這里
) -
evaluation_interval 指的是計算rule的間隔
Push Gateway
pushgateway有單獨的鏡像
docker pull prom/pushgateway
對於喜歡用push模式的應用來說,可以專門搭建一個push gateway,來適配一下。
storage
prometheus使用了G家的LevelDB來做索引(PromSQL重度依賴LevelDB
),對於大量的采樣數據有自己的存儲層,Prometheus為每個時序數據創建一個本地文件,以1024byte大小的chunk來組織。
磁盤文件
Prometheus在storage.local.path指定的路徑存儲文件,默認為./data。關於chunk編碼有三種
- type 0
第一代的編碼格式,simple delta encoding
- type 1
目前默認的編碼格式,double-delta encoding
- type 2
variable bit-width encoding,facebook的時間序列數據庫Beringei采用的編碼方式
內存使用
prometheus在內存里保存了最近使用的chunks,具體chunks的最大個數可以通過storage.local.memory-chunks來設定,默認值為1048576,即1048576個chunk,大小為1G。 除了采用的數據,prometheus還需要對數據進行各種運算,因此整體內存開銷肯定會比配置的local.memory-chunks大小要來的大,因此官方建議要預留3倍的local.memory-chunks的內存大小。
As a rule of thumb, you should have at least three times more RAM available than needed by the memory chunks alone
可以通過server的metrics去查看prometheus_local_storage_memory_chunks以及process_resident_memory_byte兩個指標值。
-
prometheus_local_storage_memory_chunks
The current number of chunks in memory, excluding cloned chunks 目前內存中暴露的chunks的個數
-
process_resident_memory_byte
Resident memory size in bytes 駐存在內存的數據大小
-
prometheus_local_storage_persistence_urgency_score 介於0-1之間,當該值小於等於0.7時,prometheus離開rushed模式。 當大於0.8的時候,進入rushed模式
-
prometheus_local_storage_rushed_mode 1表示進入了rushed mode,0表示沒有。進入了rushed模式的話,prometheus會利用storage.local.series-sync-strategy以及storage.local.checkpoint-interval的配置加速chunks的持久化。
storage參數
docker run -p 9090:9090 \ -v /tmp/prometheus-data:/prometheus-data \ prom/prometheus \ -storage.local.retention 168h0m0s \ -storage.local.max-chunks-to-persist 3024288 \ -storage.local.memory-chunks=50502740 \ -storage.local.num-fingerprint-mutexes=300960
storage.local.memory-chunks
設定prometheus內存中保留的chunks的最大個數,默認為1048576,即為1G大小
storage.local.retention
用來配置采用數據存儲的時間,168h0m0s即為24*7小時,即1周
storage.local.series-file-shrink-ratio
用來控制序列文件rewrite的時機,默認是在10%的chunks被移除的時候進行rewrite,如果磁盤空間夠大,不想頻繁rewrite,可以提升該值,比如0.3,即30%的chunks被移除的時候才觸發rewrite。
storage.local.max-chunks-to-persist
該參數控制等待寫入磁盤的chunks的最大個數,如果超過這個數,Prometheus會限制采樣的速率,直到這個數降到指定閾值的95%。建議這個值設定為storage.local.memory-chunks的50%。Prometheus會盡力加速存儲速度,以避免限流這種情況的發送。
storage.local.num-fingerprint-mutexes
當prometheus server端在進行checkpoint操作或者處理開銷較大的查詢的時候,采集指標的操作會有短暫的停頓,這是因為prometheus給時間序列分配的mutexes可能不夠用,可以通過這個指標來增大預分配的mutexes,有時候可以設置到上萬個。
storage.local.series-sync-strategy
控制寫入數據之后,何時同步到磁盤,有'never', 'always', 'adaptive'. 同步操作可以降低因為操作系統崩潰帶來數據丟失,但是會降低寫入數據的性能。 默認為adaptive的策略,即不會寫完數據就立刻同步磁盤,會利用操作系統的page cache來批量同步。
storage.local.checkpoint-interval
進行checkpoint的時間間隔,即對尚未寫入到磁盤的內存chunks執行checkpoint操作。