Skip to content

Commit fa39a05

Browse files
Refactor metrics.utils and change type of luajit metrics (#291)
1 parent df8e7d4 commit fa39a05

File tree

3 files changed

+85
-51
lines changed

3 files changed

+85
-51
lines changed

metrics/tarantool/luajit.lua

+30-44
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
local metrics = require('metrics')
2-
3-
local Shared = require('metrics.collectors.shared')
4-
51
local has_mics_module, misc = pcall(require, 'misc')
62

73
local LJ_PREFIX = 'lj_'
84

9-
local function prefix_name(name)
10-
return LJ_PREFIX .. name
11-
end
12-
13-
local function set_gauge(name, description, value, labels)
14-
local gauge = metrics.gauge(prefix_name(name), description)
15-
gauge:set(value, labels or {})
16-
return gauge
17-
end
18-
19-
local function set_counter(name, description, value, labels)
20-
local counter = metrics.counter(prefix_name(name), description)
21-
if counter.set == nil then
22-
counter.set = Shared.set
23-
end
24-
counter:set(value, labels or {})
25-
return counter
26-
end
5+
local utils = require('metrics.utils')
276

287
local collectors_list = {}
298

@@ -34,46 +13,53 @@ local function update()
3413
-- Details: https://github.com/tarantool/doc/issues/1597
3514
local lj_metrics = misc.getmetrics()
3615
collectors_list.gc_freed =
37-
set_counter('gc_freed', 'Total amount of freed memory', lj_metrics.gc_freed)
16+
utils.set_counter('gc_freed', 'Total amount of freed memory', lj_metrics.gc_freed, nil, LJ_PREFIX)
3817
collectors_list.strhash_hit =
39-
set_counter('strhash_hit', 'Number of strings being interned', lj_metrics.strhash_hit)
18+
utils.set_counter('strhash_hit', 'Number of strings being interned', lj_metrics.strhash_hit, nil, LJ_PREFIX)
4019
collectors_list.gc_steps_atomic =
41-
set_counter('gc_steps_atomic', 'Count of incremental GC steps (atomic state)', lj_metrics.gc_steps_atomic)
20+
utils.set_counter('gc_steps_atomic', 'Count of incremental GC steps (atomic state)',
21+
lj_metrics.gc_steps_atomic, nil, LJ_PREFIX)
4222
collectors_list.strhash_miss =
43-
set_counter('strhash_miss', 'Total number of strings allocations during the platform lifetime',
44-
lj_metrics.strhash_miss)
23+
utils.set_counter('strhash_miss', 'Total number of strings allocations during the platform lifetime',
24+
lj_metrics.strhash_miss, nil, LJ_PREFIX)
4525
collectors_list.gc_steps_sweepstring =
46-
set_counter('gc_steps_sweepstring', 'Count of incremental GC steps (sweepstring state)',
47-
lj_metrics.gc_steps_sweepstring)
26+
utils.set_counter('gc_steps_sweepstring', 'Count of incremental GC steps (sweepstring state)',
27+
lj_metrics.gc_steps_sweepstring, nil, LJ_PREFIX)
4828
collectors_list.gc_strnum =
49-
set_gauge('gc_strnum', 'Amount of allocated string objects', lj_metrics.gc_strnum)
29+
utils.set_gauge('gc_strnum', 'Amount of allocated string objects', lj_metrics.gc_strnum, nil, LJ_PREFIX)
5030
collectors_list.gc_tabnum =
51-
set_gauge('gc_tabnum', 'Amount of allocated table objects', lj_metrics.gc_tabnum)
31+
utils.set_gauge('gc_tabnum', 'Amount of allocated table objects', lj_metrics.gc_tabnum, nil, LJ_PREFIX)
5232
collectors_list.gc_cdatanum =
53-
set_gauge('gc_cdatanum', 'Amount of allocated cdata objects', lj_metrics.gc_cdatanum)
33+
utils.set_gauge('gc_cdatanum', 'Amount of allocated cdata objects', lj_metrics.gc_cdatanum, nil, LJ_PREFIX)
5434
collectors_list.jit_snap_restore =
55-
set_counter('jit_snap_restore', 'Overall number of snap restores', lj_metrics.jit_snap_restore)
35+
utils.set_counter('jit_snap_restore', 'Overall number of snap restores',
36+
lj_metrics.jit_snap_restore, nil, LJ_PREFIX)
5637
collectors_list.gc_total =
57-
set_gauge('gc_total', 'Memory currently allocated', lj_metrics.gc_total)
38+
utils.set_gauge('gc_total', 'Memory currently allocated', lj_metrics.gc_total, nil, LJ_PREFIX)
5839
collectors_list.gc_udatanum =
59-
set_gauge('gc_udatanum', 'Amount of allocated udata objects', lj_metrics.gc_udatanum)
40+
utils.set_gauge('gc_udatanum', 'Amount of allocated udata objects', lj_metrics.gc_udatanum, nil, LJ_PREFIX)
6041
collectors_list.gc_steps_finalize =
61-
set_counter('gc_steps_finalize', 'Count of incremental GC steps (finalize state)', lj_metrics.gc_steps_finalize)
42+
utils.set_counter('gc_steps_finalize', 'Count of incremental GC steps (finalize state)',
43+
lj_metrics.gc_steps_finalize, nil, LJ_PREFIX)
6244
collectors_list.gc_allocated =
63-
set_counter('gc_allocated', 'Total amount of allocated memory', lj_metrics.gc_allocated)
45+
utils.set_counter('gc_allocated', 'Total amount of allocated memory', lj_metrics.gc_allocated, nil, LJ_PREFIX)
6446
collectors_list.jit_trace_num =
65-
set_gauge('jit_trace_num', 'Amount of JIT traces', lj_metrics.jit_trace_num)
47+
utils.set_gauge('jit_trace_num', 'Amount of JIT traces', lj_metrics.jit_trace_num, nil, LJ_PREFIX)
6648
collectors_list.gc_steps_sweep =
67-
set_counter('gc_steps_sweep', 'Count of incremental GC steps (sweep state)', lj_metrics.gc_steps_sweep)
49+
utils.set_counter('gc_steps_sweep', 'Count of incremental GC steps (sweep state)',
50+
lj_metrics.gc_steps_sweep, nil, LJ_PREFIX)
6851
collectors_list.jit_trace_abort =
69-
set_counter('jit_trace_abort', 'Overall number of abort traces', lj_metrics.jit_trace_abort)
52+
utils.set_counter('jit_trace_abort', 'Overall number of abort traces',
53+
lj_metrics.jit_trace_abort, nil, LJ_PREFIX)
7054
collectors_list.jit_mcode_size =
71-
set_gauge('jit_mcode_size', 'Total size of all allocated machine code areas', lj_metrics.jit_mcode_size)
55+
utils.set_gauge('jit_mcode_size', 'Total size of all allocated machine code areas',
56+
lj_metrics.jit_mcode_size, nil, LJ_PREFIX)
7257
collectors_list.gc_steps_propagate =
73-
set_counter('gc_steps_propagate', 'Count of incremental GC steps (propagate state)',
74-
lj_metrics.gc_steps_propagate)
58+
utils.set_counter('gc_steps_propagate', 'Count of incremental GC steps (propagate state)',
59+
lj_metrics.gc_steps_propagate, nil, LJ_PREFIX)
7560
collectors_list.gc_steps_pause =
76-
set_counter('gc_steps_pause', 'Count of incremental GC steps (pause state)', lj_metrics.gc_steps_pause)
61+
utils.set_counter('gc_steps_pause', 'Count of incremental GC steps (pause state)',
62+
lj_metrics.gc_steps_pause, nil, LJ_PREFIX)
7763
end
7864

7965
return {

metrics/utils.lua

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@ local metrics = require('metrics')
22

33
local TNT_PREFIX = 'tnt_'
44

5-
local function prefix_name(name)
6-
return TNT_PREFIX .. name
7-
end
8-
9-
local function set_gauge(name, description, value, labels)
10-
local gauge = metrics.gauge(prefix_name(name), description)
5+
local function set_gauge(name, description, value, labels, prefix)
6+
prefix = prefix or TNT_PREFIX
7+
local gauge = metrics.gauge(prefix .. name, description)
118
gauge:set(value, labels or {})
129
return gauge
1310
end
1411

12+
local function set_counter(name, description, value, labels, prefix)
13+
prefix = prefix or TNT_PREFIX
14+
local counter = metrics.counter(prefix .. name, description)
15+
counter:reset(labels or {})
16+
counter:inc(value, labels or {})
17+
return counter
18+
end
19+
1520
local function box_is_configured()
1621
return type(box.cfg) ~= 'function'
1722
end
1823

1924
return {
2025
set_gauge = set_gauge,
26+
set_counter = set_counter,
2127
box_is_configured = box_is_configured,
22-
prefix_name = prefix_name,
2328
}

test/utils_test.lua

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
local t = require('luatest')
2+
local g = t.group()
3+
4+
local utils = require('metrics.utils')
5+
6+
g.test_set_gauge = function()
7+
local gauge = utils.set_gauge('gauge', 'gauge info', 10)
8+
9+
t.assert_equals(gauge.name, 'tnt_gauge')
10+
t.assert_equals(gauge.help, 'gauge info')
11+
t.assert_equals(gauge.observations[''], 10)
12+
end
13+
14+
g.test_set_counter = function()
15+
local counter = utils.set_counter('counter', 'counter info', 10)
16+
17+
t.assert_equals(counter.name, 'tnt_counter')
18+
t.assert_equals(counter.help, 'counter info')
19+
t.assert_equals(counter.observations[''], 10)
20+
21+
utils.set_counter('counter', 'counter info', 20)
22+
t.assert_equals(counter.observations[''], 20)
23+
end
24+
25+
g.test_set_gauge_prefix = function()
26+
local gauge = utils.set_gauge('gauge', 'gauge info', 10, nil, 'custom_')
27+
28+
t.assert_equals(gauge.name, 'custom_gauge')
29+
t.assert_equals(gauge.help, 'gauge info')
30+
t.assert_equals(gauge.observations[''], 10)
31+
end
32+
33+
g.test_set_counter_prefix = function()
34+
local counter = utils.set_counter('counter', 'counter info', 10, nil, 'custom_')
35+
36+
t.assert_equals(counter.name, 'custom_counter')
37+
t.assert_equals(counter.help, 'counter info')
38+
t.assert_equals(counter.observations[''], 10)
39+
40+
utils.set_counter('counter', 'counter info', 20, nil, 'custom_')
41+
t.assert_equals(counter.observations[''], 20)
42+
end
43+

0 commit comments

Comments
 (0)