Skip to content

Commit

Permalink
cfg: allow to use array-only include/exclude (#466)
Browse files Browse the repository at this point in the history
* cfg: allow to use array-only include/exclude

Before this patch, a user needed to set either string or array table
value to enable or disable default metrics. Tarantool 3 configuration
prototype prefers more strict type system, so it was proposed to use
`include={'all'}` instead of `include='all'` and `exclude={'all'}`
instead of `include='none'` [1]. This patch doesn't yet forbid using
existing API and not deprecates it. Defaults had also remained
unchanged for now.

1. https://www.notion.so/tarantool/metrics-9fa1b8843b2341848db040f2558d8fab

* make: rebuild if module contents had changed

Before this patch, if module files had been updated, .rocks had not been
rebuilt. If being used with Tarantool 2.11 or newer (which supports
rocks override), it resulted in tests running with old version of the
package.
  • Loading branch information
DifferentialOrange authored Aug 3, 2023
1 parent 72d00da commit 4865675
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `metrics.cfg{}` `"all"` metasection for array `include` and `exclude`
(`metrics.cfg{include={'all'}}` can be used instead of `metrics.cfg{include='all'}`,
`metrics.cfg{exclude={'all'}}` can be used instead of `metrics.cfg{include='none'}`)

## [1.0.0] - 2023-05-22
### Changed

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ endif
rpm:
OS=el DIST=7 packpack/packpack

.rocks: metrics-scm-1.rockspec
.rocks: metrics-scm-1.rockspec metrics/*.lua metrics/*/*.lua
$(TTCTL) rocks make
$(TTCTL) rocks install luatest # master newer than 0.5.7 required
$(TTCTL) rocks install luacov 0.13.0
Expand Down
1 change: 1 addition & 0 deletions doc/monitoring/api_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ Metrics functions

Supported default metric names (for ``cfg.include`` and ``cfg.exclude`` tables):

* ``all`` (metasection including all metrics)
* ``network``
* ``operations``
* ``system``
Expand Down
25 changes: 18 additions & 7 deletions metrics/tarantool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ local default_metrics = {
event_loop = require('metrics.tarantool.event_loop'),
}

local all_metrics_map = {}
for name, _ in pairs(default_metrics) do
all_metrics_map[name] = true
end

local function check_metrics_name(name, raise_if_unknown)
if default_metrics[name] == nil then
if raise_if_unknown then
Expand All @@ -42,13 +47,15 @@ local function enable_impl(include, exclude, raise_if_unknown)
local include_map = {}

if include == const.ALL then
for name, _ in pairs(default_metrics) do
include_map[name] = true
end
include_map = table.deepcopy(all_metrics_map)
elseif type(include) == 'table' then
for _, name in pairs(include) do
check_metrics_name(name, raise_if_unknown)
include_map[name] = true
if name == const.ALL then -- metasection "all"
include_map = table.deepcopy(all_metrics_map)
else
check_metrics_name(name, raise_if_unknown)
include_map[name] = true
end
end
elseif include == const.NONE then
include_map = {}
Expand All @@ -57,8 +64,12 @@ local function enable_impl(include, exclude, raise_if_unknown)
end

for _, name in pairs(exclude) do
check_metrics_name(name, raise_if_unknown)
include_map[name] = false
if name == const.ALL then -- metasection "all"
include_map = {}
else
check_metrics_name(name, raise_if_unknown)
include_map[name] = false
end
end

for name, value in pairs(default_metrics) do
Expand Down
55 changes: 55 additions & 0 deletions test/enable_default_metrics_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,61 @@ local cases = {
'tnt_info_memory_lua',
},
},
tt3_cfg_include_all = {
include = { 'all' },
exclude = nil,
expected = {
'tnt_info_uptime', 'tnt_info_memory_lua',
'tnt_net_sent_total', 'tnt_slab_arena_used',
},
not_expected = {},
},
tt3_cfg_exclude_all = {
include = nil,
exclude = { 'all' },
expected = {},
not_expected = {
'tnt_info_uptime', 'tnt_info_memory_lua',
'tnt_net_sent_total', 'tnt_slab_arena_used',
},
},
tt3_cfg_exclude_from_include_all = {
include = { 'all' },
exclude = { 'memory' },
expected = {
'tnt_info_uptime', 'tnt_net_sent_total', 'tnt_slab_arena_used',
},
not_expected = {
'tnt_info_memory_lua',
},
},
tt3_cfg_include_some_exclude_all = {
include = { 'memory' },
exclude = { 'all' },
expected = {},
not_expected = {
'tnt_info_uptime', 'tnt_info_memory_lua',
'tnt_net_sent_total', 'tnt_slab_arena_used',
},
},
tt3_cfg_include_exclude_all = {
include = { 'all' },
exclude = { 'all' },
expected = {},
not_expected = {
'tnt_info_uptime', 'tnt_info_memory_lua',
'tnt_net_sent_total', 'tnt_slab_arena_used',
},
},
tt3_cfg_include_all_and_specific = {
include = { 'memory', 'all' },
exclude = nil,
expected = {
'tnt_info_uptime', 'tnt_info_memory_lua',
'tnt_net_sent_total', 'tnt_slab_arena_used',
},
not_expected = {},
},
}

local methods = {
Expand Down

0 comments on commit 4865675

Please sign in to comment.