17

In the Grafana documentation, I found that I should be able to query my Prometheus server for all instances delivering monitoring data using the label_values query.

The query I'm using in Grafana is:

label_values(up, instance)

Unfortunately, Prometheus tells me that it is not aware of label_values:

Error executing query: parse error at char 13: unknown function with name "label_values"

I am using Prometheus 2.0.0 and Grafana 5.

What am I doing wrong?

Syntle
  • 5,168
  • 3
  • 13
  • 34
Tim Hartnack
  • 321
  • 1
  • 3
  • 10

3 Answers3

32

label_values() is only a valid function within templating. You can use it to populate a template dropdown with e.g. a list of available instances or available metrics but you can't use it within a dashboard or when querying Prometheus directly.

vadasambar
  • 447
  • 6
  • 14
Oliver
  • 11,857
  • 2
  • 36
  • 42
  • Okay. Despite having multiple instances shown in e.x an up query the label_values() does not fill my dropdown in grafana. Any idea – Tim Hartnack Mar 20 '18 at 08:06
  • 1
    I am getting \: error calling query: parse error at char 13: unknown function with name "label_values"> in Prometheus templating, this is not works in Prometheus... This works only in Grafana. – Sasha Golikov Dec 31 '19 at 06:49
12

By looking into Grafana's template code for Prometheus in grafana/public/app/plugins/datasource/prometheus/metric_find_query.ts you can see that they do not use PromQL, but visit certain endponts to the get the label values, so for label_values(instance) the URL would be something like this:

http://localhost:9090/api/v1/label/instance/values

This one however returns all labels called instance from every metric in the TSDB. If the label is unique for one particular metric, then you have your answer. Otherwise the template code for something like label_values(up, instance) it is slightly more complicated and it seems like it actually downloads the whole series for the metric from:

http://localhost:9090/api/v1/series?match[]=up

also adding time range parameters start and end which I omitted for clarity, then finds the label values by parsing the JSON output.

I would also like to do the same in PromQL, but I have not figured out whether it is possible or not.

mac13k
  • 2,423
  • 23
  • 34
-4

labels(pg_up,instances) this is correct syntax

dejanualex
  • 3,872
  • 6
  • 22
  • 37
shivam
  • 1
  • 1