-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
495 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
local buffer = require "string.buffer" | ||
local json_safe = require "cjson.safe" | ||
local wasm = require "kong.runloop.wasm" | ||
local wasmx_shm = require "resty.wasmx.shm" | ||
local pretty = require("pl.pretty").write | ||
|
||
|
||
local fmt = string.format | ||
local str_match = string.match | ||
local str_find = string.find | ||
local str_sub = string.sub | ||
local buf_new = buffer.new | ||
local ngx_say = ngx.say | ||
|
||
|
||
local _M = {} | ||
|
||
|
||
local function parse_key(key) | ||
-- [pw,lua,wa].namespace.metric_name_label1=v1_label2=v2 | ||
-- e.g. pw.filter_name.metric_name_label1=v1 | ||
-- e.g. lua.plugin_name.metric_name_label1=v1 | ||
-- e.g. wa.wasm_socket.metric_name_label1=v1 | ||
ngx.log(ngx.INFO, "attemtping to parse key: ", key) | ||
local name | ||
local labels = {} | ||
|
||
local header = { | ||
pw = "pw.", -- proxy-wasm metrics | ||
lua = "lua.", -- lua land metrics | ||
wa = "wa." -- internal ngx_wasm_module metrics | ||
} | ||
|
||
local is_pw = #key > #header.pw and key:sub(0, #header.pw) == header.pw | ||
local is_lua = #key > #header.lua and key:sub(0, #header.lua) == header.lua | ||
local is_wa = #key > #header.wa and key:sub(0, #header.wa) == header.wa | ||
|
||
local hs = is_lua and #header.lua or #header.pw -- wa's size == pw's size | ||
|
||
if is_lua or is_wa then | ||
name = key | ||
-- label support NYI | ||
|
||
elseif is_pw then | ||
local second_dot_pos, _ = str_find(key, "%.", hs + 1) | ||
local ns = str_sub(key, hs + 1, second_dot_pos - 1) | ||
|
||
local filter_config = json_safe.decode(wasm.filters_by_name[ns].config) | ||
local patterns = filter_config and filter_config.metrics.label_patterns | ||
|
||
local first_match = #key | ||
|
||
if patterns then | ||
for i, pair in ipairs(patterns) do | ||
local lkv, lv = str_match(key, pair.pattern) | ||
local lk = str_sub(lkv, 0, str_find(lkv, "=")) | ||
local lk_start, _ = str_find(key, lk) | ||
|
||
first_match = (lk_start < first_match) and lk_start or first_match | ||
|
||
labels[pair.label] = lv | ||
end | ||
|
||
name = str_sub(key, 0, first_match - 1) | ||
|
||
else | ||
name = key | ||
end | ||
else | ||
-- unreachable | ||
end | ||
|
||
return name, labels | ||
end | ||
|
||
|
||
local function serialize_labels(labels) | ||
local buf = buf_new() | ||
|
||
for l, v in pairs(labels) do | ||
buf:put(fmt('%s="%s",', l, v)) | ||
end | ||
|
||
local slabels = buf:get() | ||
|
||
if #slabels > 0 then | ||
return slabels:sub(0, #slabels - 1) -- discard trailing comma | ||
end | ||
|
||
return slabels | ||
end | ||
|
||
|
||
local function serialize_metric(m) | ||
local buf = buf_new() | ||
|
||
buf:put(fmt("# HELP %s\n# TYPE %s %s", m.name, m.name, m.type)) | ||
|
||
for labels, labeled_m in pairs(m.labels) do | ||
local slabels = serialize_labels(labels) | ||
|
||
if m.type == "counter" or m.type == "gauge" then | ||
if #slabels > 0 then | ||
buf:put(fmt("\n%s{%s} %s", m.name, slabels, labeled_m.value)) | ||
else | ||
buf:put(fmt("\n%s %s", m.name, labeled_m.value)) | ||
end | ||
|
||
elseif m.type == "histogram" then | ||
for _, bin in ipairs(labeled_m.value) do | ||
local ubl = fmt('le="%s"', bin.ub) | ||
|
||
slabels = (#slabels > 0) and (slabels .. "," .. ubl) or ubl | ||
|
||
buf:put(fmt("\n%s{%s} %s", m.name, slabels, bin.count)) | ||
end | ||
end | ||
end | ||
|
||
buf:put("\n") | ||
|
||
return buf:get() | ||
end | ||
|
||
|
||
_M.metric_data = function() | ||
local i = 0 | ||
local flush_after = 50 | ||
local metrics = {} | ||
local parsed = {} | ||
local buf = buf_new() | ||
|
||
wasmx_shm.metrics:lock() | ||
|
||
for key in wasmx_shm.metrics:iterate_keys() do | ||
metrics[key] = wasmx_shm.metrics:get_by_name(key, { prefix = false }) | ||
end | ||
|
||
wasmx_shm.metrics:unlock() | ||
|
||
for key, m in pairs(metrics) do | ||
m.name, m.labels = parse_key(key) | ||
|
||
parsed[m.name] = parsed[m.name] or { name = m.name, type = m.type, labels = {} } | ||
parsed[m.name].labels[m.labels] = m | ||
end | ||
|
||
for _, metric_by_label in pairs(parsed) do | ||
buf:put(serialize_metric(metric_by_label)) | ||
|
||
i = i + 1 | ||
|
||
if i % flush_after == 0 then | ||
ngx_say(buf:get()) | ||
end | ||
end | ||
|
||
ngx_say(buf:get()) | ||
end | ||
|
||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.