From 4865675c22f1b94248cb6ed5d818a73ea7713270 Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Thu, 3 Aug 2023 12:37:56 +0300 Subject: [PATCH] cfg: allow to use array-only include/exclude (#466) * 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. --- CHANGELOG.md | 5 +++ Makefile | 2 +- doc/monitoring/api_reference.rst | 1 + metrics/tarantool.lua | 25 +++++++++---- test/enable_default_metrics_test.lua | 55 ++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ce80dcd..94ee47e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Makefile b/Makefile index ce76f81f..adbdbc12 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/doc/monitoring/api_reference.rst b/doc/monitoring/api_reference.rst index 2da1f9c4..2f1e57b4 100644 --- a/doc/monitoring/api_reference.rst +++ b/doc/monitoring/api_reference.rst @@ -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`` diff --git a/metrics/tarantool.lua b/metrics/tarantool.lua index 45820fba..5c64bbad 100644 --- a/metrics/tarantool.lua +++ b/metrics/tarantool.lua @@ -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 @@ -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 = {} @@ -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 diff --git a/test/enable_default_metrics_test.lua b/test/enable_default_metrics_test.lua index 22e66935..8951584a 100644 --- a/test/enable_default_metrics_test.lua +++ b/test/enable_default_metrics_test.lua @@ -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 = {