From 67a9b76ebb212b99bf39f98688e8d6d0e8bcbfc2 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 16 Sep 2020 09:33:09 +0100 Subject: [PATCH] Label targets with appropriate instance names --- cookbooks/prometheus/recipes/server.rb | 4 ++-- cookbooks/prometheus/templates/default/prometheus.yml.erb | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cookbooks/prometheus/recipes/server.rb b/cookbooks/prometheus/recipes/server.rb index 5132eabdd..1e164cd1d 100644 --- a/cookbooks/prometheus/recipes/server.rb +++ b/cookbooks/prometheus/recipes/server.rb @@ -26,10 +26,10 @@ package "prometheus" clients = search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name) -prometheus_jobs = clients.each_with_object({}) do |client, jobs| +prometheus_jobs = clients.sort_by(&:name).each_with_object({}) do |client, jobs| client[:prometheus][:exporters].each do |name, address| jobs[name] ||= [] - jobs[name] << address + jobs[name] << { :address => address, :name => client.name } end end diff --git a/cookbooks/prometheus/templates/default/prometheus.yml.erb b/cookbooks/prometheus/templates/default/prometheus.yml.erb index b00c6aaf0..66051b622 100644 --- a/cookbooks/prometheus/templates/default/prometheus.yml.erb +++ b/cookbooks/prometheus/templates/default/prometheus.yml.erb @@ -11,11 +11,13 @@ scrape_configs: static_configs: - targets: - localhost:9090 -<% @jobs.sort.each do |name, addresses| -%> +<% @jobs.sort.each do |name, targets| -%> - job_name: <%= name %> static_configs: +<% targets.each do |target| -%> - targets: -<% addresses.sort.each do |address| -%> - - <%= address %> + - <%= target[:address] %> + labels: + instance: <%= target[:name].split(".").first %> <% end -%> <% end -%> -- 2.39.5