簡體   English   中英

如何在 Prometheus PromQL 中划分 2 個指標

[英]How to divide 2 metrics in Prometheus PromQL

我正在使用 Prometheus 在 Graphana 中構建儀表板。 我有 2 個指標(對服務的總調用次數和總超時錯誤)

1 是對服務 PromQL 的總調用次數

(increase(Fetching_RESPONSE_TIME_seconds_count{instance="${server}:8080"}[1h]) 

其他是總超時 PromQL

(increase(dp_errors_total{code=~"12345",instance="${server}:8080"}[1h]))

我想在我的儀表板中再增加一列,它顯示超時百分比,這將是(總超時*100/總調用服務)。

當我做這個 PromQL

(increase(dp_errors_total{code=~"12345",instance="${server}:8080"}[1h])*100
/
(increase(Fetching_RESPONSE_TIME_seconds_count{instance="${server}:8080"}[1h])

它沒有向我的儀表板顯示任何內容。

如何在儀表板中再添加一列來顯示超時百分比?

當您嘗試執行算術表達式時,Prometheus 將嘗試匹配左右兩側的時間序列。 它通過他們擁有的標簽來做到這一點。 雙方必須具有相同的標簽(名稱和值)。 我不知道您的時間序列具有的所有標簽,但我可以猜測例如code標簽僅存dp_errors_total而不是第二個。 我通常會首先聚合兩個操作數(根據需要),例如:

sum by (server) ( ... dp_errors_total query ) 
/
sum by (server) ( ... Fetching_RESPONSE_TIME_seconds_count query ...)

或者如果$server只有一台服務器,則刪除by (server)部分。

默認情況下,Prometheus 對/運算符左側和右側具有相同標簽集的時間序列對執行除法。 在我們的案例中/左側的時間序列包含codeinstance標簽,而/右側的時間序列僅包含instance label。 Prometheus 找不到匹配的時間序列對,因此根據這些規則它什么也不返回。 可以使用on()group_left()修飾符更改此行為:

  • on()修飾符用於限制標簽集,在搜索匹配的時間序列對時會考慮這些標簽集
  • group_left()修飾符用於允許將/運算符左側的多個時間序列匹配到右側的單個時間序列。 有關更多詳細信息,請參閱這些文檔

因此,生成的查詢應如下所示:

100 * increase(dp_errors_total{code=~"12345",instance="${server}:8080"}[1h])
  / on(instance) group_left()
increase(Fetching_RESPONSE_TIME_seconds_count{instance="${server}:8080"}[1h])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM