Skip to content

Commit

Permalink
Refactor metrics.utils and change type of luajit metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
yngvar-antonsson committed Aug 25, 2021
1 parent bdc569f commit 2b31488
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 51 deletions.
73 changes: 29 additions & 44 deletions metrics/tarantool/luajit.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -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 {
Expand Down
19 changes: 12 additions & 7 deletions metrics/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
43 changes: 43 additions & 0 deletions test/utils_test.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2b31488

Please sign in to comment.