天天看點

使用 PushGateway 進行資料上報采集

prometheus 是一套開源的系統監控、報警、時間序列資料庫的組合,最初有 soundcloud 開發的,後來随着越來越多公司使用,于是便獨立成開源項目。prometheus 基本原理是通過 http 協定周期性抓取被監控元件的狀态,而輸出這些被監控的元件的 http 接口為 exporter。pushgateway 作為 prometheus 生态中的一個重要一員,它允許任何用戶端向其 push 符合規範的自定義監控名額,在結合 prometheus 統一收集監控。

pushgateway 使用場景:

prometheus 采用定時 pull 模式,可能由于子網絡或者防火牆的原因,不能直接拉取各個 target 的名額資料,此時可以采用各個 target 往 pushgateway 上 push 資料,然後 prometheus 去 pushgateway 上定時 pull。

其次在監控各個業務資料時,需要将各個不同的業務資料進行統一彙總,此時也可以采用 pushgateway 來統一收集,然後 prometheus 來統一拉取。

本次示範環境,我是在虛拟機上安裝 linux 系統來執行操作,以下是安裝的軟體及版本:

oracle virtualbox: 5.1.20 r114628 (qt5.6.2)

system: centos linux release 7.3.1611 (core)

docker: 18.06.1-ce

prometheus: v2.11.1

pushgateway: 1.0.0

注意:這裡為了快速友善啟動 prometheus、pushgateway 服務,我使用 docker 方式啟動,是以本機需要安裝好 docker 環境,這裡忽略 docker 的安裝過程。其中 prometheus 安裝配置,可以參照之前文章 prometheus 監控報警系統 alertmanager 之郵件告警,這裡着重介紹一下如何啟動并配置 pushgateway 并配置資料上報到 prometheus。

pushgateway 安裝很簡單,可以使用二進制包解壓安裝服務,也可以使用 docker 啟動服務。

二進制包安裝方式,直接從 官方 github 下載下傳最新二進制安裝包,解壓即可。

使用指令 <code>./pushgateway</code> 指令即可啟動服務,此時浏覽器通路 <code>http://&lt;ip&gt;:9091</code> 即可通路 ui 頁面,隻不過預設 metrics 上沒有任何資料展示,那是因為我們還沒有往 pushgateway 上推送任何資料。

使用 PushGateway 進行資料上報采集

不過,pushgateway 服務本身是帶了一些 metrics 的,可以通過通路 <code>http://&lt;ip&gt;:9091/metrics</code> 位址來擷取,可以看到裡邊包含了 go、process 等相關的一些監控名額。

使用 PushGateway 進行資料上報采集

使用 docker 方式安裝啟動就更簡單了,直接擷取最新版官方鏡像 <code>prom/pushgateway:latest</code> 啟動指令如下:

啟動完畢後,同上方法驗證是否啟動成功。這裡為了友善示範,我采用 docker 方式啟動 pushgateway。

ok,現在 pushgateway 服務已經啟動完畢,但是還沒有跟 prometheus 關聯起來,我們需要的是通過 pushgateway 來上傳自定義監控資料,然後通過 prometheus 采集這些資料來進行監控。那麼就需要将 pushgateway 添加到 prometheus 目标任務中去,增加 <code>prometheus.yml</code> 配置如下:

說明一下,這裡采用 <code>static_configs</code> 靜态配置方式,因為目前就一個 pushgateway,如果有多個可以考慮其他服務發現方式,來友善動态加載,具體可以參考 這裡。配置完畢後,重新開機 prometheus 服務,此時可以通過 prometheus ui 頁面的 targets 下檢視是否配置成功。

使用 PushGateway 進行資料上報采集

接下來,我們要 push 資料到 pushgateway 中,可以通過其提供的 api 标準接口來添加,預設 url 位址為:<code>http://&lt;ip&gt;:9091/metrics/job/&lt;jobname&gt;{/&lt;label_name&gt;/&lt;label_value&gt;}</code>,其中 <code>&lt;jobname&gt;</code> 是必填項,為 job 标簽值,後邊可以跟任意數量的标簽對,一般我們會添加一個 <code>instance/&lt;instance_name&gt;</code> 執行個體名稱标簽,來友善區分各個名額。

接下來,可以 push 一個簡單的名額資料到 pushgateway 中測試一下。

執行完畢,重新整理一下 pushgateway ui 頁面,此時就能看到剛添加的 <code>test_metric</code> 名額資料了。

使用 PushGateway 進行資料上報采集

不過我們會發現,除了 <code>test_metric</code> 外,同時還新增了 <code>push_time_seconds</code> 和 <code>push_failure_time_seconds</code> 兩個名額,這兩個是 pushgateway 系統自動生成的相關名額。此時,我們在 prometheus ui 頁面上 graph 頁面可以查詢的到該名額了。

使用 PushGateway 進行資料上報采集

這裡要着重提一下的是,上圖中 <code>test_metric</code> 我們查詢出來的結果為 <code>test_metric{exported_job="test_job",instance="pushgateway",job="pushgateway"}</code> ,眼尖的會發現這裡頭好像不太對勁,剛剛送出的名額所屬 job 名稱為 <code>test_job</code> ,為啥顯示的為 <code>exported_job="test_job"</code> ,而 job 顯示為 <code>job="pushgateway"</code> ,這顯然不太正确,那這是因為啥?其實是因為 prometheus 配置中的一個參數 <code>honor_labels</code> (預設為 <code>false</code>)決定的,我們不妨再 push 一個資料,來示範下添加 <code>honor_labels: true</code> 參數前後的變化。

這次,我們 push 一個複雜一些的,一次寫入多個名額,而且每個名額添加 <code>type</code> 及 <code>help</code> 說明。

添加完畢,再重新整理一下 pushgateway ui 頁面,可以看到添加的資料了。

使用 PushGateway 進行資料上報采集

從上圖可以看出,<code>/metrics/job/test_job</code> 和 <code>metrics/job/test_job/instance/test_instance</code> 雖然它們都屬于 <code>test_job</code>,但是它們屬于兩個名額值,因為 instance 對二者做了區分。此時我們通路 prometheus ui 頁面上 graph 頁面查詢該名額。

使用 PushGateway 進行資料上報采集

依舊有問題,那麼修改一下 <code>prometheus.yaml</code>,增加 <code>honor_labels: true</code> 參數配置如下:

重新開機 prometheus,稍等一會,等到 prometheus 采集到資料後,我們再通路 prometheus ui 頁面上 graph 頁面查詢該名額。

使用 PushGateway 進行資料上報采集

此時,可以看到能夠正确比對到 push 的名額值對應到 job 和 instance 上了。這裡說明一下 <code>honor_labels</code> 的作用:因為 prometheus 配置 pushgateway 的時候,也會指定 job 和 instance,但是它隻表示 pushgateway 執行個體本身,不能真正表達收集資料的含義。是以配置 pushgateway 需要添加 <code>honor_labels:true</code> 參數,避免收集資料本身的 job 和 instance 被覆寫。詳細可參考 這裡 官網文檔對該參數的說明。

上邊我們 push 名額資料是通過指令行追加方式,少量資料還湊合,如果需要 push 的資料比較大時,就不太友善了,這裡我們也可以通過将名額資料寫入到檔案,然後将檔案内容送出,也可以正常添加到 pushgateway。建立一個名額資料檔案 <code>pgdata.txt</code> 如下:

然後執行如下指令,将資料 push 上去。

執行完畢,在 pushgateway ui 頁面同樣能夠查詢的到。

使用 PushGateway 進行資料上報采集

最後,如果要删除某一個名額,同樣可以通過 api 方式觸發删除。例如删除 <code>job="test_job"</code> 組下的所有名額值,可以執行如下指令:

注意:删除 <code>job="test_job"</code> 組下的所有名額值,不包括 <code>{job="test_job", instance="test_instance"}</code> 中的名額值,雖然它們的 job 名稱都為 <code>test_job</code>。如果想删除該名額值,那麼需要執行如下指令:

同樣,我們也可以在 pushgateway ui 頁面指定名額記錄後邊,點選 <code>delete group</code> 按鈕來删除,這裡就不在示範了。

通過 client sdk 推送 metric 資訊到 pushgateway,官方示例中支援 python、java、go 等不同語言類型 client,這裡我以 java 語言為例,來示範下如何 push 資料到 pushgateway 中。

首先,<code>pom.xml</code> 中添加 <code>simpleclient_pushgateway</code> 依賴包。

編寫一個簡單 demo 如下:

執行完畢後,通路一下 pushgateway ui 頁面,能夠查詢的到該名額。

使用 PushGateway 進行資料上報采集

上邊列子比較簡單,咱們在來一個稍微負載一些的,指定多個 label 标簽組的名額。

同樣,在 pushgateway ui 頁面,也能夠查詢的到該名額。

使用 PushGateway 進行資料上報采集

名額值隻能是數字類型,非數字類型報錯。 $ echo "test_metric 12.34.56ff" | curl --data-binary @- http://172.30.12.167:9091/metrics/job/test_job_1 text format parsing error in line 1: expected float as value, got "12.34.56ff"

名額值支援最大長度為 16 位,超過16 位後預設置為 0 $ echo "test_metric 1234567898765432123456789" | curl --data-binary @- http://172.30.12.167:9091/metrics/job/test_job_2 # 實際擷取值 test_metric{job="test_job_2"} 1234567898765432200000000

pushgateway 資料持久化操作 預設 pushgateway 不做資料持久化操作,當 pushgateway 重新開機或者異常挂掉,導緻資料的丢失,我們可以通過啟動時添加 <code>-persistence.file</code> 和 <code>-persistence.interval</code> 參數來持久化資料。<code>-persistence.file</code> 表示本地持久化的檔案,将 push 的名額資料持久化儲存到指定檔案,<code>-persistence.interval</code> 表示本地持久化的名額資料保留時間,若設定為 5m,則表示 5 分鐘後将删除存儲的名額資料。 $ docker run -d -p 9091:9091 prom/pushgateway "-persistence.file=pg_file –persistence.interval=5m"

pushgateway 推送及 prometheus 拉取時間設定 prometheus 每次從 pushgateway 拉取的資料,并不是拉取周期内使用者推送上來的所有資料,而是最後一次 push 到 pushgateway 上的資料,是以推薦設定推送時間小于或等于 prometheus 拉取的時間,這樣保證每次拉取的資料是最新 push 上來的。

參考資料

github pushgateway

github pushgateway client-java

prometheus docs