From a2fcf073ea11dab5073424d9099420f933bf99d5 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 15 Sep 2020 18:04:32 +0000 Subject: [PATCH] Group prometheus josb by exporter instead of client --- cookbooks/prometheus/recipes/server.rb | 9 ++++++++- .../prometheus/templates/default/prometheus.yml.erb | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/cookbooks/prometheus/recipes/server.rb b/cookbooks/prometheus/recipes/server.rb index f82fe04bf..297070b33 100644 --- a/cookbooks/prometheus/recipes/server.rb +++ b/cookbooks/prometheus/recipes/server.rb @@ -21,12 +21,19 @@ package "prometheus" clients = search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name) +prometheus_jobs = clients.each_with_object({}) do |client, jobs| + client[:prometheus][:exporters].each do |name, address| + jobs[name] ||= [] + jobs[name] << address + end +end + template "/etc/prometheus/prometheus.yml" do source "prometheus.yml.erb" owner "root" group "root" mode "644" - variables :clients => clients + variables :jobs => prometheus_jobs end service "prometheus" do diff --git a/cookbooks/prometheus/templates/default/prometheus.yml.erb b/cookbooks/prometheus/templates/default/prometheus.yml.erb index 60345ef52..b00c6aaf0 100644 --- a/cookbooks/prometheus/templates/default/prometheus.yml.erb +++ b/cookbooks/prometheus/templates/default/prometheus.yml.erb @@ -11,11 +11,11 @@ scrape_configs: static_configs: - targets: - localhost:9090 -<% @clients.each do |client| -%> - - job_name: <%= client.name %> +<% @jobs.sort.each do |name, addresses| -%> + - job_name: <%= name %> static_configs: - targets: -<% client[:prometheus][:exporters].sort.each do |_,address| -%> +<% addresses.sort.each do |address| -%> - <%= address %> <% end -%> <% end -%> -- 2.39.5