From: Tom Hughes Date: Sun, 24 Jan 2021 19:12:21 +0000 (+0000) Subject: Add infrastructure to support relabelling metrics X-Git-Url: https://git.openstreetmap.org./chef.git/commitdiff_plain/ff13884a1e237c946502c2a4c67dbb12c9eebdbd Add infrastructure to support relabelling metrics --- diff --git a/cookbooks/prometheus/recipes/server.rb b/cookbooks/prometheus/recipes/server.rb index 2ebf5a823..a5a24267d 100644 --- a/cookbooks/prometheus/recipes/server.rb +++ b/cookbooks/prometheus/recipes/server.rb @@ -159,13 +159,19 @@ search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client if exporter.is_a?(Hash) name = exporter[:name] address = exporter[:address] + metric_relabel = exporter[:metric_relabel] || [] else name = key address = exporter + metric_relabel = [] end jobs[name] ||= [] - jobs[name] << { :address => address, :name => client.name } + jobs[name] << { + :address => address, + :instance => client.name.split(".").first, + :metric_relabel => metric_relabel + } end end diff --git a/cookbooks/prometheus/resources/exporter.rb b/cookbooks/prometheus/resources/exporter.rb index 69cb80e14..b56cbf789 100644 --- a/cookbooks/prometheus/resources/exporter.rb +++ b/cookbooks/prometheus/resources/exporter.rb @@ -28,6 +28,7 @@ property :command, :kind_of => String property :options, :kind_of => [String, Array] property :environment, :kind_of => Hash, :default => {} property :service, :kind_of => String +property :metric_relabel, :kind_of => Array action :create do systemd_service service_name do @@ -57,7 +58,9 @@ action :create do end node.default[:prometheus][:exporters][new_resource.port] = { - :name => new_resource.exporter, :address => listen_address + :name => new_resource.exporter, + :address => listen_address, + :metric_relabel => new_resource.metric_relabel } end diff --git a/cookbooks/prometheus/templates/default/prometheus.yml.erb b/cookbooks/prometheus/templates/default/prometheus.yml.erb index b7a3cf81e..d90c8292b 100644 --- a/cookbooks/prometheus/templates/default/prometheus.yml.erb +++ b/cookbooks/prometheus/templates/default/prometheus.yml.erb @@ -34,7 +34,17 @@ scrape_configs: - targets: - "<%= target[:address] %>" labels: - instance: <%= target[:name].split(".").first %> + instance: <%= target[:instance] %> +<% end -%> +<% end -%> + metric_relabel_configs: +<% @jobs.sort.each do |name, targets| -%> +<% targets.each do |target| -%> +<% target[:metric_relabel].each do |relabel| -%> + - source_labels: [instance,<%= relabel[:source_labels] %>] + regex: "<%= target[:instance] %>;<%= relabel[:regex] %>" + action: <%= relabel[:action] %> +<% end -%> <% end -%> <% end -%>