简体   繁体   English

带有 Puppet 的 Prometheus 配置文件:如何处理 yaml 配置文件中的引号和括号?

[英]Prometheus config file with Puppet: how to handle quotes and brackets in yaml config file?

I'm trying to write a Prometheus config with this module: https://github.com/voxpupuli/puppet-prometheus我正在尝试使用此模块编写 Prometheus 配置: https : //github.com/voxpupuli/puppet-prometheus

But I can't seem to get it to generate a valid relabeling config for Prometheus.但我似乎无法让它为 Prometheus 生成有效的重新标记配置。

This is the relevant part of my current config:这是我当前配置的相关部分:

'relabel_configs' => [
  {
    'source_labels' => '[__meta_consul_node]',
    'regex'         => '^(.*)$',
    'target_label'  => 'instance',
    'replacement'   => '$1',
  },
  {
    'source_labels' => '[__meta_consul_service]',
    'regex'         => '^(.*)$',
    'target_label'  => 'job',
    'replacement'   => '$1',
  }
],

This is what I get in my prometheus.yml:这是我在 prometheus.yml 中得到的:

relabel_configs:
  - source_labels: '[__meta_consul_node]'
    regex: ^(.*)$
    target_label: instance
    replacement: $1
  - source_labels: '[__meta_consul_service]'
    regex: ^(.*)$
    target_label: job
    replacement: $1

What I want is:我想要的是:

relabel_configs:
  - source_labels: ['__meta_consul_node']
    regex: '^(.*)$'
    target_label: 'instance'
    replacement: '$1'
  - source_labels: ['__meta_consul_service']
    regex: '^(.*)$'
    target_label: 'job'
    replacement: '$1'

I tried a ton of things, but could not figure out how to format hash values in my manifest so that I would get the correct config file for Prometheus.我尝试了很多东西,但无法弄清楚如何在我的清单中格式化哈希值,以便我获得 Prometheus 的正确配置文件。

This seems to be a problem with the ruby parser?这似乎是 ruby​​ 解析器的问题? How do I escape the ' correctly?我如何正确转义'

I think the confusion here is how variable arrays are handled in Puppet and in YAML, and how strings are parsed in YAML.我认为这里的困惑在于如何在 Puppet 和 YAML 中处理变量数组,以及如何在 YAML 中解析字符串。

In YAML, variable arrays can be:在 YAML 中,变量数组可以是:

array:
- element
- another_element

or:或者:

array: [element, another_element]

In Puppet, they look like:在 Puppet 中,它们看起来像:

$array = [element, another_element]

Therefore, you can have a clean one-to-one mapping of the arrays by simply specifying an array of strings in your Puppet hash for the source_labels key like:因此,您可以通过简单地在 Puppet 哈希中为source_labels键指定一个字符串数组来获得数组的干净的一对一映射,例如:

'relabel_configs' => [
  {
    'source_labels' => ['__meta_consul_node'], # single element string array
    'regex'         => '^(.*)$',
    'target_label'  => 'instance',
    'replacement'   => '$1',
  },
  {
    'source_labels' => ['__meta_consul_service'], # single element string array
    'regex'         => '^(.*)$',
    'target_label'  => 'job',
    'replacement'   => '$1',
  }
],

and this will correctly generate an array of strings for your source_labels as you described for what you desired.这将正确地为您的source_labels生成一个字符串数组,如您所描述的那样。

relabel_configs:
  - source_labels: ['__meta_consul_node'] # single element string array
    regex: ^(.*)$
    target_label: instance
    replacement: $1
  - source_labels: ['__meta_consul_service'] # single element string array
    regex: ^(.*)$
    target_label: job
    replacement: $1

Note that the YAML will be parsed exactly the same for quoted and unquoted strings (eg target_label: job is the same as target_label: 'job' ).请注意,对于带引号和不带引号的字符串,YAML 的解析方式将完全相同(例如target_label: jobtarget_label: 'job' )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM