summaryrefslogtreecommitdiff
path: root/doc/operations/metrics/dashboards/templating_variables.md
blob: 20071300decde0147914d77c0786dc54a0225a64 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
---
stage: Monitor
group: Monitor
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
---

# Templating variables for metrics dashboards **(FREE)**

> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/214539) in GitLab 13.0.

Templating variables can be used to make your metrics dashboard more versatile.

`templating` is a top-level key in the
[dashboard YAML](yaml.md#dashboard-top-level-properties).
Define your variables in the `variables` key, under `templating`. The value of
the `variables` key should be a hash, and each key under `variables`
defines a templating variable on the dashboard, and may contain alphanumeric and underscore characters.

A variable can be used in a Prometheus query in the same dashboard using the syntax
described [in Using Variables](variables.md).

## `text` variable type

WARNING:
This variable type is an _alpha_ feature, and is subject to change at any time
without prior notice!

For each `text` variable defined in the dashboard YAML, a free text field displays
on the dashboard UI, allowing you to enter a value for each variable.

The `text` variable type supports a simple and a full syntax.

### Simple syntax

This example creates a variable called `variable1`, with a default value
of `default value`:

```yaml
templating:
  variables:
    variable1: 'default value'     # `text` type variable with `default value` as its default.
```

### Full syntax

This example creates a variable called `variable1`, with a default value of `default`.
The label for the text box on the UI is the value of the `label` key:

```yaml
templating:
  variables:
    variable1:                       # The variable name that can be used in queries.
      label: 'Variable 1'            # (Optional) label that will appear in the UI for this text box.
      type: text
      options:
        default_value: 'default'     # (Optional) default value.
```

## `custom` variable type

WARNING:
This variable type is an _alpha_ feature, and is subject to change at any time
without prior notice!

Each `custom` variable defined in the dashboard YAML creates a dropdown
selector on the dashboard UI, allowing you to select a value for each variable.

The `custom` variable type supports a simple and a full syntax.

### Simple syntax

This example creates a variable called `variable1`, with a default value of `value1`.
The dashboard UI displays a dropdown with `value1`, `value2` and `value3`
as the choices.

```yaml
templating:
  variables:
    variable1: ['value1', 'value2', 'value3']
```

### Full syntax

This example creates a variable called `variable1`, with a default value of `value_option_2`.
The label for the text box on the UI is the value of the `label` key.
The dashboard UI displays a dropdown with `Option 1` and `Option 2`
as the choices.

If you select `Option 1` from the dropdown, the variable is replaced with `value option 1`.
Similarly, if you select `Option 2`, the variable is replaced with `value_option_2`:

```yaml
templating:
  variables:
    variable1:                           # The variable name that can be used in queries.
      label: 'Variable 1'                # (Optional) label that will appear in the UI for this dropdown.
      type: custom
      options:
        values:
          - value: 'value option 1'        # The value that will replace the variable in queries.
            text: 'Option 1'               # (Optional) Text that will appear in the UI dropdown.
          - value: 'value_option_2'
            text: 'Option 2'
            default: true                  # (Optional) This option should be the default value of this variable.
```

## `metric_label_values` variable type

WARNING:
This variable type is an _alpha_ feature, and is subject to change at any time
without prior notice!

### Full syntax

This example creates a variable called `variable2`. The values of the dropdown are
all the different values of the `backend` label in the Prometheus series described by
`up{env="production"}`.

```yaml
templating:
  variables:
    variable2:                           # The variable name that can be interpolated in queries.
      label: 'Variable 2'                # (Optional) label that will appear in the UI for this dropdown.
      type: metric_label_values
      options:
        series_selector: 'up{env="production"}'
        label: 'backend'
```