Prometheus-09 promtool工具探索


promtool工具探索

Prometheus提供了一個非常有用的支持命令行工具promtool。這個小型的Golang二進制文件可用於快速執行幾個故障排除操作,並且包含了許多有用的子命令。

1.檢查

屬於這個類別的子命令為用戶提供了檢查和驗證Prometheus服務器的幾個配置方面和度量標准遵守從性的能力。

1.1檢查配置

promtool提供了幾種類型的檢查。其中最有價值的是檢查Prometheus服務器的著配置文件。

檢查配置時,0表示成功,1表示失敗。

案例如下:

$ promtool check config /etc/prometheus/prometheus.yml
Checking /etc/prometheus/prometheus.yml
	SUCCESS: 1 rule files found

Checking /etc/prometheus/first_rules.yml
	SUCCESS: 1 rule rules found

1.2規則檢查

check rules 分析並確定規則配置文件中的錯誤配置。它允許直接真對特定的規則文件,這允許您測試在主Prometheus配置中尚未引用的文件。這種能力對於規則文件的開發周期和在使用配置管理時驗證上述文件中的自動更改非常方便。

案例如下:

$ promtool check rules /rules/prometheus/first_rules.yml
Checking /etc/prometheus/first_rules.yml
	SUCCESS: 1 rule rules found

1.3度量值檢查

check metrics 子命令驗證傳遞給他的度量在一致性和正確性方面是否遵循Prometheus准則。

$ curl -s http://prometheus:9090/metrics | promtool check metrics
prometheus_tsdb_storage_blocks_bytes_total non-counter should not have "_total" suffix

可以看到,prometheus_tsdb_storage_blocks_bytes_total度量似乎有問題。讓我們看看這個特殊的度量來排除錯誤:

$ curl -s http://prometheus:9090/metrics | grep prometheus_tsdb_storage_blocks_bytes_total
# HELP prometheus_tsdb_storage_blocks_bytes_total The number of bytes that are currently used for jocal storage by all blocks.
# TYPE prometheus_tsdb_storage_blocks_bytes_total guage
prometheus_tsdb_storage_blocks_bytes_total 0

2. 查詢

屬於這個類別的子命令允許直接從命令行執行PromQL表達式。這些查詢依賴於Prometheus公共HTTP API。下面的主題將演示如何使用它們。

2.1查詢實例

query instant 子命令允許根據當前時間通過命令行直接查詢Prometheus服務器。要使其工作,必須提供一個Prometheus服務器URL作為參數,以及要執行的查詢,就像這樣。

$ promtool query instant 'http://prometheus:9090' 'up == 1' 

'''
up{group="Retail", instance="http://192.168.1.102:80", job="blackbox-http"} => 1 @[1637019176.876]
up{group="Retail", instance="https://baidu.com", job="blackbox-http"} => 1 @[1637019176.876]
up{instance="192.168.1.121:8080", job="docker"} => 1 @[1637019176.876]
up{instance="192.168.1.121:9090", job="prometheus"} => 1 @[1637019176.876]
up{instance="192.168.1.121:9100", job="node"} => 1 @[1637019176.876]
'''

2.2 查詢范圍

與前面的子命令類似,查詢范圍允許在指定的時間范圍內顯示結果。因此,我們必須提供開始和結束unix格式的時間戳,以及查詢和Prometheus服務器端點。

例如,我們將使用date命令來定義開始和結束時間戳,生成五分鍾前的unix格式時間戳和現在的另一個時間戳。我們還可以使用--step標志指定查詢的解析,在我們的示例中,它是一分鍾。最后,我們防止PromQL表達式來執行,最后得到一個類似下面的指令:

$ promtool query range --start=$(date -d '5minutes ago' +'%s') --end=$(date -d 'now' +'%s') --step=1m 'http://prometheus:9090'

2.3 query series

使用query series可以搜索與一組度量名稱和標簽匹配的所有時間序列。以下是使用方法:

$ promtool query series 'http://prometheus:9090' --match='up' --match='go_info{job="prometheus"}'

{__name__="go_info", instance="192.168.1.121:9090", job="prometheus", version="go1.17.3"}
{__name__="go_info", instance="zhang.com:9090", job="prometheus", version="go1.17.3"}
{__name__="up", group="Retail", instance="http://192.168.1.102:80", job="blackbox-http"}

2.4query labels

使用查詢標簽,您可以跨所有可用的指標搜索特定的標簽,並返回附加到它的所有可能的值;例如:

$ promtool query labels 'http://prometheus:9090' 'mountpoint'

/
/boot
/run
/run/user/0

3. Debug

屬於這個類別的子命令允許從運行的Prometheus服務器提取調試數據,以便對其進行分析。接下來我們將演示如何使用它們。

3.1 debug pprof

$ promtool debug pprof 'http://prometheus:9090'

collecting: http://192.168.1.121:9090/debug/pprof/profile?seconds=30
collecting: http://192.168.1.121:9090/debug/pprof/block
collecting: http://192.168.1.121:9090/debug/pprof/goroutine
collecting: http://192.168.1.121:9090/debug/pprof/heap
collecting: http://192.168.1.121:9090/debug/pprof/mutex
collecting: http://192.168.1.121:9090/debug/pprof/threadcreate
collecting: http://192.168.1.121:9090/debug/pprof/trace?seconds=30
Compiling debug information complete, all files written in "debug.tar.gz".

當我們提取前一個命令生成的存檔文件時,我們可以看到幾個文件:

$ tar xzvf debug.tar.gz
cpu.pb
tar: cpu.pb: implausibly old time stamp 1970-01-01 08:00:00
block.pb
tar: block.pb: implausibly old time stamp 1970-01-01 08:00:00
goroutine.pb
tar: goroutine.pb: implausibly old time stamp 1970-01-01 08:00:00
heap.pb
tar: heap.pb: implausibly old time stamp 1970-01-01 08:00:00
mutex.pb
tar: mutex.pb: implausibly old time stamp 1970-01-01 08:00:00
threadcreate.pb
tar: threadcreate.pb: implausibly old time stamp 1970-01-01 08:00:00
trace.pb
tar: trace.pb: implausibly old time stamp 1970-01-01 08:00:00

使用pprof,我們可以生成轉儲的鏡像,在下一個代碼片段中我們可以管擦到這一點。

$ pprof -svg heap.pb > /vagrant/cache/heap.svg

在主機上,在./cache/路徑(相對於存儲庫根)下的代碼存儲庫中,您現在應該有一個可伸縮的向量圖形文件heap。可由瀏覽器打開以供查看。下main的截圖顯示了你可能會看到什么時,看看由上述例子產生的文件:

3.2 debug metrics

此子命令下載提供的Prometheus示例在壓縮歸檔中公開的度量。調試指標並不常用,因為/metrics Prometheus端點對於任何能夠運行此命令的人都是可用的;它的存在是為了在需要時更容易地向外部援助(例如Prometheus的維護者)提供Prometheus實例的當前狀態。這個子命令可以使用如下:

$ promtool debug metrics 'http://prometheus:9090'
collecting: http://192.168.1.121:9090/metrics
Compiling debug information complete, all files written in "debug.tar.gz".

tar xzvf debug.tar.gz
metrics.txt

tail -n 5 metrics.txt
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 3786
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0

3.3 debug all

該選項將之前的調試子命令聚合為一條指令,如下例所示:

$ promtool debug all 'http://prometheus:9090'

collecting: http://192.168.1.121:9090/debug/pprof/mutex
collecting: http://192.168.1.121:9090/debug/pprof/threadcreate
collecting: http://192.168.1.121:9090/debug/pprof/profile?seconds=30
collecting: http://192.168.1.121:9090/debug/pprof/block
collecting: http://192.168.1.121:9090/debug/pprof/goroutine
collecting: http://192.168.1.121:9090/debug/pprof/heap
collecting: http://192.168.1.121:9090/debug/pprof/trace?seconds=30
collecting: http://192.168.1.121:9090/metrics
Compiling debug information complete, all files written in "debug.tar.gz".

4.Endpoints

檢查Prometheus是否啟動並運行通常非常簡單,因為它遵循了大多數雲本地應用程序用於服務健康狀況的慣例:一個端點檢查服務是否健康,另一個端點檢查服務是否准備好開始處理傳入的請求。對於哪些過去使用或曾經使用過Kubernetes的人來說,這些可能聽起來很熟悉;實際上,Kubernetes還使用這些約定來評估是否需要重新啟動容器(例如,如果應用程序思索並停止響應健康狀況探測),以及是否可以開始向容器發送流量。

在Prometheus中,有/-/healthy 和/-ready 端點。

您可以在測試環境中運行以下命令並檢查它們的輸出以及它們的HTTP狀態碼,從而親自嘗試這些端點:

$ curl -w "%{http_code}\n" http://localhost:9090/-/healthy
Prometheus is Healthy.
200

$ curl -w "%{http_code}/n" http://localhost:9090/-/ready
Prometheus is Ready.
200

此外,Prometheus公開了一個/debug/pprof/ endpoint,promtool debug pprof 命令使用了這個端點,如前一節所示。這個端點還公開了一個可導航的web UI,可以在其中查詢pprof調試信息,比如當前goroutines及其堆棧跟蹤、堆分配、內存分配等;

5.Logs

通過 --log.level選項來設置日志級別。

查看配置日志級別:

$ sudo systemctl cat prometheus.service

ExecStart=/usr/bin/prometheus \
        --log.level=debug
        --config.file=/etc/prometheus/prometheus.yml \
        --storage.tsdb.path=/var/lib/prometheus/data \
        --web.console.templates=/usr/share/prometheus/consoles \
        --web.console.libraries=/usr/share/prometheus/console_libraries \
        --log.level=debug
sudo service node_exporter stop
sudo journalctl -fu prometheus | grep dubug


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM