-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stats: add statistics for CRUD router operations
Add statistics module for collecting metrics of CRUD operations on router. Wrap all CRUD operation calls in the statistics collector. Statistics must be enabled manually with `crud.cfg`. They can be disabled, restarted or re-enabled later. This patch introduces `crud.cfg`. `crud.cfg` is a tool to set module configuration. It is similar to Tarantool `box.cfg`, although we don't need to call it to bootstrap the module -- it is used only to change configuration. `crud.cfg` is a callable table. To change configuration, call it: `crud.cfg{ stats = true }`. You can check table contents as with ordinary table, but do not change them directly -- use call instead. Table contents is immutable and use proxy approach (see [1, 2]). Iterating through `crud.cfg` with pairs is not supported yet, refer to #265. `crud.stats()` returns --- - spaces: my_space: insert: ok: latency: 0.002 count: 19800 time: 39.6 error: latency: 0.000001 count: 4 time: 0.000004 ... `spaces` section contains statistics for each observed space. If operation has never been called for a space, the corresponding field will be empty. If no requests has been called for a space, it will not be represented. Space data is based on client requests rather than storages schema, so requests for non-existing spaces are also collected. Possible statistics operation labels are `insert` (for `insert` and `insert_object` calls), `get`, `replace` (for `replace` and `replace_object` calls), `update`, `upsert` (for `upsert` and `upsert_object` calls), `delete`, `select` (for `select` and `pairs` calls), `truncate`, `len`, `count` and `borders` (for `min` and `max` calls). Each operation section consists of different collectors for success calls and error (both error throw and `nil, err`) returns. `count` is the total requests count since instance start or stats restart. `latency` is the average time of requests execution, `time` is the total time of requests execution. Since `pairs` request behavior differs from any other crud request, its statistics collection also has specific behavior. Statistics (`select` section) are updated after `pairs` cycle is finished: you either have iterated through all records or an error was thrown. If your pairs cycle was interrupted with `break`, statistics will be collected when pairs objects are cleaned up with Lua garbage collector. Statistics are preserved between package reloads. Statistics are preserved between Tarantool Cartridge role reloads [3] if CRUD Cartridge roles are used. 1. http://lua-users.org/wiki/ReadOnlyTables 2. tarantool/tarantool#2867 3. https://www.tarantool.io/en/doc/latest/book/cartridge/cartridge_api/modules/cartridge.roles/#reload Part of #224
- Loading branch information
1 parent
461f25f
commit 6da4f56
Showing
18 changed files
with
2,023 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
---- Module for CRUD configuration. | ||
-- @module crud.cfg | ||
-- | ||
|
||
local checks = require('checks') | ||
local errors = require('errors') | ||
|
||
local stash = require('crud.common.stash') | ||
local stats = require('crud.stats') | ||
|
||
local CfgError = errors.new_class('CfgError', {capture_stack = false}) | ||
|
||
local cfg_module = {} | ||
|
||
local function set_defaults_if_empty(cfg) | ||
if cfg.stats == nil then | ||
cfg.stats = false | ||
end | ||
|
||
return cfg | ||
end | ||
|
||
local cfg = set_defaults_if_empty(stash.get(stash.name.cfg)) | ||
|
||
--- Configure CRUD module. | ||
-- | ||
-- @function __call | ||
-- | ||
-- @tab self | ||
-- | ||
-- @tab[opt] opts | ||
-- | ||
-- @bool[opt] opts.stats | ||
-- Enable or disable statistics collect. | ||
-- Statistics are observed only on router instances. | ||
-- | ||
-- @return Configuration table. | ||
-- | ||
local function __call(self, opts) | ||
checks('table', { stats = '?boolean' }) | ||
|
||
opts = opts or {} | ||
|
||
if opts.stats ~= nil then | ||
if opts.stats == true then | ||
stats.enable() | ||
else | ||
stats.disable() | ||
end | ||
|
||
rawset(cfg, 'stats', opts.stats) | ||
end | ||
|
||
return self | ||
end | ||
|
||
local function __newindex() | ||
CfgError:assert(false, 'Use crud.cfg{} instead') | ||
end | ||
|
||
-- Iterating through `crud.cfg` with pairs is not supported | ||
-- yet, refer to tarantool/crud#265. | ||
cfg_module.cfg = setmetatable({}, { | ||
__index = cfg, | ||
__newindex = __newindex, | ||
__call = __call, | ||
__serialize = function() return cfg end | ||
}) | ||
|
||
return cfg_module |
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,63 @@ | ||
---- Module for preserving data between reloads. | ||
-- @module crud.common.stash | ||
-- | ||
local dev_checks = require('crud.common.dev_checks') | ||
|
||
local stash = {} | ||
|
||
--- Available stashes list. | ||
-- | ||
-- @tfield string cfg | ||
-- Stash for CRUD module configuration. | ||
-- | ||
-- @tfield string stats_internal | ||
-- Stash for main stats module. | ||
-- | ||
-- @tfield string stats_local_registry | ||
-- Stash for local metrics registry. | ||
-- | ||
stash.name = { | ||
cfg = '__crud_cfg', | ||
stats_internal = '__crud_stats_internal', | ||
stats_local_registry = '__crud_stats_local_registry' | ||
} | ||
|
||
--- Setup Tarantool Cartridge reload. | ||
-- | ||
-- Call on Tarantool Cartridge roles that are expected | ||
-- to use stashes. | ||
-- | ||
-- @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) | ||
dev_checks('string') | ||
|
||
local instance = rawget(_G, name) or {} | ||
rawset(_G, name, instance) | ||
|
||
return instance | ||
end | ||
|
||
return stash |
Oops, something went wrong.