-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch introduces `metrics.cfg` as a single entrypoint to configure the module. It covers both `enable_default_metrics` and `set_global_labels` with the same behavior (except for deprecated `include={}` behavior, where it uses enable_v2 approach). It doesn't cover external features like cartridge HTTP setup yet, but it may change in the future. A single entrypoint is a prerequirement for configuring the module with box.cfg after embedding. `metrics.cfg` API is similar to `box.cfg` API (or more like `crud.cfg` API [1] since it has read-only table values). `metrics.cfg` values and effect are preserved between reloads. So if some default metrics were enabled with cfg before the reload, both their collectors and callbacks will remain active (they still be reloaded though). It is planned to replace `cartridge.roles.metrics` configuration code with using metrics.cfg after the deprecated behavior will be dropped. 1. tarantool/crud@6da4f56 Part of tarantool/tarantool#7725
- Loading branch information
1 parent
5d0e1b0
commit d892e7d
Showing
14 changed files
with
450 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
-- Based on https://github.com/tarantool/crud/blob/73bf5bf9353f9b9ee69c95bb14c610be8f2daeac/crud/cfg.lua | ||
|
||
local checks = require('checks') | ||
|
||
local metrics_api = require('metrics.api') | ||
local const = require('metrics.const') | ||
local stash = require('metrics.stash') | ||
local metrics_tarantool = require('metrics.tarantool') | ||
|
||
local function set_defaults_if_empty(cfg) | ||
if cfg.include == nil then | ||
cfg.include = const.ALL | ||
end | ||
|
||
if cfg.exclude == nil then | ||
cfg.exclude = {} | ||
end | ||
|
||
if cfg.labels == nil then | ||
cfg.labels = {} | ||
end | ||
|
||
return cfg | ||
end | ||
|
||
local function configure(cfg, opts) | ||
if opts.include == nil then | ||
opts.include = cfg.include | ||
end | ||
|
||
if opts.exclude == nil then | ||
opts.exclude = cfg.exclude | ||
end | ||
|
||
if opts.labels == nil then | ||
opts.labels = cfg.labels | ||
end | ||
|
||
|
||
metrics_tarantool.enable_v2(opts.include, opts.exclude) | ||
metrics_api.set_global_labels(opts.labels) | ||
|
||
rawset(cfg, 'include', opts.include) | ||
rawset(cfg, 'exclude', opts.exclude) | ||
rawset(cfg, 'labels', opts.labels) | ||
end | ||
|
||
local _cfg = set_defaults_if_empty(stash.get(stash.name.cfg)) | ||
local _cfg_internal = stash.get(stash.name.cfg_internal) | ||
|
||
if _cfg_internal.initialized then | ||
configure(_cfg, {}) | ||
end | ||
|
||
local function __call(self, opts) | ||
checks('table', { | ||
include = '?string|table', | ||
exclude = '?table', | ||
labels = '?table', | ||
}) | ||
|
||
opts = table.deepcopy(opts) or {} | ||
|
||
configure(_cfg, opts) | ||
|
||
_cfg_internal.initialized = true | ||
|
||
return self | ||
end | ||
|
||
local function __index(_, key) | ||
if _cfg_internal.initialized then | ||
return _cfg[key] | ||
else | ||
error('Call metrics.cfg{} first') | ||
end | ||
end | ||
|
||
local function __newindex() | ||
error('Use metrics.cfg{} instead') | ||
end | ||
|
||
return { | ||
-- Iterating through `metrics.cfg` with pairs is not supported yet. | ||
cfg = setmetatable({}, { | ||
__index = __index, | ||
__newindex = __newindex, | ||
__call = __call, | ||
__serialize = function() return _cfg end | ||
}), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
-- Based on https://github.com/tarantool/crud/blob/73bf5bf9353f9b9ee69c95bb14c610be8f2daeac/crud/common/stash.lua | ||
|
||
local stash = {} | ||
|
||
--- Available stashes list. | ||
-- | ||
-- @tfield string cfg | ||
-- Stash for metrics module configuration. | ||
-- | ||
stash.name = { | ||
cfg = '__metrics_cfg', | ||
cfg_internal = '__metrics_cfg_internal' | ||
} | ||
|
||
--- Setup Tarantool Cartridge reload. | ||
-- | ||
-- @function setup_cartridge_reload | ||
-- | ||
-- @return Returns | ||
-- | ||
function stash.setup_cartridge_reload() | ||
local hotreload = require('cartridge.hotreload') | ||
for _, name in pairs(stash.name) do | ||
hotreload.whitelist_globals({ name }) | ||
end | ||
end | ||
|
||
--- Get a stash instance, initialize if needed. | ||
-- | ||
-- Stashes are persistent to package reload. | ||
-- To use them with Cartridge roles reload, | ||
-- call `stash.setup_cartridge_reload` in role. | ||
-- | ||
-- @function get | ||
-- | ||
-- @string name | ||
-- Stash identifier. Use one from `stash.name` table. | ||
-- | ||
-- @treturn table A stash instance. | ||
-- | ||
function stash.get(name) | ||
local instance = rawget(_G, name) or {} | ||
rawset(_G, name, instance) | ||
|
||
return instance | ||
end | ||
|
||
return stash |
Oops, something went wrong.