-
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.
Add statistics for CRUD operations on router
Add statistics module for collecting metrics of CRUD operations on router. Wrap all CRUD operation calls in statistics collector. Statistics must be enabled manually, and they can be disabled, restarted or re-enabled later. `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 space_not_found: 0 ... `spaces` section contains statistics for each observed space. If operation has never been called for space, corresponding field will be empty. If no operations has been called for a space, it will not be represented. Operations called for spaces not found both on storages and in statistics registry (including cases when crud can't verify space existence) will increment `space_not_found` counter. 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` and `borders` (for `min` and `max` calls). Each operation section contains of different collectors for success calls and error (both error throw and `nil, err`) returns. `count` is total requests count since instance start or stats restart. `latency` is average time of requests execution, `time` is total time of requests execution. Part of #224
- Loading branch information
1 parent
6fcf363
commit 5685009
Showing
10 changed files
with
1,471 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
local dev_checks = require('crud.common.dev_checks') | ||
local registry_common = require('crud.stats.registry_common') | ||
|
||
local registry = {} | ||
local internal_registry = {} | ||
|
||
--- Initialize local metrics registry | ||
-- | ||
-- Registries are not meant to used explicitly | ||
-- by users, init is not guaranteed to be idempotent. | ||
-- | ||
-- @function init | ||
-- | ||
-- @treturn boolean Returns true. | ||
-- | ||
function registry.init() | ||
internal_registry.spaces = {} | ||
internal_registry.space_not_found = 0 | ||
|
||
return true | ||
end | ||
|
||
--- Destroy local metrics registry | ||
-- | ||
-- Registries are not meant to used explicitly | ||
-- by users, destroy is not guaranteed to be idempotent. | ||
-- | ||
-- @function destroy | ||
-- | ||
-- @treturn boolean Returns true. | ||
-- | ||
function registry.destroy() | ||
internal_registry = {} | ||
|
||
return true | ||
end | ||
|
||
--- Get copy of local metrics registry | ||
-- | ||
-- Registries are not meant to used explicitly | ||
-- by users, get is not guaranteed to work without init. | ||
-- | ||
-- @function get | ||
-- | ||
-- @tparam string space_name | ||
-- (Optional) If specified, returns table with statistics | ||
-- of operations on table, separated by operation type and | ||
-- execution status. If there wasn't any requests for table, | ||
-- returns {}. In not specified, returns table with statistics | ||
-- about all existing spaces and count of calls to spaces | ||
-- that wasn't found. | ||
-- | ||
-- @treturn table Returns copy of metrics registry (or registry section). | ||
-- | ||
function registry.get(space_name) | ||
dev_checks('?string') | ||
|
||
if space_name ~= nil then | ||
return table.deepcopy(internal_registry.spaces[space_name]) or {} | ||
end | ||
|
||
return table.deepcopy(internal_registry) | ||
end | ||
|
||
--- Check if space statistics are present in registry | ||
-- | ||
-- @function is_unknown_space | ||
-- | ||
-- @tparam string space_name | ||
-- Name of space. | ||
-- | ||
-- @treturn boolean True, if space stats found. False otherwise. | ||
-- | ||
function registry.is_unknown_space(space_name) | ||
dev_checks('string') | ||
|
||
return internal_registry.spaces[space_name] == nil | ||
end | ||
|
||
--- Increase requests count and update latency info | ||
-- | ||
-- @function observe | ||
-- | ||
-- @tparam string space_name | ||
-- Name of space. | ||
-- | ||
-- @tparam number latency | ||
-- Time of call execution. | ||
-- | ||
-- @tparam string op | ||
-- Label of registry collectors. | ||
-- Use `require('crud.common.const').OP` to pick one. | ||
-- | ||
-- @tparam string success | ||
-- 'ok' if no errors on execution, 'error' otherwise. | ||
-- | ||
-- @treturn boolean Returns true. | ||
-- | ||
function registry.observe(latency, space_name, op, status) | ||
dev_checks('number', 'string', 'string', 'string') | ||
|
||
registry_common.init_collectors_if_required(internal_registry.spaces, space_name, op) | ||
local collectors = internal_registry.spaces[space_name][op][status] | ||
|
||
collectors.count = collectors.count + 1 | ||
collectors.time = collectors.time + latency | ||
collectors.latency = collectors.time / collectors.count | ||
|
||
return true | ||
end | ||
|
||
--- Increase count of "space not found" collector by one | ||
-- | ||
-- @function observe_space_not_found | ||
-- | ||
-- @treturn boolean Returns true. | ||
-- | ||
function registry.observe_space_not_found() | ||
internal_registry.space_not_found = internal_registry.space_not_found + 1 | ||
|
||
return true | ||
end | ||
|
||
return registry |
Oops, something went wrong.