Skip to content

Commit

Permalink
internal: collect extended on json export
Browse files Browse the repository at this point in the history
After this patch, the only metrics method that plugin export uses
is metrics.collect{invoke_callbacks = true, extended_format = true}.
Process API is also separated, so now a developer can reuse export
handles if he already has an output (for example, processing historical
data in Flight Recorder).

Older version was preserved for test purposes to ensure that output is
the same in new implementation.

Part of tarantool/tarantool#7725
Part of tarantool/tarantool#7728
  • Loading branch information
DifferentialOrange committed Feb 15, 2023
1 parent ecd78b0 commit a6d1cbb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
30 changes: 29 additions & 1 deletion metrics/plugins/json.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local metrics = require('metrics')
local string_utils = require('metrics.string_utils')
local json = require('json')
local json_exporter = {}

Expand Down Expand Up @@ -38,7 +39,8 @@ local function format_observation(obs)
return part
end

function json_exporter.export()
json_exporter.internal = {}
function json_exporter.internal.collect_and_serialize_v1()
metrics.invoke_callbacks()
local stat = {}

Expand All @@ -51,4 +53,30 @@ function json_exporter.export()
return json.encode(stat)
end

function json_exporter.format_output(output)
local result = {}
for _, coll_obs in pairs(output) do
for group_name, obs_group in pairs(coll_obs.observations) do
local metric_name = string_utils.build_name(coll_obs.name, group_name)
for _, obs in pairs(obs_group) do
table.insert(result, {
metric_name = metric_name,
label_pairs = format_label_pairs(obs.label_pairs),
timestamp = coll_obs.timestamp,
value = format_value(obs.value),
})
end
end
end

return json.encode(result)
end

function json_exporter.internal.collect_and_serialize_v2()
local output = metrics.collect{invoke_callbacks = true, extended_format = true}
return json_exporter.format_output(output)
end

json_exporter.export = json_exporter.internal.collect_and_serialize_v2

return json_exporter
24 changes: 24 additions & 0 deletions test/plugins/json_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,27 @@ g.test_number64_ull_value_parses_to_json_number = function()
t.assert_not_equals(type(obs_ull.value), 'string', 'number64 is not casted to string on export')
t.assert_equals(obs_ull.value, 9007199254740992ULL, 'number64 ULL parsed to corrent number value')
end

g.test_collect_and_serialize_preserves_format = function()
-- Prepare some data for all collector types.
metrics.cfg{include = 'all', exclude = {}, labels = {alias = 'router-3'}}

local c = metrics.counter('cnt', nil, {my_useful_info = 'here'})
c:inc(3, {mylabel = 'myvalue1'})
c:inc(2, {mylabel = 'myvalue2'})

c = metrics.gauge('gauge', nil, {my_useful_info = 'here'})
c:set(3, {mylabel = 'myvalue1'})
c:set(2, {mylabel = 'myvalue2'})

c = metrics.histogram('histogram', nil, {2, 4}, {my_useful_info = 'here'})
c:observe(3, {mylabel = 'myvalue1'})
c:observe(2, {mylabel = 'myvalue2'})

local output_v1 = json.decode(json_exporter.internal.collect_and_serialize_v1())
local output_v2 = json.decode(json_exporter.internal.collect_and_serialize_v2())

for _, obs in ipairs(output_v1) do
utils.find_obs(obs.metric_name, obs.label_pairs, output_v2)
end
end

0 comments on commit a6d1cbb

Please sign in to comment.