From 2b31488399004a78c58abf0bd04b003ba26ecd2d Mon Sep 17 00:00:00 2001 From: Igor Zolotarev Date: Tue, 24 Aug 2021 00:37:17 +0300 Subject: [PATCH] Refactor metrics.utils and change type of luajit metrics --- metrics/tarantool/luajit.lua | 73 ++++++++++++++---------------------- metrics/utils.lua | 19 ++++++---- test/utils_test.lua | 43 +++++++++++++++++++++ 3 files changed, 84 insertions(+), 51 deletions(-) create mode 100644 test/utils_test.lua diff --git a/metrics/tarantool/luajit.lua b/metrics/tarantool/luajit.lua index 0dc47a20..6cec3ae6 100644 --- a/metrics/tarantool/luajit.lua +++ b/metrics/tarantool/luajit.lua @@ -1,29 +1,8 @@ -local metrics = require('metrics') - -local Shared = require('metrics.collectors.shared') - local has_mics_module, misc = pcall(require, 'misc') local LJ_PREFIX = 'lj_' -local function prefix_name(name) - return LJ_PREFIX .. name -end - -local function set_gauge(name, description, value, labels) - local gauge = metrics.gauge(prefix_name(name), description) - gauge:set(value, labels or {}) - return gauge -end - -local function set_counter(name, description, value, labels) - local counter = metrics.counter(prefix_name(name), description) - if counter.set == nil then - counter.set = Shared.set - end - counter:set(value, labels or {}) - return counter -end +local utils = require('metrics.utils') local collectors_list = {} @@ -34,46 +13,52 @@ local function update() -- Details: https://github.com/tarantool/doc/issues/1597 local lj_metrics = misc.getmetrics() collectors_list.gc_freed = - set_counter('gc_freed', 'Total amount of freed memory', lj_metrics.gc_freed) + utils.set_counter('gc_freed', 'Total amount of freed memory', lj_metrics.gc_freed, nil, LJ_PREFIX) collectors_list.strhash_hit = - set_counter('strhash_hit', 'Number of strings being interned', lj_metrics.strhash_hit) + utils.set_counter('strhash_hit', 'Number of strings being interned', lj_metrics.strhash_hit, nil, LJ_PREFIX) collectors_list.gc_steps_atomic = - set_counter('gc_steps_atomic', 'Count of incremental GC steps (atomic state)', lj_metrics.gc_steps_atomic) + utils.set_counter('gc_steps_atomic', 'Count of incremental GC steps (atomic state)', + lj_metrics.gc_steps_atomic, nil, LJ_PREFIX) collectors_list.strhash_miss = - set_counter('strhash_miss', 'Total number of strings allocations during the platform lifetime', - lj_metrics.strhash_miss) + utils.set_counter('strhash_miss', 'Total number of strings allocations during the platform lifetime', + lj_metrics.strhash_miss, nil, LJ_PREFIX) collectors_list.gc_steps_sweepstring = - set_counter('gc_steps_sweepstring', 'Count of incremental GC steps (sweepstring state)', - lj_metrics.gc_steps_sweepstring) + utils.set_counter('gc_steps_sweepstring', 'Count of incremental GC steps (sweepstring state)', + lj_metrics.gc_steps_sweepstring, nil, LJ_PREFIX) collectors_list.gc_strnum = - set_gauge('gc_strnum', 'Amount of allocated string objects', lj_metrics.gc_strnum) + utils.set_gauge('gc_strnum', 'Amount of allocated string objects', lj_metrics.gc_strnum, nil, LJ_PREFIX) collectors_list.gc_tabnum = - set_gauge('gc_tabnum', 'Amount of allocated table objects', lj_metrics.gc_tabnum) + utils.set_gauge('gc_tabnum', 'Amount of allocated table objects', lj_metrics.gc_tabnum, nil, LJ_PREFIX) collectors_list.gc_cdatanum = - set_gauge('gc_cdatanum', 'Amount of allocated cdata objects', lj_metrics.gc_cdatanum) + utils.set_gauge('gc_cdatanum', 'Amount of allocated cdata objects', lj_metrics.gc_cdatanum, nil, LJ_PREFIX) collectors_list.jit_snap_restore = - set_counter('jit_snap_restore', 'Overall number of snap restores', lj_metrics.jit_snap_restore) + utils.set_counter('jit_snap_restore', 'Overall number of snap restores', + lj_metrics.jit_snap_restore, nil, LJ_PREFIX) collectors_list.gc_total = - set_gauge('gc_total', 'Memory currently allocated', lj_metrics.gc_total) + utils.set_gauge('gc_total', 'Memory currently allocated', lj_metrics.gc_total, nil, LJ_PREFIX) collectors_list.gc_udatanum = - set_gauge('gc_udatanum', 'Amount of allocated udata objects', lj_metrics.gc_udatanum) + utils.set_gauge('gc_udatanum', 'Amount of allocated udata objects', lj_metrics.gc_udatanum, nil, LJ_PREFIX) collectors_list.gc_steps_finalize = - set_counter('gc_steps_finalize', 'Count of incremental GC steps (finalize state)', lj_metrics.gc_steps_finalize) + utils.set_counter('gc_steps_finalize', 'Count of incremental GC steps (finalize state)', + lj_metrics.gc_steps_finalize, nil, LJ_PREFIX) collectors_list.gc_allocated = - set_counter('gc_allocated', 'Total amount of allocated memory', lj_metrics.gc_allocated) + utils.set_counter('gc_allocated', 'Total amount of allocated memory', lj_metrics.gc_allocated, nil, LJ_PREFIX) collectors_list.jit_trace_num = - set_gauge('jit_trace_num', 'Amount of JIT traces', lj_metrics.jit_trace_num) + utils.set_gauge('jit_trace_num', 'Amount of JIT traces', lj_metrics.jit_trace_num, nil, LJ_PREFIX) collectors_list.gc_steps_sweep = - set_counter('gc_steps_sweep', 'Count of incremental GC steps (sweep state)', lj_metrics.gc_steps_sweep) + utils.set_counter('gc_steps_sweep', 'Count of incremental GC steps (sweep state)', + lj_metrics.gc_steps_sweep, nil, LJ_PREFIX) collectors_list.jit_trace_abort = - set_counter('jit_trace_abort', 'Overall number of abort traces', lj_metrics.jit_trace_abort) + utils.set_counter('jit_trace_abort', 'Overall number of abort traces', lj_metrics.jit_trace_abort, nil, LJ_PREFIX) collectors_list.jit_mcode_size = - set_gauge('jit_mcode_size', 'Total size of all allocated machine code areas', lj_metrics.jit_mcode_size) + utils.set_gauge('jit_mcode_size', 'Total size of all allocated machine code areas', + lj_metrics.jit_mcode_size, nil, LJ_PREFIX) collectors_list.gc_steps_propagate = - set_counter('gc_steps_propagate', 'Count of incremental GC steps (propagate state)', - lj_metrics.gc_steps_propagate) + utils.set_counter('gc_steps_propagate', 'Count of incremental GC steps (propagate state)', + lj_metrics.gc_steps_propagate, nil, LJ_PREFIX) collectors_list.gc_steps_pause = - set_counter('gc_steps_pause', 'Count of incremental GC steps (pause state)', lj_metrics.gc_steps_pause) + utils.set_counter('gc_steps_pause', 'Count of incremental GC steps (pause state)', + lj_metrics.gc_steps_pause, nil, LJ_PREFIX) end return { diff --git a/metrics/utils.lua b/metrics/utils.lua index bf305d43..be463ec0 100644 --- a/metrics/utils.lua +++ b/metrics/utils.lua @@ -2,22 +2,27 @@ local metrics = require('metrics') local TNT_PREFIX = 'tnt_' -local function prefix_name(name) - return TNT_PREFIX .. name -end - -local function set_gauge(name, description, value, labels) - local gauge = metrics.gauge(prefix_name(name), description) +local function set_gauge(name, description, value, labels, prefix) + prefix = prefix or TNT_PREFIX + local gauge = metrics.gauge(prefix .. name, description) gauge:set(value, labels or {}) return gauge end +local function set_counter(name, description, value, labels, prefix) + prefix = prefix or TNT_PREFIX + local counter = metrics.counter(prefix .. name, description) + counter:reset(labels or {}) + counter:inc(value, labels or {}) + return counter +end + local function box_is_configured() return type(box.cfg) ~= 'function' end return { set_gauge = set_gauge, + set_counter = set_counter, box_is_configured = box_is_configured, - prefix_name = prefix_name, } diff --git a/test/utils_test.lua b/test/utils_test.lua new file mode 100644 index 00000000..d98b6494 --- /dev/null +++ b/test/utils_test.lua @@ -0,0 +1,43 @@ +local t = require('luatest') +local g = t.group() + +local utils = require('metrics.utils') + +g.test_set_gauge = function() + local gauge = utils.set_gauge('gauge', 'gauge info', 10) + + t.assert_equals(gauge.name, 'tnt_gauge') + t.assert_equals(gauge.help, 'gauge info') + t.assert_equals(gauge.observations[''], 10) +end + +g.test_set_counter = function() + local counter = utils.set_counter('counter', 'counter info', 10) + + t.assert_equals(counter.name, 'tnt_counter') + t.assert_equals(counter.help, 'counter info') + t.assert_equals(counter.observations[''], 10) + + utils.set_counter('counter', 'counter info', 20) + t.assert_equals(counter.observations[''], 20) +end + +g.test_set_gauge_prefix = function() + local gauge = utils.set_gauge('gauge', 'gauge info', 10, nil, 'custom_') + + t.assert_equals(gauge.name, 'custom_gauge') + t.assert_equals(gauge.help, 'gauge info') + t.assert_equals(gauge.observations[''], 10) +end + +g.test_set_counter_prefix = function() + local counter = utils.set_counter('counter', 'counter info', 10, nil, 'custom_') + + t.assert_equals(counter.name, 'custom_counter') + t.assert_equals(counter.help, 'counter info') + t.assert_equals(counter.observations[''], 10) + + utils.set_counter('counter', 'counter info', 20, nil, 'custom_') + t.assert_equals(counter.observations[''], 20) +end +