1. Templating variables for metrics dashboards
原文:https://docs.gitlab.com/ee/operations/metrics/dashboards/templating_variables.html
2. Templating variables for metrics dashboards
在 GitLab 13.0 中引入 .
模板变量可用于使指标仪表板更加通用.
templating是仪表板 YAML 中的顶级键. 在templating下的variables键中定义变量. variables键的值应为哈希, variables下的每个键在仪表板上定义一个模板变量,并且可以包含字母数字和下划线字符.
可以使用使用变量中所述的语法在同一仪表板的 Prometheus 查询中使用变量 .
2.1. text variable type
警告:此变量类型是alpha功能,如有更改,恕不另行通知!
对于仪表板 YAML 中定义的每个text变量,仪表板 UI 上都会有一个自由文本框,您可以为每个变量输入一个值.
text变量类型支持简单和完整的语法.
2.1.1. Simple syntax
本示例创建一个名为variable1 ,其默认值为default value :
templating:
  variables:
    variable1: 'default  value'     # `text` type variable with `default value` as its default. 
2.1.2. Full syntax
本示例创建一个名为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. 
2.2. custom variable type
警告:此变量类型是alpha功能,如有更改,恕不另行通知!
仪表板 YAML 中定义的每个custom变量都会在仪表板 UI 上创建一个下拉选择器,允许您为每个变量选择一个值.
custom变量类型支持简单和完整的语法.
2.2.1. Simple syntax
本示例创建一个名为variable1 ,其默认值为value1 . 仪表板界面将显示一个下拉列表,其中包含value1 , value2和value3作为选择.
templating:
  variables:
    variable1: ['value1', 'value2', 'value3'] 
2.2.2. Full syntax
本示例创建一个名为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. 
2.3. metric_label_values variable type
警告:此变量类型是alpha功能,如有更改,恕不另行通知!
2.3.1. Full syntax
本示例创建一个名为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'