企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 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' ```