-
Notifications
You must be signed in to change notification settings - Fork 17
/
dynamic_variables.html.md.erb
55 lines (35 loc) · 2.72 KB
/
dynamic_variables.html.md.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---
title: Grafana dynamic variables
weight: 10
hide_in_navigation: true
link_in_toc: true
summary:
---
# <%= current_page.data.title %>
If you want a dynamic drop down list for a dashboard the first step is to create a query variable, doing this will allow us to reuse common dashboards and allow dashboards to be clearer.
## Example
Services on kubernetes are deployed to different namespaces. A dashboard will not generally span different organisations but may well use different namespaces. Also if you use the dashboard in a different organisation then the list of spaces will be different.
## Variable
You should look at your data and see what labels you have available to use. This can easily be done by going to your Prometheus ( or other data source ) instance and querying back the raw data. In our example we are interested in **memory_utilization** so a simple selection of memory_utilization yields.
### Example 1
```
memory_utilization{app="alertmanager-dev-get-into-teaching",exported_instance="0",guid="cdd26aed-1041-4263-9330-b541ff233df5",instance="paas-prometheus-exporter-prod-get-into-teaching.london.cloudapps.digital:443",job="PaaS Prometheus Exporter",organisation="dfe-teacher-services",space="get-into-teaching"}
```
2.3572323843836784
### Example 2
```
memory_utilization{app="alertmanager-prod-get-into-teaching",exported_instance="0",guid="3fee9a22-0881-468e-8d6c-6e3b7db0b5fe",instance="paas-prometheus-exporter-prod-get-into-teaching.london.cloudapps.digital:443",job="PaaS Prometheus Exporter",organisation="dfe-teacher-services",space="get-into-teaching-monitoring"}
```
3.026614896953106
You can see there are some useful labels, in our case we are interested in space.
## Grafana Variable
So we need to go to our dashboard and using the ‘COG’ at the top of the dashboard add a new variable. This should be a query variable and you should enter:
![Example of Dynamic Variables](dynamic_variables.png)
The key value is `label_value(space)`, this returns all the labels named ‘space’ into the variable. [Documentation](https://docs.huihoo.com/grafana/2.6/datasources/prometheus/index.html)
You should notice when you have created your variable it gives an example of the data it has returned. If this looks good, you are ready to ADD the variable.
Note that the queried label must be included in **all** the metrics. If not, the list may be empty. To workaround this issue, it is possible to query a particular metric which is guaranteed to return the label using the syntax `label_values(metric, label)`. Example:
```
label_values(memory_utilization, space)
```
## Using Variable
You should now notice a populated drop down list at the top of your dashboard, now it is a simple case of including the setting in your panels.