184.md 4.7 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
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
# Templating variables for metrics dashboards

> 原文:[https://docs.gitlab.com/ee/operations/metrics/dashboards/templating_variables.html](https://docs.gitlab.com/ee/operations/metrics/dashboards/templating_variables.html)

*   [`text` variable type](#text-variable-type)
    *   [Simple syntax](#simple-syntax)
    *   [Full syntax](#full-syntax)
*   [`custom` variable type](#custom-variable-type)
    *   [Simple syntax](#simple-syntax-1)
    *   [Full syntax](#full-syntax-1)
*   [`metric_label_values` variable type](#metric_label_values-variable-type)
    *   [Full syntax](#full-syntax-2)

# Templating variables for metrics dashboards[](#templating-variables-for-metrics-dashboards "Permalink")

在 GitLab 13.0 中[引入](https://gitlab.com/gitlab-org/gitlab/-/issues/214539) .

模板变量可用于使指标仪表板更加通用.

`templating`[仪表板 YAML 中](yaml.html#dashboard-top-level-properties)的顶级键. 在`templating`下的`variables`键中定义变量. `variables`键的值应为哈希, `variables`下的每个键在仪表板上定义一个模板变量,并且可以包含字母数字和下划线字符.

可以使用[使用变量中](variables.html)所述的语法在同一仪表板的 Prometheus 查询[中使用变量](variables.html) .

## `text` variable type[](#text-variable-type "Permalink")

**警告:**此变量类型是*alpha*功能,如有更改,恕不另行通知!

对于仪表板 YAML 中定义的每个`text`变量,仪表板 UI 上都会有一个自由文本框,您可以为每个变量输入一个值.

`text`变量类型支持简单和完整的语法.

### Simple syntax[](#simple-syntax "Permalink")

本示例创建一个名为`variable1` ,其默认值为`default value`

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

### Full syntax[](#full-syntax "Permalink")

本示例创建一个名为`variable1` ,其默认值为`default` . UI 上文本框的标签将为`label`键的值:

```
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[](#custom-variable-type "Permalink")

**警告:**此变量类型是*alpha*功能,如有更改,恕不另行通知!

仪表板 YAML 中定义的每个`custom`变量都会在仪表板 UI 上创建一个下拉选择器,允许您为每个变量选择一个值.

`custom`变量类型支持简单和完整的语法.

### Simple syntax[](#simple-syntax-1 "Permalink")

本示例创建一个名为`variable1` ,其默认值为`value1` . 仪表板界面将显示一个下拉列表,其中包含`value1``value2``value3`作为选择.

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

### Full syntax[](#full-syntax-1 "Permalink")

本示例创建一个名为`variable1` ,其默认值为`value_option_2` . UI 上文本框的标签将为`label`键的值. 仪表板界面将显示一个下拉列表,其中包含`Option 1``Option 2` .

如果从下拉列表中选择`Option 1` ,则变量将被替换为`value option 1` . 同样,如果选择`Option 2` ,则变量将替换为`value_option_2`

```
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[](#metric_label_values-variable-type "Permalink")

**警告:**此变量类型是*alpha*功能,如有更改,恕不另行通知!

### Full syntax[](#full-syntax-2 "Permalink")

本示例创建一个名为`variable2` . 下拉列表的值将是`up{env="production"}`描述的 Prometheus 系列中`backend`标签的所有不同值.

```
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' 
```