How do I write a Prometheus query that returns the value of a label?
Asked Answered
D

9

66

I'm making a Grafana dashboard and want a panel that reports the latest version of our app. The version is reported as a label in the app_version_updated (say) metric like so:

app_version_updated{instance="eu99",version="1.5.0-abcdefg"}

I've tried a number of Prometheus queries to extract the version label as a string from the latest member of this time series, to no effect.

For example, the query

count(app_version_updated) by (version)

returns a {version="1.5.0-abcdefg"} element with a value of 1. When put in a Grafana dashboard in a single value panel, this doesn't display the version string but instead the count value (1).

How can I construct a Prometheus query that returns the version string?

Dunkle answered 22/7, 2016 at 11:58 Comment(0)
T
34

My answer tries to elaborate on Carl's answer. I assume that the GUI layout may have changed a little since 2016, so it took me while to find the "name" option.

Assuming you have a metric as follows:

# HELP db2_prometheus_adapter_info Information on the state of the DB2-Prometheus-Adapter
# TYPE db2_prometheus_adapter_info gauge
db2_prometheus_adapter_info{app_state="UP"} 1.0

and you would like to show the value of the label app_state.

Follow these steps:

  • Create a "SingleStat" visualization.
  • Go to the "Queries" tab:
    • Enter the name (here db2_prometheus_adapter_info) of the metric.
    • Enter the label name as the legend using the {{[LABEL]}} notation (here {{app_state}}).
    • Activate the "instant" option.

Settings in Queries Tab

  • Go to the "Visualization" tab:
    • Choose the value "Name" under "Value - Stat".

Setting in Visualization Tab

Note on the "Instant" setting: This setting switches from a range query to a simplified query only returning the most recent value of the metric (also see What does the "instant" checkbox in Grafana graphs based on prometheus do?). If not activated, the panel will show an error as soon as there is more than one distinct value for the label in the history of the metric. For a "normal" metric you would remedy this by choosing "current" in the "Value - Stat" option. But doing so here prevents your label value to be shown.

Trireme answered 9/9, 2019 at 8:47 Comment(1)
SingleStat is deprecated in v7 and removed in v8. It was replaced with Stat.Rattrap
C
17

We recently added support for displaying the serie name as a value in the single stat panel (https://github.com/grafana/grafana/issues/4740). So you have to run our nightly build until we release 4.0.

Just make sure the query returns one serie and you can use the "name" value in the dropdown under Options -> big value.Then you can format the string using the legend formater. Ex {{job}} would return "node" as a serie name.

I hope this answers your question.

Crofoot answered 25/8, 2016 at 8:26 Comment(0)
O
9

This worked for me.

label_values(my_metric{type= "xxx", another_label="xxx"},target_label)
Ox answered 20/5, 2020 at 13:13 Comment(4)
That doesn't work. I what field do you put this, because it can't be processed by PromQL ?Kisor
Yes, "label_values" function does not exist in PromQL. It is a grafana thing that I used to create a variable in the dashbord forexample.Ox
explain this more pleaseSolicit
label_values can only be used on the variables screen in a dashboard in grafana grafana.com/docs/grafana/latest/datasources/prometheus/….Particular
M
9

With Stat visualization in Grafana 8+, you can set the legend to the intended label name and then change the Stat Styles -> Text Mode to Name enter image description here

Merce answered 12/2, 2022 at 17:13 Comment(0)
M
6

While most of @marcus-rickert's answer is still valid in Grafana 7, there is some change. The 'Stat' 'Name' field seems to be gone, instead in the 'Field' panel, you can set the 'Display name' to the label you want to show.

enter image description here

Matri answered 31/5, 2020 at 12:19 Comment(0)
T
2

Prometheus doesn't have any functions that return strings, what you're looking for is for a Grafana singlestat to be able to display a label value - which it unfortunately doesn't support yet.

https://github.com/grafana/grafana/issues/5094 tracks this.

Tarn answered 22/7, 2016 at 12:9 Comment(1)
This feature is now implementedWithrow
C
2

I wanted to get all the various values of a label from Prometheus to put into a variable in Grafana. I used this simple query to populate my varialbe Drop-Down list:

label_values(myLabelName)

This returned the expected "value01", "value02" etc

Centromere answered 22/12, 2020 at 10:42 Comment(2)
yes I agree with this. It works for me.Szechwan
How to get a single label, when bunch of labels get returned by label_values functionKokand
O
2

Another update on very latest kube-prometheus-stack using grafana 8.0.3. I still faced this issue, for me in a Bar gauge I still had this issue in scenarios where it only returned 1 value, but with 2+ values the Legend field works OK.

My solution:

Go to Edit Panel, find Display Name, type

${__field.labels.insertYourLegendValueHere}

Outrank answered 16/7, 2021 at 6:51 Comment(0)
O
2

You can use sum followed by the label you trying to get the value from.

For example:

sum(windows_cpu_info{instance="$hostname"}) by (l2_cache_size)

In this case, l2_cache_size is the label I wanted to get the returned value from.

Outdated answered 28/3, 2023 at 1:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.