原文:https://prometheus.io/docs/prometheus/latest/getting_started/
本指南是一種“ Hello World”風格的教程,它通過簡單的設置示例展示瞭如何安裝、配置和使用Prometheus。 您將在本地下載並運行Prometheus,對其進行配置以抓取自身和示例應用程序,然後使用查詢、規則和繪圖來利用收集的時間序列數據。
下載並運行Prometheus
爲您的平臺下載最新版本的Prometheus,然後解壓縮並運行它:
tar xvfz prometheus-*.tar.gz
cd prometheus-*
在啓動Prometheus之前,讓我們對其進行配置。
配置Prometheus監控自身
Prometheus通過HTTP endpoints從被監控的目標上抓取指標來收集。 由於Prometheus還以相同的方式公開有關其自身的數據,因此它也可以抓取並監控其自身的健康狀況。
雖然僅收集有關自身數據的Prometheus server在實踐中不是很有用,但它是一個很好的入門示例。 將以下基本Prometheus配置保存爲名爲prometheus.yml的文件:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
有關配置選項的完整說明,請參閱配置文檔。
啓動Prometheus
要使用新創建的配置文件啓動Prometheus,請切換到包含Prometheus二進制文件的目錄並運行:
# Start Prometheus.
# By default, Prometheus stores its database in ./data (flag --storage.tsdb.path).
./prometheus --config.file=prometheus.yml
Prometheus應該啓動了。 您還應該能夠在localhost:9090瀏覽到有關其自身的狀態頁。 花幾秒鐘的時間從其自己的HTTP指標終結點收集有關其自身的數據。
您還可以通過導航到其指標端點來驗證Prometheus是否正在提供有關其自身的指標:localhost:9090/metrics
使用表達式瀏覽器
讓我們嘗試查看Prometheus收集的有關自身的一些數據。 要使用Prometheus的內置表達式瀏覽器,請導航至http://localhost:9090/graph,然後在“Graph”選項卡中選擇“Console”視圖。
正如您可以從localhost:9090/metrics收集的那樣,Prometheus導出的有關其自身的一個指標稱爲prometheus_target_interval_length_seconds(目標抓取之間的實際時間)。 繼續並將其輸入到表達式控制檯中:
prometheus_target_interval_length_seconds
這應該返回多個不同的時間序列(以及每個時間序列的最新記錄值),所有時間序列的指標名稱均爲prometheus_target_interval_length_seconds,但具有不同的標籤。 這些標籤指定不同的延遲百分比和目標組間隔。
如果我們只對第99個百分位延遲感興趣,則可以使用以下查詢來檢索該信息:
prometheus_target_interval_length_seconds{quantile="0.99"}
要計算返回的時間序列數,您可以寫:
count(prometheus_target_interval_length_seconds)
有關表達語言的更多信息,請參見表達式語言文檔。
使用繪圖界面
要繪製圖形表達式,請導航至http://localhost:9090/graph並使用“圖形”選項卡。
例如,輸入以下表達式以繪製在自抓取的Prometheus中創建的塊的每秒速率:
rate(prometheus_tsdb_head_chunks_created_total[1m])
試試圖形範圍參數和其他設置。
啓動示例目標
讓我們變得更加有趣,併爲Prometheus抓取一些示例目標。
Go客戶端庫包含一個示例,該示例導出具有不同延遲分佈的三個服務的虛構RPC延遲。
確保您已安裝Go編譯器,並設置了可正常運行的Go構建環境(具有正確的GOPATH)。
下載Prometheus的Go客戶端庫,並運行以下三個示例過程:
#go get如果緩慢的話,可以設置代理: export GO111MODULE=on && export GOPROXY=https://goproxy.cn
# Fetch the client library code and compile example.
git clone https://github.com/prometheus/client_golang.git
cd client_golang/examples/random
go get -d
go build
# Start 3 example targets in separate terminals:
./random -listen-address=:8080
./random -listen-address=:8081
./random -listen-address=:8082
現在你的示例目標監聽在http://localhost:8080/metrics, http://localhost:8081/metrics, and http://localhost:8082/metrics.
配置Prometheus監視示例目標
現在,我們將配置Prometheus來抓取這些新目標。 讓我們將所有三個端點分組爲一個稱爲example-random的job。 但是,假設前兩個端點是生產目標,而第三個端點代表金絲雀實例。 爲了在Prometheus中對此建模,我們可以將多個端點組添加到單個job中,併爲每個目標組添加額外的標籤。 在此示例中,我們將group="production"標籤添加到第一個目標組,同時將group="canary"添加到第二個目標。
爲此,請將以下作業定義添加到prometheus.yml中的scrape_configs部分,然後重新啓動Prometheus實例:
scrape_configs:
- job_name: 'example-random'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:8080', 'localhost:8081']
labels:
group: 'production'
- targets: ['localhost:8082']
labels:
group: 'canary'
轉到表達式瀏覽器,並驗證Prometheus現在是否具有有關這些示例端點公開的時間序列的信息,例如rpc_durations_seconds指標。
配置規則以將抓取的數據彙總到新的時間序列中
儘管在我們的示例中沒有問題,但是在臨時計算時,聚集了數千個時間序列的查詢可能會變慢。 爲了提高效率,Prometheus允許您通過配置的記錄規則將表達式預記錄到全新的持久時間序列中。 假設我們感興趣的是記錄在5分鐘的窗口中測得的所有實例(但保留job和service維度)平均的示例RPC每秒速率(rpc_durations_seconds_count)。 我們可以這樣寫:
avg(rate(rpc_durations_seconds_count[5m])) by (job, service)
嘗試繪製此表達式的圖形。
要將由該表達式產生的時間序列記錄到名爲job_service:rpc_durations_seconds_count:avg_rate5m的新指標中,請使用以下記錄規則創建文件並將其另存爲prometheus.rules.yml:
groups:
- name: example
rules:
- record: job_service:rpc_durations_seconds_count:avg_rate5m
expr: avg(rate(rpc_durations_seconds_count[5m])) by (job, service)
要使Prometheus選擇此新規則,請在prometheus.yml中添加rule_files語句。 現在,配置應如下所示:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # Evaluate rules every 15 seconds.
# Attach these extra labels to all timeseries collected by this Prometheus instance.
external_labels:
monitor: 'codelab-monitor'
rule_files:
- 'prometheus.rules.yml' #指定規則文件
scrape_configs:
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
- job_name: 'example-random'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:8080', 'localhost:8081']
labels:
group: 'production'
- targets: ['localhost:8082']
labels:
group: 'canary'
通過新的配置重新啓動Prometheus,並通過表達式瀏覽器對其進行查詢或對其進行製圖,以驗證指標名稱爲job_service:rpc_durations_seconds_count:avg_rate5m的新時間序列現在可用。