Prometheus是什么
Prometheus是一個(gè)開(kāi)源的系統(tǒng)監(jiān)控和報(bào)警工具躏碳,特點(diǎn)是
- 多維數(shù)據(jù)模型(時(shí)序列數(shù)據(jù)由metric名和一組key/value組成)
- 在多維度上靈活的查詢語(yǔ)言(PromQl)
- 不依賴分布式存儲(chǔ)道伟,單主節(jié)點(diǎn)工作.
- 通過(guò)基于HTTP的pull方式采集時(shí)序數(shù)據(jù)
- 可以通過(guò)push gateway進(jìn)行時(shí)序列數(shù)據(jù)推送(pushing)
- 可以通過(guò)服務(wù)發(fā)現(xiàn)或者靜態(tài)配置去獲取要采集的目標(biāo)服務(wù)器
- 多種可視化圖表及儀表盤支持
pull方式
Prometheus采集數(shù)據(jù)是用的pull也就是拉模型,通過(guò)HTTP協(xié)議去采集指標(biāo)焰枢,只要應(yīng)用系統(tǒng)能夠提供HTTP接口就可以接入監(jiān)控系統(tǒng)诗轻,相比于私有協(xié)議或二進(jìn)制協(xié)議來(lái)說(shuō)開(kāi)發(fā)斗埂、簡(jiǎn)單槽卫。
push方式
對(duì)于定時(shí)任務(wù)這種短周期的指標(biāo)采集赠叼,如果采用pull模式止喷,可能造成任務(wù)結(jié)束了馆类,Prometheus還沒(méi)有來(lái)得及采集,這個(gè)時(shí)候可以使用加一個(gè)中轉(zhuǎn)層弹谁,客戶端推數(shù)據(jù)到Push Gateway緩存一下乾巧,由Prometheus從push gateway pull指標(biāo)過(guò)來(lái)。(需要額外搭建Push Gateway预愤,同時(shí)需要新增job去從gateway采數(shù)據(jù)
)
組成及架構(gòu)
Prometheus server
主要負(fù)責(zé)數(shù)據(jù)采集和存儲(chǔ)沟于,提供PromQL查詢語(yǔ)言的支持客戶端sdk
官方提供的客戶端類庫(kù)有g(shù)o、java植康、scala旷太、python、ruby销睁,其他還有很多第三方開(kāi)發(fā)的類庫(kù)供璧,支持nodejs、php冻记、erlang等Push Gateway
支持臨時(shí)性Job主動(dòng)推送指標(biāo)的中間網(wǎng)關(guān)PromDash
使用rails開(kāi)發(fā)的dashboard睡毒,用于可視化指標(biāo)數(shù)據(jù)exporters
支持其他數(shù)據(jù)源的指標(biāo)導(dǎo)入到Prometheus,支持?jǐn)?shù)據(jù)庫(kù)冗栗、硬件演顾、消息中間件、存儲(chǔ)系統(tǒng)隅居、http服務(wù)器钠至、jmx等alertmanager
實(shí)驗(yàn)性組件、用來(lái)進(jìn)行報(bào)警prometheus_cli
命令行工具其他輔助性工具
架構(gòu)圖如下:
默認(rèn)配置
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秒鐘去抓取數(shù)據(jù)(這里
)evaluation_interval
指的是計(jì)算rule的間隔
Push Gateway
pushgateway有單獨(dú)的鏡像
docker pull prom/pushgateway
對(duì)于喜歡用push模式的應(yīng)用來(lái)說(shuō)胎源,可以專門搭建一個(gè)push gateway棉钧,來(lái)適配一下。
storage
prometheus使用了G家的LevelDB來(lái)做索引(PromSQL重度依賴LevelDB
)乒融,對(duì)于大量的采樣數(shù)據(jù)有自己的存儲(chǔ)層掰盘,Prometheus為每個(gè)時(shí)序數(shù)據(jù)創(chuàng)建一個(gè)本地文件摄悯,以1024byte大小的chunk來(lái)組織。
磁盤文件
Prometheus在storage.local.path指定的路徑存儲(chǔ)文件愧捕,默認(rèn)為./data奢驯。關(guān)于chunk編碼有三種
- type 0
第一代的編碼格式,simple delta encoding
- type 1
目前默認(rèn)的編碼格式次绘,double-delta encoding
- type 2
variable bit-width encoding瘪阁,facebook的時(shí)間序列數(shù)據(jù)庫(kù)Beringei采用的編碼方式
內(nèi)存使用
prometheus在內(nèi)存里保存了最近使用的chunks,具體chunks的最大個(gè)數(shù)可以通過(guò)storage.local.memory-chunks來(lái)設(shè)定邮偎,默認(rèn)值為1048576管跺,即1048576個(gè)chunk,大小為1G禾进。
除了采用的數(shù)據(jù)豁跑,prometheus還需要對(duì)數(shù)據(jù)進(jìn)行各種運(yùn)算,因此整體內(nèi)存開(kāi)銷肯定會(huì)比配置的local.memory-chunks大小要來(lái)的大泻云,因此官方建議要預(yù)留3倍的local.memory-chunks的內(nèi)存大小艇拍。
As a rule of thumb, you should have at least three times more RAM available than needed by the memory chunks alone
可以通過(guò)server的metrics去查看prometheus_local_storage_memory_chunks以及process_resident_memory_byte兩個(gè)指標(biāo)值。
- prometheus_local_storage_memory_chunks
The current number of chunks in memory, excluding cloned chunks
目前內(nèi)存中暴露的chunks的個(gè)數(shù)
- process_resident_memory_byte
Resident memory size in bytes
駐存在內(nèi)存的數(shù)據(jù)大小
prometheus_local_storage_persistence_urgency_score
介于0-1之間宠纯,當(dāng)該值小于等于0.7時(shí)卸夕,prometheus離開(kāi)rushed模式。
當(dāng)大于0.8的時(shí)候婆瓜,進(jìn)入rushed模式prometheus_local_storage_rushed_mode
1表示進(jìn)入了rushed mode快集,0表示沒(méi)有。進(jìn)入了rushed模式的話廉白,prometheus會(huì)利用storage.local.series-sync-strategy以及storage.local.checkpoint-interval的配置加速chunks的持久化个初。
storage參數(shù)
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
設(shè)定prometheus內(nèi)存中保留的chunks的最大個(gè)數(shù),默認(rèn)為1048576蒙秒,即為1G大小
storage.local.retention
用來(lái)配置采用數(shù)據(jù)存儲(chǔ)的時(shí)間勃黍,168h0m0s即為24*7小時(shí)宵统,即1周
storage.local.series-file-shrink-ratio
用來(lái)控制序列文件rewrite的時(shí)機(jī)晕讲,默認(rèn)是在10%的chunks被移除的時(shí)候進(jìn)行rewrite,如果磁盤空間夠大马澈,不想頻繁rewrite瓢省,可以提升該值,比如0.3痊班,即30%的chunks被移除的時(shí)候才觸發(fā)rewrite勤婚。
storage.local.max-chunks-to-persist
該參數(shù)控制等待寫入磁盤的chunks的最大個(gè)數(shù),如果超過(guò)這個(gè)數(shù)涤伐,Prometheus會(huì)限制采樣的速率馒胆,直到這個(gè)數(shù)降到指定閾值的95%缨称。建議這個(gè)值設(shè)定為storage.local.memory-chunks的50%。Prometheus會(huì)盡力加速存儲(chǔ)速度祝迂,以避免限流這種情況的發(fā)送睦尽。
storage.local.num-fingerprint-mutexes
當(dāng)prometheus server端在進(jìn)行checkpoint操作或者處理開(kāi)銷較大的查詢的時(shí)候,采集指標(biāo)的操作會(huì)有短暫的停頓型雳,這是因?yàn)閜rometheus給時(shí)間序列分配的mutexes可能不夠用当凡,可以通過(guò)這個(gè)指標(biāo)來(lái)增大預(yù)分配的mutexes,有時(shí)候可以設(shè)置到上萬(wàn)個(gè)纠俭。
storage.local.series-sync-strategy
控制寫入數(shù)據(jù)之后沿量,何時(shí)同步到磁盤,有'never', 'always', 'adaptive'. 同步操作可以降低因?yàn)椴僮飨到y(tǒng)崩潰帶來(lái)數(shù)據(jù)丟失冤荆,但是會(huì)降低寫入數(shù)據(jù)的性能朴则。
默認(rèn)為adaptive的策略,即不會(huì)寫完數(shù)據(jù)就立刻同步磁盤钓简,會(huì)利用操作系統(tǒng)的page cache來(lái)批量同步佛掖。
storage.local.checkpoint-interval
進(jìn)行checkpoint的時(shí)間間隔,即對(duì)尚未寫入到磁盤的內(nèi)存chunks執(zhí)行checkpoint操作涌庭。