From 3bcebcae18b2ff547aadfb82cfcda74feaf6bf3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Molinero=20Fern=C3=A1ndez?= <hector@molinero.dev> Date: Wed, 9 Dec 2020 22:15:02 +0100 Subject: [PATCH] Transform Graphite tags into Prometheus labels --- modules/http/prometheus.lua | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/modules/http/prometheus.lua b/modules/http/prometheus.lua index 68b491da5..094c5f4ed 100644 --- a/modules/http/prometheus.lua +++ b/modules/http/prometheus.lua @@ -102,7 +102,29 @@ 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('%.', '_')) + -- Transform Graphite tags into Prometheus labels + -- See: https://gitlab.nic.cz/knot/knot-resolver/-/issues/650 + local k_index, k_len, k_tag = 0, #k, 0 + k = select(1, k:gsub('.', function (c) + k_index = k_index + 1 + if k_tag == 0 then + if c == '.' then return '_' end + if c == ';' then k_tag = 1; return '{' end + elseif k_tag == 1 then + if k_index == k_len then + if c == '=' then return '=""}' + else return c .. '"}' end + end + if c == '=' then k_tag = 2; return '="' end + elseif k_tag == 2 then + if k_index == k_len then + if c == ';' then return '"}' + else return c .. '"}' end + end + if c == ';' then k_tag = 1; return '",' end + end + return nil + end)) -- Aggregate histograms local band = k:match('answer_([%d]+)ms') if band then @@ -112,7 +134,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 metric, label = key:match('^([^{]+)(.*)$') + table.insert(render, string.format(counter, metric, metric .. label, v)) end end -- Fill in latency histogram -- GitLab