简体   繁体   中英

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

But I can't seem to get it to generate a valid relabeling config for 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:

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.

This seems to be a problem with the ruby parser? 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.

In YAML, variable arrays can be:

array:
- element
- another_element

or:

array: [element, another_element]

In Puppet, they look like:

$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:

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

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' ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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