[英]Prometheus query to calculate percentage from two metrics with different set of labels
我們有一個服務,它接受 HTTP 請求並使用響應中所有匹配的用戶數據進行響應,並生成兩個應該在 grafana 上以圖形/圖表形式提供的指標。 這里提到的指標 -
應用程序在不同數據中心區域收到的請求總數
application_requests_total{data_center_region_id=1} //200 application_requests_total{data_center_region_id=2} //100
對應用程序的每個請求都將嘗試在響應指標中包含所有匹配的用戶數據
application_response_total{user="user1, data_center_region_id=1} //100 application_response_total{user="user1, data_center_region_id=2} //100 application_response_total{user="user2, data_center_region_id=1} //50 application_response_total{user="user2, data_center_region_id =2} //100
快速觀察指標
user
label 僅存在於響應指標application_response_total
data_center_region_id
是請求和響應指標中常見的 labelapplication_response_total
中。我需要找出用戶級別的響應與特定數據中心區域中對應用程序的總請求數的百分比。
例如:根據上述數據,預期結果將是
對於 data_center_region_id=1
對於 data_center_region_id=2
我嘗試了幾個基於Prometheus 矢量匹配文檔的查詢,但無法達到預期的結果。 幾個示例查詢如下; 我不確定,但我想我搞砸了ON
/ IGNORING
和GROUP_LEFT
/ GROUP_RIGHT
鍵
sum(rate(application_response_total{data_center_region_id=~"$region"}[5m])) by (user, data_center_region_id) / on(user) group_left(data_center_region_id) sum(rate(application_requests_total{data_center_region_id=~"$region"}[5m])) by (user, data_center_region_id)
也參考了這里的問題,但沒有什么對我有用。
請以超出預期的結果指導我;
此外,這是獲得所需圖表的唯一方法嗎?
請注意,如果應用程序要為無限數量的用戶提供服務,那么將user
放入度量標簽可能是個壞主意。 這可能導致例如高基數問題。
至於原始問題,您需要在on(...)
修飾符中放置左側和右側系列的公共標簽,以便 Prometheus 可以在/
運算符的兩側找到具有給定標簽的系列對。 以下查詢返回每個(用戶、區域)的 rps 共享:
rate(application_response_total[5m])
/ on(data_center_region_id) group_left()
rate(application_requests_total[5m])
group_left()
修飾符指示 Prometheus 左側可能包含具有相同data_center_region_id
label 值的多個系列。 在這種情況下,Prometheus 獨立地將每個此類系列除以右側具有匹配data_center_rrgion_id
label 值的系列。 結果系列將具有與左側系列相同的標簽集。 有關詳細信息,請參閱這些文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.