[英]Prometheus: how to get label_value() in query
所以我有 2 個指標,我需要從第一個指標中獲取 label 值,然后查詢第二個指標,其中 {param="label_values_from_1st_metric"}。 例如:
metric_1{instance="abc"}
- 返回 3 個時間序列: metric_1{name="my-service", env="production", host="example-0.org"}
metric_1{name="my-service", env="production", host="example-1.org"}
metric_1{name="my-service", env="production", host="example-2.org"}
metric_2{domain="exmple-0,1,2.org"}
所以問題是,我如何將 label_values 傳遞給第二個查詢? 據我了解,我只能將 label_values() 用於 grafana 面板中的變量,因此我無法編寫一個可以為我執行此操作的查詢。
您可以通過以下方式嘗試:
metric_2 * on(instance) group_left(host) metric_1{instance="abc"}
on(instance) =>這是在 label 實例上加入的方法。
group_left(host) metric_1{instance="abc"} =>表示,將 label 主機從 metric_1 保留在結果中。
好吧,終於,我明白了。 因此,正如 Pulak Kanti 建議的那樣,我使用了 group_left。 但是它返回錯誤:不允許多對多數學運算。 所以我做了什么:
metric_1和metric_2之間的標簽不匹配,但是修復它很簡單,我們需要添加 label_replace() func label_replace(metric_2,"host","$1","domain", "(.+)")
。 此表達式將使用從 label域復制的值添加到 metric_2 label主機。 從我們的角度來看,我們剛剛將 label域重命名為 label主機。 這是個好消息,因為現在 metric_1 和 metric_2 有一個共同的 label - host 。 所以這一步的查詢看起來像這樣:
label_replace(metric_2,"host","$1","domain", "(.+)") + on (host) group_left(domain) metric_1{instance="abc"}
現在我們的查詢至少沒有返回任何錯誤,但是,它返回了意外的結果——這是因為它將來自 metric_2 的值與來自 metric_1 的值相乘。 因為我們只需要來自 metric_2 的值,我們只需要 null metric_1,所以它最終是: label_replace(metric_2,"host","$1","domain", "(.+)") + on (host) group_left(domain) 0*metric_1{instance="abc"}
我確實了解此查詢無效,但它對我有用。 如果有人能提供幫助並使這個查詢更有效率,我將不勝感激。 順便說一句:這里 group_left() 括號內的內容並不重要,因為它只影響我們顯示的 label ,對我來說這並不重要,所以label_replace(metric_2,"host","$1","domain", "(.+)") + on (host) group_left() 0*metric_1{instance="abc"}
也有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.