forked from criteo/consul-templaterb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.erb
95 lines (85 loc) · 3.83 KB
/
metrics.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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 regexps to exclude (example: lbl7.*,netsvc-probe.*,consul-probed.*)
<%
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_raw = (ENV['EXCLUDE_SERVICES'] || 'lbl7.*,netsvc-probe.*,consul-probed.*').split(',')
services_blacklist = services_blacklist_raw.map { |v| Regexp.new(v) }
backends = {}
service_count = 0
services(tag: service_tag_filter).each do |service_name, tags|
if !services_blacklist.any? {|r| r.match(service_name)} && (instance_must_tag.nil? || tags.include?(instance_must_tag))
service_count += 1
res = {
'passing' => 0,
'warning' => 0,
'critical' => 0,
}
srv = service(service_name)
srv.each do |snode|
state = snode.status
res[state] += 1
end
backends[service_name] = {
state: res,
stats: srv.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 %>
# HELP consul_nodes_count A gauge of number of nodes in the cluster
# TYPE consul_nodes_count gauge
consul_nodes_count <%= nodes().count %>
# HELP consul_services_count A gauge of the number of services
# TYPE consul_services_count gauge
consul_services_count <%= service_count %>
<%
%i[success errors bytes_read changes network_bytes].each do |sym|
%>
# HELP consul_net_info_<%= sym.to_s %> consul-templaterb global stats for <%= sym.to_s %>
# TYPE consul_net_info_<%= sym.to_s %> counter
consul_net_info_<%= sym.to_s %> <%= @net_info[sym] %>
<%
end
%>
# 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
%>