Skip to content
Snippets Groups Projects
Commit 776af506 authored by Tomas Krizek's avatar Tomas Krizek
Browse files

Merge branch 'prometheus-labels' into 'master'

Transform Graphite tags into Prometheus labels

Closes #650

See merge request !1109
parents 08e3991f 29dadd73
Branches
Tags
1 merge request!1109Transform Graphite tags into Prometheus labels
Pipeline #73488 passed with stage
in 33 seconds
......@@ -95,6 +95,35 @@ local function stream_stats(_, ws)
end
end
-- Transform metrics from Graphite to Prometheus format
-- See: https://gitlab.nic.cz/knot/knot-resolver/-/issues/650
-- E.g.:
-- worker.ipv4 -> worker_ipv4
-- answer.blocked;stype=A -> answer_blocked{stype="A"}
local function get_metric(key)
local key_index, key_len, key_tag = 0, #key, 0
return select(1, key:gsub('.', function (c)
key_index = key_index + 1
if key_tag == 0 then
if c == '.' then return '_' end
if c == ';' then key_tag = 1; return '{' end
elseif key_tag == 1 then
if key_index == key_len then
if c == '=' then return '=""}'
else return c .. '"}' end
end
if c == '=' then key_tag = 2; return '="' end
elseif key_tag == 2 then
if key_index == key_len then
if c == ';' then return '"}'
else return c .. '"}' end
end
if c == ';' then key_tag = 1; return '",' end
end
return nil
end))
end
-- Render stats in Prometheus text format
local function serve_prometheus()
-- First aggregate metrics list and print counters
......@@ -102,7 +131,7 @@ local function serve_prometheus()
local latency = {}
local counter = '# TYPE %s counter\n%s %f'
for k,v in pairs(slist) do
k = select(1, k:gsub('%.', '_'))
k = get_metric(k)
-- Aggregate histograms
local band = k:match('answer_([%d]+)ms')
if band then
......@@ -112,7 +141,8 @@ local function serve_prometheus()
-- Counter as a fallback
else
local key = M.namespace .. k
table.insert(render, string.format(counter, key, key, v))
local name, label = key:match('^([^{]+)(.*)$')
table.insert(render, string.format(counter, name, name .. label, v))
end
end
-- Fill in latency histogram
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment