How to execute multiple queries in one call in Prometheus

Viewed 21537

I'm running prometheus inside kubernetes cluster.

I need to send queries to Prometheus every minute, to gather information of many metrics from many containers. There are too match queries, so I must combine them.

I know how I can ask Prometheus for one metric information on multiple containers: my_metric{container_name=~"frontend|backend|db"} , but I haven't found a way to ask Prometheus for multiple metric information in one query.

I'm looking for the equivalent to 'union' in sql queries.

3 Answers

I found here this solution: {__name__=~"metricA|metricB|metricC",container_name=~"frontend|backend|db"}.

You can use the or operator, however this does not generalise as it ignores metric names. I'd suggest making multiple queries to the API.

PromQL doesn't provide the ability to execute multiple queries in a single api call. But MetricsQL, which is backwards-compatible to PromQL, provides such an ability via union function:

union(
  query1,
  query2,
  ...
  queryN,
)

This returns all the time series from all the query* queries. Note that it is expected that each time series has an unique set of labels (including metric name aka ___name__ label). If this isn't the case, then duplicate series with identical labelsets will be removed from the result. In this case the alias and label_set functions may be useful for adding unique labels per query*.

Related