How do I write a Prometheus query that returns the value of a label? - grafana

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 (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?

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.
Go to the "Visualization" tab:
Choose the value "Name" under "Value - Stat".
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.

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.

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.

This worked for me.
label_values(my_metric{type= "xxx", another_label="xxx"},target_label)

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

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.

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}

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

Related

Create a variable in Prometheus/Grafana with all values selected by default

I'm using Grafana with Prometheus as a Datasource.
I'm trying to create a variable with a filtered list of servers and I want to use this list to fix list of servers to display in my Dashboard.
My variable, named servers_front, is a query (label_values(info_fqdn)) with a regex to choose my servers /.*_front/
I want to use this full list of servers by default without displaying a combobox to choose which one I want to display.
Another requirement : If a new server that matches /.*_front/ is created, I want the list is automatically updated
Edit the variable
Variables > Edit > Selection Options >enable Include All option >Custom all value>.*
When saving dashboard you have an option to "save current variables", which means that currently selected value will be stored as a default when dashboard is loaded.
So if you have a templating variable with enabled "All" option you can save your dashboard ensuring "All" is selected (with option mentioned above enabled) and it should work.
Note that what is stored in dashboard is real "all" option and not an expanded list of all currently present values, so it should be dynamically expanded.
If you want that variable hidden then either you can hide the variable and save again after changing and saving its value or (if you have variable hidden from the start you can open url for dashboard with "&var-servers_front=All" appended (which will force variable to have "All" value and save dashboard with "save current variables".

How to access variable, passed through URL in grafana?

I am trying to make a detailed dashboard in grafana that opens on click. I do it by passing a variable to the dashboard debending on the clicked facility. When the dashboard opens it needs to display value using the passed url variable inst. I just cannot seem to get it working. Here is an example of my dashboard link.
How can i use the variable inst?
I will add gogibogi4's correct answer. The Grafana documentation is lacking on this.
In your drill-down dashboard, add a constant variable. To be retrieved in Grafana, you need the prefix var- then the name of the variable in the query string. Let's assume the variable is "region." The URL coming into the drilldown dashboard should look similar to the following:
/drilldowndashboard/?var-region=NorthWest
Drilldown Dashboard Settings:
In the drilldown dashboard settings, create a variable. The Name in this example is "region" Make the type Constant. Under Hide, you can just leave that unselected. Under Constant options, you can write the word "blank" The preview of values will just show the word "blank" but go ahead and save it as it will be replaced in the query.
Query:
You can refer to the variable using [[region]] in your data query.
Parent Dashboard:
The parent dashboard merely needs to refer to the drilldown dashboard with var-region as a query parameter.
I figured out the solution on my own. The variable that i wanted to pass was var-inst, the trick to use it is to create a custom template variable named inst and give it a dummy value. After that is done i can use the passed value by using $inst. Note that in order for the variable to be passed i had to go back to my primary dashboard and then i had to click my link again. After that the value got passed and is working perfectly.
just referred above answer by gogibogi4 and it worked for me but when I tried to see variable information then I saw I have selected constant as a type but automatically it got converted into datasource and type is selected grafana. when I try to select them manually it didn't worked. this is some kind of bug of grafana I guess.

Create absolute link with specific value of cell in table view

I want to render a absolute link in table from value of cell but I got a relative link instead.
Here is my configuration:
What I got when click on cell:
http://10.93.9.209:3000/http%3A%2F%2F10.131.227.253%2Fjob%2FPerformance%20Testing%20Pipeline%2Fjob%2Fstable%2F21%2F
In Grafana 7 (new table panel) it's ${__data.fields[2]} where number is column index.
This is a change introduced in one of the new Grafana versions. If you want an absolute URL, you must use ${__cell:raw} instead of ${__cell}. Reference
If you want to reference another column when clicking:
This is possible, I am using 9.1.7 and originally used the old table; however, after migrating noticed the same issue. That said, I found a way to make this work:
Add the ID column that you want to hide; example simID
If the column is the first column, say 0; then use:
var-whatever=${__data.fields[0]}
Now, override the column, field with name: simID Add option ‘Hide in Table’ and turn on
I also had to format the number, e.g. remove decimals & set unit to standard
Hope that helps someone… took me a good 2 hours to figure.
Btw, it also works on graphs and you do not have to hide; aka reference your original query in the datasource and add a data link with the same options.
What result do you get if you try $__cell_2?
The syntax above no longer works. The value is now ${__value:raw}
$__cell_2 above solution worked for me.
With the new table panel introduced in Grafana either ${__cell} or ${__cell:raw} do not work for this now.
You must use ${__value:raw}

Kibana (current version) - Not showing string type fields in visualization

I am using current version of Kibana, not showing string type fields to select for x-axis (Line or Bar charts). Am I missing anything here?
Here is my sample json. How to breakdown by IterationName?
JSON
{
"iterationName":"Sprint 60",
"manualEffortInSec":300,
"automationEffortInSec":100,
"hrlyRate":150,
"manualEffortCost":750,
"automationEffortCost":250,
"verdictInteger":1
}
for visualizing fields in buckets(x-axis) you should, for example, choose the "Terms" and under that choose your field which is "iterationName".
this part shows all the fields regardless of their type.
maybe there is a problem in your mapping, it is better to go to the discover tab and check the fields of your index pattern to see if all the fields are indexed or not

Prometheus & Grafana: templating and "bool" modifier

I am building Grafana (4.1.1) graphs with a Prometheus (1.1.2) backend, and am trying to use Templates to select/deselect certain sub-sets of metrics on a dashboard. I have a template variable called "$POP" which describes my city locations, and a template variable called "$Resolver" which describes the type of resolver I am using ("unbound" or "pdns" are the possible string results.)
I have these two metrics like this that work quite well currently selecting on the various values in $POP:
irate(dnsdist_recursor_main_servers_unbound_drops{env="prod",loc=~"$POP"}[1m]) > 0
irate(dnsdist_recursor_main_servers_pdns_drops{env="prod",loc=~"$POP"}[1m]) > 0
Putting aside for the moment that instead of labels the selection criteria are embedded in the metric name (I know it's not ideal but this is just an example of larger problem sets so please do not suggest how I can use labels) I would like the following results:
When (~"$Resolver" == "unbound") I would like to show the first metric (dnsdist_recursor_main_servers_unbound_drops) and when (~"$Resolver" == "pdns") I would like to show the second one (dnsdist_recursor_main_servers_pdns_drops) and when both are selected I'd like to show both metric set data. Basically, I hope to have an on/off visibility trigger for a metric that is keyed to my $Resolver template value.
How do I create a boolean such that the metric is shown or not shown based on the setting of the string value in the templated setting of $Resolver? The trick here is that the selection criteria is in the metric name, not a label, which is really stumping me. I tried "bool" modifiers but was never able to get anything to work on string equality tests. I'm sure it's just a syntax issue but examples are thin on the ground.
What you can take advantage of here is that there's no uniqueness restriction on label selectors, so you can do {resolver="$resolver",resolver="unbound"}