Promql filter out time series with value in label same like in another metric dynamicaly

265 views Asked by At

Assuming I have two different metrics with different labels names but the same a group of values :

metric1{label1="some_values_the_same_as_in_metric2"}  val examples: val1 val2 val3
metric2{label2="some_values_the_same_as_in_metric1"}  val examples: val2 val3 

Now I want to query metric1 with label1 but filter out all metrics with the same value as in metric2 label2

I know I can metric1{label1!=~"val2|val3"}

but what if i have 300 values in metric1 and 200 in metric2 and these can change over time? how to filter it out dynamically?

tried many things like this: metric_name1 unless metric_name2 on(common_label) group_left but without success

1

There are 1 answers

1
markalex On BEST ANSWER

Your attempt is in correct direction. It's just that on() clause needs label common for both metrics, but based on your example, they are not.

This is not a problem though: label_replace can help us here.

metric_name1
 unless on(label1)
 label_replace(metric_name2, "label1", "$1", "label2", "(.*)")

Here, I copy label label2 into label1, and then use it in unless to exclude all the metrics with matching label1.

You don't need group_left since unless with on clause doesn't change left operand's labels set (unlike and with on clause).