Is it possible to merge prometheus.yml target into 1 line?

Viewed 32

This is my prometheus.yml, it works. The problem is I have hundreds servers and the yml file will be very long...

      - targets: [ 'https://x.x.x.x/' ]
        labels:
          group: 'Infrastructure'
          service: 'Git'
      - targets: [ 'https://a.b.c.d/' ]
        labels:
          group: 'Infrastructure'
          service: 'Database'

I try to merge them to 1 line but get error, is it possible? It's for readability and maintenance.

      - targets: [ 'https://a.b.c.d/' ] labels: group: 'Infrastructure' service: 'Database'

Update: I'll change to the following format, it's more readable to main hundres targets.

[
{"targets":["https://x.x.x.x/"],"labels":{"group":"Infrastructure","service":"Git"}},
{"targets":["https://a.b.c.d/"],"labels":{"group":"Infrastructure","service":"Database"}}
]
1 Answers

It is possible to merge any YAML config into a single line by converting it to JSON line, since JSON is a YAML subset. For example, the yaml above can be converted to the following JSON:

[
  {
    "targets": ["https://x.x.x.x/"],
    "labels": {
          "group": "Infrastructure",
          "service": "Git"
    }
  },
  {
    "targets": ["https://a.b.c.d/"],
    "labels": {
          "group": "Infrastructure",
          "service": "Database"
    }
  }
]

And this json can be easily converted to a single-line YAML:

[{"targets":["https://x.x.x.x/"],"labels":{"group":"Infrastructure","service":"Git"}},{"targets":["https://a.b.c.d/"],"labels":{"group":"Infrastructure","service":"Database"}}]

As you can see, the resulting yaml becomes unreadable and unmaintainable. So the question is why do you need this? :)

Related