# A template for exporting metrics for prometheus using consul-templaterb # This template can be configure the following way with environment variables # Environment variables to filter services/instances # SERVICES_TAG_FILTER: basic tag filter for service (default HTTP) # INSTANCE_MUST_TAG: Second level of filtering (optional, default to SERVICES_TAG_FILTER) # INSTANCE_EXCLUDE_TAG: Exclude instances having the given tag # EXCLUDE_SERVICES: comma-separated services to exclude (example: consul-agent-http,mesos-slave,mesos-agent-watcher) <% service_tag_filter = ENV['SERVICES_TAG_FILTER'] || nil instance_must_tag = ENV['INSTANCE_MUST_TAG'] || service_tag_filter instance_exclude_tag = ENV['INSTANCE_EXCLUDE_TAG'] # Services to hide services_blacklist = (ENV['EXCLUDE_SERVICES'] || '').split(',') # Compute the health of a Service def compute_state(snode) states = ['passing', []] snode['Checks'].each do |chk| st = chk['Status'] states[1] << st if st == 'critical' states[0] = st elsif st == 'warning' && states[0] == 'passing' states[0] = st end end states end backends = {} service_count = 0 services(tag: service_tag_filter).each do |service_name, tags| if !services_blacklist.include?(service_name) && (instance_must_tag.nil? || tags.include?(instance_must_tag)) service_count += 1 res = { 'passing' => 0, 'warning' => 0, 'critical' => 0, } service(service_name).each do |snode| state = compute_state(snode)[0] res[state] += 1 end backends[service_name] = { state: res, stats: service(service_name).endpoint.stats, } end end %> # HELP consul_datacenters_count A gauge of number of datacenters available # TYPE consul_nodes_count gauge consul_datacenters_count <%= datacenters().count %> <%= datacenters().endpoint.stats.last_success_or_error.to_i * 1000 %> # HELP consul_nodes_count A gauge of number of nodes in the cluster # TYPE consul_nodes_count gauge consul_nodes_count <%= nodes().count %> <%= nodes().endpoint.stats.last_success_or_error.to_i * 1000 %> # HELP consul_services_count A gauge of the number of services # TYPE consul_services_count gauge consul_services_count <%= service_count %> <%= services(tag: service_tag_filter).endpoint.stats.last_success_or_error.to_i * 1000 %> # HELP consul_service_count A gauge of number instances of service with their current state # TYPE consul_service_count gauge # HELP consul_service_stats_requests_total A counter of requests performed by consul-templaterb, shows activity on a service # TYPE consul_service_stats_requests_total counter # HELP consul_service_stats_requests_bytes A counter of bytes transfered from Consul Agent to consul-templaterb # TYPE consul_service_stats_requests_bytes counter # HELP consul_service_stats_requests_bytes_per_sec A counter of bytes transfered from Consul Agent to consul-templaterb # TYPE consul_service_stats_requests_bytes_per_sec gauge # HELP consul_service_changes_total A counter of changes that occured on a service # TYPE consul_service_changes_total counter <% json_backends = {} now = Time.now.utc backends.each_pair do |service_name, service_info| service_info[:state].each_pair do |state_name, state_count| %>consul_service_count{service="<%= service_name %>",state="<%= state_name %>"} <%= state_count %> <% end current_stats = service_info[:stats] %>consul_service_stats_requests_total{service="<%= service_name %>",type="success"} <%= current_stats.successes %> <% if current_stats.errors > 0 %>consul_service_stats_requests_total{service="<%= service_name %>",type="errors"} <%= current_stats.errors %> <% end %>consul_service_stats_requests_bytes{service="<%= service_name %>"} <%= service_info[:stats].body_bytes %> consul_service_stats_requests_bytes_per_sec{service="<%= service_name %>"} <%= service_info[:stats].bytes_per_sec(now) %> consul_service_changes_total{service="<%= service_name %>"} <%= service_info[:stats].changes %> <% end %>