How to sum values of metrics via PromQL

30 views
Skip to first unread message

Владимир organ2

unread,
Jun 2, 2020, 11:52:42 AM6/2/20
to Prometheus Users
There is some resource for example git.company.com and it have 2 ip addresses: internal and external addresses.
I've got two metrics in prometheus:
probe_success{instance="192.168.0.2",job="blackbox_tcp_general"} 1
probe_success{instance="172.170.220.31",job="blackbox_tcp_general"} 1

When I try to do query in PromQL:
probe_success{instance="192.168.0.2",job="blackbox_tcp_general"} + probe_success{instance="172.170.220.31",job="blackbox_tcp_general"}

I expect that I'll get result 2. But I get result "no data".
How can I sum values of metrics?

Brian Candler

unread,
Jun 2, 2020, 12:13:19 PM6/2/20
to Prometheus Users
https://prometheus.io/docs/prometheus/latest/querying/operators/#one-to-one-vector-matches

When you do a binary operation between two instant vectors, be default they must have exactly the same set of labels.

As long as all the other labels are the same, you can do this:

probe_success{instance="192.168.0.2",job="blackbox_tcp_general"} + ignoring(instance) probe_success{instance="172.170.220.31",job="blackbox_tcp_general"}

Or you can rewrite your expression to use "sum" across multiple timeseries:

sum(probe_success{instance=~"192\.168\.0\.2|172\.170\.220\.31",job="blackbox_tcp_general})

Ideally you'd using "meaningful instance labels" so that you can associate these metrics naturally by a common label. e.g.

{instance="server1",address="192.168.0.2"}
{instance="server1",address="172.170.220.31"}

That would allow you to do

sum by (instance) (probe_success{job="blackbox_tcp_general"})

Владимир organ2

unread,
Jun 3, 2020, 7:04:19 AM6/3/20
to Prometheus Users
Thank you!

Now my query works and looks:
sum(probe_success{instance="git.company.com:443",job="blackbox_tcp_general"}) * 2 - sum(probe_success{instance="git.company.com:22",job="blackbox_tcp_general"})

вторник, 2 июня 2020 г., 19:13:19 UTC+3 пользователь Brian Candler написал:
Reply all
Reply to author
Forward
0 new messages