Unable to run multiple queries in single one

20 views
Skip to first unread message

Anuj Kumar

unread,
May 3, 2023, 10:19:09 AM5/3/23
to Prometheus Users
HI All,

I am using the below query but getting output for the two queries . I need to get the output for each query. Any help would be appreciated.

machine_cpu_cores or sum by(nodename) (irate(node_cpu_seconds_total{mode="idle"}[5m]) * on(instance) group_left(nodename) node_uname_info) or sum by(nodename) (irate(node_cpu_seconds_total{mode!="idle"}[5m]) * on(instance) group_left(nodename) node_uname_info)

Thanks,
Anuj Kumar

Brian Candler

unread,
May 3, 2023, 12:18:37 PM5/3/23
to Prometheus Users
I think you need to describe:
* what you actually see
* what you would like to see instead
* the results of each of the subexpressions (i.e. left and right of "or") in the PromQL browser

Then it should be clearer how to combine them to achieve the result you want.


The most important thing to note is that it's not a boolean: it's a union.  An expression like "a or b" has a vector of values for "a" and a vector of values for "b". The result combines both timeseries a and b into the result set.  However, if there are any metrics from "a" and "b" which match *exactly* the same set of label values, then only the "a" one will be included in the result set.

You can restrict the set of labels used for matching using "on(labels...)" or "ignoring(labels...)"

In your example: the subexpressions "sum by (nodename) (...)" will only have a single label {nodename="XXX"}, whilst the subexpression "machine_cpu_cores" very likely has more labels than that (job, instance etc) and may not have a "nodename" label at all.  Since the labels of the LHS and RHS of "or" don't match, both sides are included in the result.

Brian Candler

unread,
May 3, 2023, 1:16:01 PM5/3/23
to Prometheus Users
Another approach is to turn it around using the "unless" operator. This will *only* give timeseries from the vector on the LHS, and will suppress any which have matching label sets on the RHS.

sum by (nodename) (....) unless on (nodename) machine_cpu_cores

[assuming that machine_cpu_cores has a "nodename" label]

Brian Candler

unread,
May 3, 2023, 1:16:51 PM5/3/23
to Prometheus Users
Sorry, ignore that. You want one if it's present, *or* the other.
Reply all
Reply to author
Forward
0 new messages