Prometheus可以通過命令行參數和配置文件進行配置。雖然命令行參數可以配置一些不可變的系統參數(例如存儲位置,保留在磁盤和內存中的數據量等),但配置文件能夠定義與抓取作業及其實例相關的所有內容,以及哪些規則文件可以被加載等。
要查看所有可用的命令行參數,請運行./prometheus -h。
Prometheus可以在運行時重新加載其配置。如果新配置的格式不正確,則不會應用更改。如果想要重載Prometheus配置,可以給Prometheus的主進程發送SIGHUP信號或者發送post請求給指定端點 /-/reload(前提條件是在啓動時加上--web.enable-lifecycle啓動參數)
Prometheus的配置文件是YAML格式,當我們運行prometheus二進制文件(windows是prometheus.exe可執行文件)時,我們通過參數可指定一個配置文件。
./prometheus --config.file=prometheus.yml
Prometheus的解壓包裏自帶了一個默認的配置文件prometheus.yml。讓我們來看一下:
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).
# 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"
# 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']
在這個缺省的配置文件裏定義了4個單元:global、alerting、rule_files和scrape_configs。
Global
global包含用於控制prometheus服務器行爲的全局設置。
scrape_interval參數指定Prometheus抓取應用程序數據的間隔爲15秒。
可以爲特定的服務設定不同的參數來覆蓋這個全局參數。但是最好不要這樣做,在整個服務器上保持一個全局性間隔。這樣可以確保所有時間序列數據具有相同的採集間隔,可以組合在一起計算。如果覆蓋全局採集間隔,則可能由於嘗試比較不同間隔收集的數據而導致結果不一致。
evaluation_interval參數指定Prometheus評估規則的頻率。
規則可以分爲兩大類:記錄規則和警報規則:
- 記錄規則-允許您根據預先記錄表達式抓取監控數據,並將其結果保存爲派生的時間序列數據。
- 警報規則-允許你定義警報條件。
通過這個參數,Prometheus將每隔15秒(重新)評估這些規則。
Altering
altering配置Prometheus的警報服務器。Prometheus的警報由一個名爲AlertManager的獨立工具提供。
AlertManager是一個可以集羣化的獨立警報管理工具。
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
Prometheus還支持對AlertManager的服務發現,例如,你可以查詢外部源(如Consul服務器)以返回可用的AlertManager列表,而不是單獨指定每個AlertManager。
Rule files
rule_files指定了一組規則文件,可以包含記錄規則或警報規則。
規則文件的語法是:
groups:
[ - <rule_group> ]
一個簡單的記錄規則文件是:
groups:
- name: example
rules:
- record: job:http_inprogress_requests:sum
expr: sum(http_inprogress_requests) by (job)
警報規則的示例文件是:
groups:
- name: example
rules:
- alert: HighErrorRate
expr: job:request_latency_seconds:mean5m{job="myjob"} > 0.5
for: 10m
labels:
severity: page
annotations:
summary: High request latency
要在不啓動Prometheus服務器的情況下快速檢查規則文件在語法上是否正確,請安裝並運行Prometheus的promtool命令行工具:
go get github.com/prometheus/prometheus/cmd/promtool
promtool check rules /path/to/example.rules.yml
Scrape configuration
scrape_configs具體說明了Prometheus想要抓取的目標。
Prometheus通過訪問獲取端點來抓取數據。爲了抓取一個端點,普羅米修斯定義了一個稱爲目標的配置。這是執行抓取所需的信息,例如,需要應用的標籤、連接所需的任何身份驗證,或者定義抓取將如何發生的其他信息。目標組稱爲作業。在作業內部,每個目標都有一個名爲instance的標籤,該標籤唯一地標識目標對象。
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
在缺省配置中有一個名爲prometheus的作業,它裏面包含一個static_config配置項,列出了這個作業將要抓取的目標。這個要抓取的目標列表可以手動第靜態配置或通過服務發現來設置。
這裏Prometheus將監控自己,它將抓取localhost的9090端口的服務的監控指標。Prometheus默認從 /metrics 端口抓取數據,因此它將訪問的地址是
http://localhost:9090/metrics。