Search code examples
prometheuspromql

query Prometheus HTTP API by list of metrics


I want to query prometheus with a list of metric names in a single curl request.

There is a way to do it?

Something like this -

curl 'localhost:9090/api/v1/labels?name=<list_of_metric_names>'

I expect response like this -

[{
  metric_name:"metric1",
  labels:["label1", "label2"]
 },
 {
  metric_name:"metric2",
  labels:[..]
 }]

Solution

  • Endpoint api/v1/labels returns a combined list of label names. If this is acceptable, you can pass selector like {__name__=~"metric1|metric2"} to get them.


    If you need list grouped by metric, you might use endpoint api/v1/series. It returns the list of time series matching selector (basically all the possible labels for every selected metric).

    For example, command

    curl -g 'https://prometheus.demo.do.prometheus.io/api/v1/series?' \
      --data-urlencode 'match[]=up{job="alertmanager"}' \
      --data-urlencode 'match[]=node_cpu_seconds_total{mode="user"}'
    

    will return following json

    {
        "status":"success",
        "data":[
            {
                "__name__":"node_cpu_seconds_total",
                "cpu":"0",
                "env":"demo",
                "instance":"demo.do.prometheus.io:9100",
                "job":"node",
                "mode":"user"
            },
            {
                "__name__":"up",
                "env":"demo",
                "instance":"demo.do.prometheus.io:9093",
                "job":"alertmanager"
            }
        ]
    }
    

    Here, you'll receive all possible combinations of label values, and will need to aggregate them.