-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprometheus.lua
More file actions
152 lines (134 loc) · 5.13 KB
/
prometheus.lua
File metadata and controls
152 lines (134 loc) · 5.13 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
--[[
prom.translations table holds the mapping between the HAProxy internal names
for metrics (as returned by Proxy.get_stats() and Server.get_stats()
methods, see also
https://github.com/haproxy/haproxy/blob/master/doc/management.txt,
section "9.1 CSV Format") and the names we present to prometheus.
Final string for each metric is built by the following formula:
prom.prefix + variant + translation + optional labels + metric value.
Allowed variants: "frontend", "backend", "server", "listener".
See prom.build_metrics and prom.build_labels for additional details.
]]--
prom = {
prefix = "haproxy",
translations = {
bin = "bytes_in_total",
bout = "bytes_out_total",
econ = "connection_errors_total",
ereq = "request_errors_total",
eresp = "response_errors_total",
qcur = "current_queue",
qmax = "max_queue",
qlimit = "queue_limit",
scur = "current_sessions",
smax = "max_sessions",
slim = "limit_sessions",
stot = "total_sessions",
dreq = "requests_denied_total",
dresp = "responses_denied_total",
wretr = "retry_warnings_total",
wredis = "redispatch_warnings_total",
weight = "weight",
act = "active_servers",
bck = "backup_servers",
chkfail = "check_failures_total",
chkdown = "check_transitions_total",
lastchg = "last_transition_interval",
throttle = "throttle_percent",
downtime = "downtime_seconds_total",
lbtot = "selected_times_total",
rate = "current_session_rate",
rate_lim = "limit_sessions",
rate_max = "max_session_rate",
ctime = "connect_time_average",
rtime = "response_time_average",
qtime = "queue_time_average",
ttime = "session_time_average",
status = { "up", function(metric)
local _, regex = Regex.new("^(UP|no check).*", true)
if regex:exec(metric) then
return 1
end
return 0
end },
hrsp_1xx = { "http_responses_total", { code = "1xx" }},
hrsp_2xx = { "http_responses_total", { code = "2xx" }},
hrsp_3xx = { "http_responses_total", { code = "3xx" }},
hrsp_4xx = { "http_responses_total", { code = "4xx" }},
hrsp_5xx = { "http_responses_total", { code = "5xx" }},
hrsp_other = { "http_responses_total", { code = "other" }},
req_rate = "http_request_rate",
req_rate_max = "http_max_request_rate",
req_tot = "http_requests_total"
},
types = {
counter = "^[CM]",
gauge = "^[AaDGLRm]"
}
}
-- build_labels converts a table with labels into a string.
prom.build_labels = function(labels)
local l = core.concat()
for k,v in pairs(labels) do
l:add(k.."=\""..v.."\",")
end
local lstring = l:dump()
return string.sub(lstring,1,-2)
end
-- get_type converts internal HAProxy metric type into
-- either "counter" or "gauge" for prometheus.
prom.get_type = function(mtype)
if mtype:match(prom.types.counter) then
return "counter"
elseif mtype:match(prom.types.gauge) then
return "gauge"
end
return "untyped"
end
-- build_metrics returns rendered prometheus metrics for a particular
-- proxy variant: -- listener, frontend, backend, server.
-- We intentionally don't include "#HELP ..." strings for metrics in the
-- output: prometheus does not need them, human beings can read the docs,
-- and it's always good to save ourselves some traffic.
prom.build_metrics = function(prom, variant)
local response = core.concat()
for n, t in pairs(prom.translations) do
if stats.metrics[variant] and stats.metrics.has[variant][n] then
local translation = ""
local predefined_labels = {}
local translation_func
if type(t) == "table" then
translation = t[1]
if type(t[2]) == "function" then
translation_func = t[2]
else
for k,v in pairs(t[2]) do
predefined_labels[k] = v
end
end
else
translation = t
end
response:add("# TYPE "..prom.prefix.."_"..variant.."_"..translation.." "..prom.get_type(stats.metrics.has[variant][n]:sub(2,2)).."\n")
for proxy_id, metrics in pairs(stats.metrics[variant]) do
local labels = predefined_labels
if variant == "server" then
labels.addr = metrics.addr
labels.server = metrics.svname
labels.backend = metrics.pxname
else
labels[variant] = metrics.pxname
end
if metrics[n] then
local metric_value = metrics[n]
if translation_func then
metric_value = translation_func(metric_value)
end
response:add(prom.prefix.."_"..variant.."_"..translation.."{"..prom.build_labels(labels).."} "..metric_value.."\n")
end
end
end
end
return response
end
return prom