Skip to content

Commit

Permalink
box: configure metrics with box.cfg
Browse files Browse the repository at this point in the history
metrics.cfg is a module configuration entrypoint [1]. After this patch,
box.cfg{metrics = cfg} triggers metrics.cfg. Parameter is dynamic.

This patch also enables all metrics by default. To disable them, use
box.cfg{metrics = {include = 'none'}}. Since callbacks are triggered
manually (when user calls `invoke_callbacks()` or
`collect{invoke_callbacks=true}`), it should not affect
the performance.

1. https://www.tarantool.io/en/doc/latest/book/monitoring/api_reference/#lua-function.metrics.cfg

Closes #7725

@TarantoolBot document
Title: embedded metrics configuration

This patch add new box.cfg dynamic parameter: metrics. It is a table
with the same effect as in metrics.cfg. metrics.cfg has its own
documentation, see [1].

1. https://www.tarantool.io/en/doc/latest/book/monitoring/api_reference/#lua-function.metrics.cfg
  • Loading branch information
DifferentialOrange authored and LeonidVas committed Mar 24, 2023
1 parent 82ebbb3 commit 8f95eaa
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelogs/unreleased/gh-7725-embed-metrics.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
## feature/lua

* Embed tarantool/metrics module for metrics aggregation and export (gh-7725).
* Configure metrics with box.cfg (gh-7725).
14 changes: 13 additions & 1 deletion src/box/lua/load_cfg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ local default_cfg = {
sql_cache_size = 5 * 1024 * 1024,
txn_timeout = 365 * 100 * 86400,
txn_isolation = "best-effort",

metrics = {
include = 'all',
exclude = {},
labels = {},
},
}

-- We need to track cfg changes done through API of distinct modules (log.cfg of
Expand Down Expand Up @@ -377,6 +383,8 @@ local template_cfg = {
net_msg_max = 'number',
sql_cache_size = 'number',
txn_timeout = 'number',

metrics = 'table',
}

local function normalize_uri_list_for_replication(port_list)
Expand Down Expand Up @@ -492,7 +500,11 @@ local dynamic_cfg = {
password_enforce_digits = ifdef_security(nop),
password_enforce_specialchars = ifdef_security(nop),
password_history_length = ifdef_security(nop),
wal_ext = private.cfg_set_wal_ext
wal_ext = private.cfg_set_wal_ext,

metrics = function()
require('metrics').cfg(box.cfg.metrics)
end,
}

-- The modules that can apply all new options with single call. The
Expand Down
1 change: 1 addition & 0 deletions test/box-luatest/gh_8192_feedback_daemon_metrics_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ g.before_all(function()
g.server = server:new{
alias = 'default',
box_cfg = {
metrics = {include = 'none'},
-- The tests here are unit ones, works with dummy metrics
-- package. After metrics package embedding, feedback background
-- loop may succeed to extract empty metrics table from
Expand Down
7 changes: 7 additions & 0 deletions test/box/admin.result
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ cfg_filter(box.cfg)
- <hidden>
- - memtx_use_mvcc_engine
- false
- - metrics
- - - exclude
- []
- - include
- all
- - labels
- []
- - net_msg_max
- 768
- - pid_file
Expand Down
14 changes: 14 additions & 0 deletions test/box/cfg.result
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ cfg_filter(box.cfg)
| - <hidden>
| - - memtx_use_mvcc_engine
| - false
| - - metrics
| - - - exclude
| - []
| - - include
| - all
| - - labels
| - []
| - - net_msg_max
| - 768
| - - pid_file
Expand Down Expand Up @@ -220,6 +227,13 @@ cfg_filter(box.cfg)
| - <hidden>
| - - memtx_use_mvcc_engine
| - false
| - - metrics
| - - - exclude
| - []
| - - include
| - all
| - - labels
| - []
| - - net_msg_max
| - 768
| - - pid_file
Expand Down
124 changes: 124 additions & 0 deletions test/metrics-luatest/box_cfg_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
require('test.metrics-luatest.helper')

local t = require('luatest')
local g = t.group('box-cfg-metrics')
local server = require('luatest.server')

local utils = require('third_party.metrics.test.utils')

g.before_all(function()
g.server = server:new()
g.server:start()
end)

g.after_all(function()
g.server:drop()
end)

g.before_each(function()
g.server:exec(function()
box.cfg{
metrics = {
include = 'all',
exclude = {},
labels = {},
}
}
end)
end)

g.test_include = function()
local default_metrics = g.server:exec(function()
box.cfg{
metrics = {
include = {'info'},
}
}

return require('metrics').collect{invoke_callbacks = true}
end)

local uptime = utils.find_metric('tnt_info_uptime', default_metrics)
t.assert_not_equals(uptime, nil)
local memlua = utils.find_metric('tnt_info_memory_lua', default_metrics)
t.assert_equals(memlua, nil)
end

g.test_exclude = function()
local default_metrics = g.server:exec(function()
box.cfg{
metrics = {
exclude = {'memory'},
}
}

return require('metrics').collect{invoke_callbacks = true}
end)

local uptime = utils.find_metric('tnt_info_uptime', default_metrics)
t.assert_not_equals(uptime, nil)
local memlua = utils.find_metric('tnt_info_memory_lua', default_metrics)
t.assert_equals(memlua, nil)
end

g.test_include_with_exclude = function()
local default_metrics = g.server:exec(function()
box.cfg{
metrics = {
include = {'info', 'memory'},
exclude = {'memory'},
}
}

return require('metrics').collect{invoke_callbacks = true}
end)

local uptime = utils.find_metric('tnt_info_uptime', default_metrics)
t.assert_not_equals(uptime, nil)
local memlua = utils.find_metric('tnt_info_memory_lua', default_metrics)
t.assert_equals(memlua, nil)
end

g.test_include_none = function()
local default_metrics = g.server:exec(function()
box.cfg{
metrics = {
include = 'none',
exclude = {'memory'},
}
}

return require('metrics').collect{invoke_callbacks = true}
end)

t.assert_equals(default_metrics, {})
end

g.test_labels = function()
local default_metrics = g.server:exec(function()
box.cfg{
metrics = {
labels = {mylabel = 'myvalue'}
}
}

return require('metrics').collect{invoke_callbacks = true}
end)

local uptime = utils.find_obs('tnt_info_uptime', {mylabel = 'myvalue'},
default_metrics)
t.assert_equals(uptime.label_pairs, {mylabel = 'myvalue'})

default_metrics = g.server:exec(function()
box.cfg{
metrics = {
labels = {}
}
}

return require('metrics').collect{invoke_callbacks = true}
end)

uptime = utils.find_obs('tnt_info_uptime', {}, default_metrics)
t.assert_equals(uptime.label_pairs, {})
end

0 comments on commit 8f95eaa

Please sign in to comment.