【環球新要聞】Prometheus監控之檢查工具Promtool TSDB

2022-12-26 14:09:16 來源:51CTO博客


【資料圖】

一、概述

Promtool在TSDB方面一個有6個子命令,分別用來進行寫性能測試、TSDB分析、列出TSDB數據塊、dump、從OpenMetric導入數據塊、為新的記錄規則創建數據塊

二、TSDB

1、寫性能測試

Promtool 可以對 Prometheus 進行寫的性能測試,命令參數如下:./promtool tsdb bench write --helpusage: promtool tsdb bench write [] []Run a write performance benchmark.Flags:  -h, --help                 Show context-sensitive help (also try --help-long and --help-man).      --version              Show application version.      --enable-feature= ...  Comma separated feature names to enable (only PromQL related). See                             https://prometheus.io/docs/prometheus/latest/feature_flags/ for the options and more details.      --out="benchout"       Set the output path.      --metrics=10000        Number of metrics to read.      --scrapes=3000         Number of scrapes to simulate.Args:  []  Input file with samples data, default is (../../tsdb/testdata/20kseries.json).  首先從官網下載測試所需要的數據 20kseries.json 文件,這個文件在源碼倉庫的 /tsdb/testdata/ 下,可以通過這個命令來下載wget https://raw.githubusercontent.com/prometheus/prometheus/main/tsdb/testdata/20kseries.json下載好以后,指定 metrics 和 scrapes 兩個參數進行測試./promtool tsdb bench write --metrics=10000 --scrapes=3000 ./20kseries.jsonlevel=info ts=2022-07-27T13:21:25.546626055Z caller=head.go:493 msg="Replaying on-disk memory mappable chunks if any"level=info ts=2022-07-27T13:21:25.546734166Z caller=head.go:536 msg="On-disk memory mappable chunks replay completed" duration=11.815μslevel=info ts=2022-07-27T13:21:25.546766084Z caller=head.go:542 msg="Replaying WAL, this may take a while"level=info ts=2022-07-27T13:21:25.547101874Z caller=head.go:613 msg="WAL segment loaded" segment=0 maxSegment=0level=info ts=2022-07-27T13:21:25.547131383Z caller=head.go:619 msg="WAL replay completed" checkpoint_replay_duration=38.26μs wal_replay_duration=315.491μs total_replay_duration=409.177μslevel=info ts=2022-07-27T13:21:25.549132675Z caller=db.go:1467 msg="Compactions disabled">> start stage=readData>> completed stage=readData duration=145.973395ms>> start stage=ingestScrapesingestion completed>> completed stage=ingestScrapes duration=3.628682202s > total samples: 30000000 > samples/sec: 8.267435217071414e+06>> start stage=stopStorage>> completed stage=stopStorage duration=1.522008202s這個測試會在當前目錄生成一個 benchout 的文件夾,里邊是測試生成的文件。這個路徑也可以通過參數來進行修改,生成到其他地方或者其他名稱。簡單來看,這次讀數據消耗了 145ms ,存儲數據消耗了 1.5s 。

2、TSDB分析

Promtool 可以對 Prometheus 的數據塊進行分析,分析 churn 、label 對的基數和壓縮效率,命令參數如下:./promtool tsdb analyze --helpusage: promtool tsdb analyze [] [] []Analyze churn, label pair cardinality and compaction efficiency.Flags:  -h, --help                 Show context-sensitive help (also try --help-long and --help-man).      --version              Show application version.      --enable-feature= ...  Comma separated feature names to enable (only PromQL related). See                             https://prometheus.io/docs/prometheus/latest/feature_flags/ for the options and more details.      --limit=20             How many items to show in each list.      --extended             Run extended analysis.Args:  []   Database path (default is data/).  []  Block to analyze (default is the last block).在分析的時候可以指定每個列表最多顯示多少個項,必須要指定數據的 data 目錄,block id 可以指定也可以不指定,不指定的話會分析最新的那個 block 。執行命令以后可以看到如下輸出。./promtool tsdb analyze ./prometheus/data/Block ID: 01G8ZXKAHRGF7BV0FTQYZ18FH5Duration: 59m55.713sSeries: 606677Label names: 26Postings (unique label pairs): 13190Postings entries (total label pairs): 7819776......

3、列出TSDB數據塊

使用 Promtool 還可以列出當前 Prometheus 的所有數據塊。命令的參數如下:./promtool tsdb list --helpusage: promtool tsdb list [] []List tsdb blocks.Flags:  -h, --help                 Show context-sensitive help (also try --help-long and --help-man).      --version              Show application version.      --enable-feature= ...  Comma separated feature names to enable (only PromQL related). See                             https://prometheus.io/docs/prometheus/latest/feature_flags/ for the options and more details.  -r, --human-readable       Print human readable values.Args:  []  Database path (default is data/).執行這個命令我們來看一下效果,建議加上-r參數./promtool tsdb list -r ./prometheus-pushgateway/data/BLOCK ULID                  MIN TIME                       MAX TIME                       DURATION     NUM SAMPLES  NUM CHUNKS   NUM SERIES   SIZE01G8XB6J42S6FTDMAPYV30CTR3  2022-07-26 12:00:03 +0000 UTC  2022-07-26 13:00:00 +0000 UTC  59m56.018s   78421200     435860       435412       92MiB217KiB450B01G8XEMDR374M816NZ2NMNS5AQ  2022-07-26 13:00:03 +0000 UTC  2022-07-26 14:00:00 +0000 UTC  59m56.018s   78421200     435860       435412       92MiB217KiB825B通過這個命令我們可以看到 Block ID、數據存儲的開始時間和結束時間,一共存儲了多長時間段 數據,Sample 的數量,Chunk 的數量,Serie 的數量、以及 Block 的大小。

4、dump

Promtool 還行可以進行 dump,只不過是從 TSDB 中 dump Sample 數據出來。./promtool tsdb dump --helpusage: promtool tsdb dump [] []Dump samples from a TSDB.Flags:  -h, --help                 Show context-sensitive help (also try --help-long and --help-man).      --version              Show application version.      --enable-feature= ...  Comma separated feature names to enable (only PromQL related). See                             https://prometheus.io/docs/prometheus/latest/feature_flags/ for the options and more details.      --min-time=-9223372036854775808                             Minimum timestamp to dump.      --max-time=9223372036854775807                             Maximum timestamp to dump.Args:  []  Database path (default is data/).對于這個命令一定要指定數據目錄 data 的路徑,另外最大最小時間也指定一下,否則會 dump 數據庫中所有的 Sample 數據。./promtool tsdb dump --min-time=1658927217000 --max-time=1658930818000 ../prometheus-pushgateway/data/這個命令 dump 出來的數據好像只會在屏幕輸出,并不會輸出到文件,dump 的時候記得人為導入到某個文件。

5、從OpenMetric導入數據塊

Promtool工具還可以從OpenMetrics輸入導入sample數據并生成TSDB數據塊。 這個命令的使用場景是在不同的監控系統或者時序數據庫之間遷移數據使用的,先將對應的數據轉換成OpenMetric格式,然后將OpenMetric格式的數據導入到Prometheus的TSDB數據庫中。./promtool tsdb create-blocks-from openmetrics --helpusage: promtool tsdb create-blocks-from openmetrics  []Import samples from OpenMetrics input and produce TSDB blocks. Please refer to the storage docs for more details.Flags:  -h, --help                 Show context-sensitive help (also try --help-long and --help-man).      --version              Show application version.      --enable-feature= ...  Comma separated feature names to enable (only PromQL related). See                             https://prometheus.io/docs/prometheus/latest/feature_flags/ for the options and more details.  -r, --human-readable       Print human readable values.  -q, --quiet                Do not print created blocks.Args:            OpenMetrics file to read samples from.  []  Output directory for generated blocks.

6、為新的記錄規則創建數據塊

當創建新的記錄規則以后,這個新的記錄規則是沒有歷史數據的。記錄規則所產生的數據只在創建時開始存儲到數據庫中,Promtool可以創建記錄規則的歷史數據。./promtool tsdb create-blocks-from rules --helpusage: promtool tsdb create-blocks-from rules --start=START [] ...Create blocks of data for new recording rules.Flags:  -h, --help                 Show context-sensitive help (also try --help-long and --help-man).      --version              Show application version.      --enable-feature= ...  Comma separated feature names to enable (only PromQL related). See                             https://prometheus.io/docs/prometheus/latest/feature_flags/ for the options and more details.  -r, --human-readable       Print human readable values.  -q, --quiet                Do not print created blocks.      --url=http://localhost:9090                             The URL for the Prometheus API with the data where the rule will be backfilled from.      --start=START          The time to start backfilling the new rule from. Must be a RFC3339 formatted date                             or Unix timestamp. Required.      --end=END              If an end time is provided, all recording rules in the rule files provided will                             be backfilled to the end time. Default will backfill up to 3 hours ago. Must be a                             RFC3339 formatted date or Unix timestamp.      --output-dir="data/"   Output directory for generated blocks.      --eval-interval=60s    How frequently to evaluate rules when backfilling if a value is not set in the                             recording rule files.Args:    A list of one or more files containing recording rules to be backfilled. All recording rules                listed in the files will be backfilled. Alerting rules are not evaluated.命令中提供的記錄規則文件應該是一個普通的 Prometheus 規則文件,然后指定要生成的歷史數據的時間范圍,./promtool tsdb create-blocks-from rules \    --start 1617079873 \    --end 1617097873 \    --url http://mypromserver.com:9090 \    rules.yaml rules2.yaml命令promtool tsdb create-blocks-from rules的輸出結果為一個目錄,該目錄中包含記錄規則文件中所有規則的歷史規則數據塊。默認情況下,輸出目錄為data/。為了利用這些新的塊數據,必須將這些塊移動到正在運行的 Prometheus 實例數據目錄下,然后在 Prometheus 實例啟動的時候將參數--storage.tsdb.allow-overlapping-blocks設為啟用。之后 Prometheus 會在在下一次壓縮運行時,將新塊與現有塊合并。這樣記錄規則文件生成的歷史數據就可以在 Prometheus 中進行查詢了。創建記錄規則的歷史數據有一些限制:如果多次執行生成歷史數據 ,且開始時間和結束時間重疊,則每次運行記錄規則生成歷史數據時都會創建包含相同數據的塊。記錄規則文件中的所有規則都將被評估。如果在記錄規則文件中設置了時間間隔,該時間間隔優先于 tsdb create-blocks-from rules 命令中的--eval-interval 參數。如果告警規則在記錄規則文件中,那么告警規則會被忽略。同一組中的規則無法看到之前規則的結果。這意味著不支持引用正在生成歷史數據的其他規則的規則。一種解決方法是多次生成歷史數據并首先創建依賴數據(并將依賴數據移動到 Prometheus 服務器數據目錄,以便從 Prometheus API 訪問它)。

標簽: 歷史數據 性能測試 開始時間

上一篇:焦點信息:Webpack開發環境要如何搭建?
下一篇:全球熱點評!圖計算引擎分析 ——Gemini