Search code examples
prometheusgrafanamonitoringpromql

Is there a way to create Grafana Prometheus Queries without repeating the same filters for two different metrics?


I am relativey new to working with grafana promql queries. To cite a specific use case, I am creating my query for filesystem utilization this way:

Filesystem percentage - free for /var < 10%

avg by (hostname) (node_filesystem_free_bytes/node_filesystem_size_bytes{mountpoint="/var"}/node_filesystem_size_bytes{mountpoint="/var"}) < 10%

Data source: node_exporter

Main question: Is there a way for me to implement it without repeating the filter {mountpoint="/var" <+ other filters here>} for both instances?

Any answers, including improvement points on writing my query will be appreciated here as well. Thanks!

Seems like doing it this way seems to return issues (this might be obvious, but I was just exploring to see how it works)

avg by (hostname) ( (node_filesystem_free_bytes/node_filesystem_size_bytes){mountpoint="/var"} )

Solution

  • If both metrics contain same metric, you can specify label selector only on the one of operands, and ommit it on all other.

    For example queries node_filesystem_free_bytes{mountpoint="/var"}/node_filesystem_size_bytes and node_filesystem_free_bytes{mountpoint="/var"}/node_filesystem_size_bytes{mountpoint="/var"} do exactly the same.

    Explanation for this is Prometheus' label matching: unless specified otherwise Prometheus matches operands of binary operation with same labelsets. So once you filtered out labels for one of metrics, others will be filtered in the same way. This, however, will not work if you'll use instructions on or ignoring, and many-to-one matching (with group_left/group_right)