Skip to content

Commit 442a2a2

Browse files
api: allow to collect only default metrics
After this patch, it is possible to collect only default metrics in Lua. Collector metainfo is used to decide which metric is default and which is not. Beware that collect does not control the list of enabled metrics -- if some (or all) default metrics were disabled or weren't enabled, they won't be appear in observation. The motivation is a requirement from Tarantool Feedback Daemon [1] to collect default metrics, but not the user ones. 1. tarantool/tarantool#8192 Part of tarantool/tarantool#8192, part of tarantool/tarantool#7725
1 parent 2b2cc85 commit 442a2a2

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Ability to set metainfo for collectors
1414
- Set `metainfo.default` to `true` for all collectors
1515
from `enable_default_metrics()` and psutils collectors
16+
- `default_only` option for `metrics.collect()`
1617

1718
### Fixed
1819

doc/monitoring/api_reference.rst

+1
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ Metrics functions
360360
:param table opts: table of collect options:
361361

362362
* ``invoke_callbacks`` -- if ``true``, ``invoke_callbacks()`` is triggerred before actual collect.
363+
* ``default_only`` -- if ``true``, observations contain only default metrics (``metainfo.default = true``).
363364

364365
.. class:: registry
365366

metrics/init.lua

+19-2
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,32 @@ local function invoke_callbacks()
4141
return registry:invoke_callbacks()
4242
end
4343

44+
local function get_collector_values(collector, result)
45+
for _, obs in ipairs(collector:collect()) do
46+
table.insert(result, obs)
47+
end
48+
end
49+
4450
local function collect(opts)
45-
checks({invoke_callbacks = '?boolean'})
51+
checks({invoke_callbacks = '?boolean', default_only = '?boolean'})
4652
opts = opts or {}
4753

4854
if opts.invoke_callbacks then
4955
registry:invoke_callbacks()
5056
end
5157

52-
return registry:collect()
58+
local result = {}
59+
for _, collector in pairs(registry.collectors) do
60+
if opts.default_only then
61+
if collector.metainfo.default then
62+
get_collector_values(collector, result)
63+
end
64+
else
65+
get_collector_values(collector, result)
66+
end
67+
end
68+
69+
return result
5370
end
5471

5572
local function clear()

test/metrics_test.lua

+35
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,38 @@ g.test_default_metrics_metainfo = function()
199199
('default collector %s has metainfo label "default"'):format(k))
200200
end
201201
end
202+
203+
local collect_default_only_cases = {
204+
default = {
205+
args = {invoke_callbacks = true},
206+
custom_expected = true,
207+
},
208+
['true'] = {
209+
args = {invoke_callbacks = true, default_only = true},
210+
custom_expected = false,
211+
},
212+
['false'] = {
213+
args = {invoke_callbacks = true, default_only = false},
214+
custom_expected = true,
215+
},
216+
}
217+
218+
for name, case in pairs(collect_default_only_cases) do
219+
g['test_collect_default_only_' .. name] = function()
220+
metrics.enable_default_metrics()
221+
local c = metrics.gauge('custom_metric')
222+
c:set(42)
223+
224+
local observations = metrics.collect(case.args)
225+
226+
local default_obs = utils.find_metric('tnt_info_memory_lua', observations)
227+
t.assert_not_equals(default_obs, nil)
228+
229+
local custom_obs = utils.find_metric('custom_metric', observations)
230+
if case.custom_expected then
231+
t.assert_not_equals(custom_obs, nil)
232+
else
233+
t.assert_equals(custom_obs, nil)
234+
end
235+
end
236+
end

0 commit comments

Comments
 (0)