Skip to content

Commit 07f59c8

Browse files
Resolve space name in statistics
`crud.len` supports using space id instead of name. After this patch, stats wrapper support mapping id to name. Part of #224
1 parent 2a3e671 commit 07f59c8

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

crud/stats/module.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ local function wrap_tail(space_name, op, opts, start_time, call_status, ...)
118118
end
119119

120120
local context_stats = utils.get_context_section('router_stats')
121+
-- Describe local variables to use `goto`.
122+
local space_not_found_msg, space
121123

122124
-- If space not exists, do not build a separate collector for it.
123125
-- Call request for non-existing space will always result in error.
@@ -127,7 +129,7 @@ local function wrap_tail(space_name, op, opts, start_time, call_status, ...)
127129
-- it is treated as unknown as well.
128130
if status == 'error' and registry.is_unknown_space(space_name) then
129131
if type(err) == 'table' and type(err.err) == 'string' then
130-
local space_not_found_msg = utils.space_doesnt_exist_msg(space_name)
132+
space_not_found_msg = utils.space_doesnt_exist_msg(space_name)
131133
if string.find(err.err, space_not_found_msg) ~= nil then
132134
registry.observe_space_not_found()
133135
goto return_values
@@ -137,13 +139,22 @@ local function wrap_tail(space_name, op, opts, start_time, call_status, ...)
137139
-- We can't rely only on parsing error value because space existence
138140
-- is not always the first check in request validation.
139141
-- Check explicitly if space do not exist.
140-
local space = utils.get_space(space_name, vshard.router.routeall())
142+
space = utils.get_space(space_name, vshard.router.routeall())
141143
if space == nil then
142144
registry.observe_space_not_found()
143145
goto return_values
144146
end
145147
end
146148

149+
-- If space id is provided instead of name, resolve name.
150+
if type(space_name) ~= 'string' then
151+
if space == nil then
152+
space = utils.get_space(space_name, vshard.router.routeall())
153+
end
154+
155+
space_name = space.name
156+
end
157+
147158
registry.observe(latency, space_name, op, status)
148159

149160
if context_stats ~= nil then

test/entrypoint/srv_select.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package.preload['customers-storage'] = function()
2424
},
2525
if_not_exists = true,
2626
engine = engine,
27+
id = 542,
2728
})
2829
-- primary index
2930
customers_space:create_index('id_index', {

test/entrypoint/srv_simple_operations.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package.preload['customers-storage'] = function()
2121
},
2222
if_not_exists = true,
2323
engine = engine,
24+
id = 542,
2425
})
2526
customers_space:create_index('id', {
2627
parts = { {field = 'id'} },

test/integration/stats_test.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local stats_registry_common = require('crud.stats.registry_common')
77
local g = t.group('stats_integration')
88
local helpers = require('test.helper')
99

10+
local space_id = 542
1011
local space_name = 'customers'
1112
local unknown_space_name = 'non_existing_space'
1213

@@ -559,3 +560,11 @@ for name, case in pairs(select_cases) do
559560
'Expected count of map reduces planned')
560561
end
561562
end
563+
564+
g.test_resolve_name_from_id = function(g)
565+
local op = 'len'
566+
g.router:call('crud.len', { space_id })
567+
568+
local stats = g:get_stats(space_name)
569+
t.assert_not_equals(stats[op], nil, "Statistics is filled by name")
570+
end

test/unit/stats_test.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local utils = require('crud.common.utils')
99
local g = t.group('stats_unit')
1010
local helpers = require('test.helper')
1111

12+
local space_id = 542
1213
local space_name = 'customers'
1314
local unknown_space_name = 'non_existing_space'
1415

@@ -496,3 +497,11 @@ g.test_disable_stats_after_fetch_callback_get_do_not_break_call = function(g)
496497

497498
t.success('No unexpected errors')
498499
end
500+
501+
g.test_resolve_name_from_id = function(g)
502+
local op = stats_module.op.LEN
503+
g.router:eval(call_wrapped, { 'return_true', stats_module.op.LEN, {}, space_id })
504+
505+
local stats = g:get_stats(space_name)
506+
t.assert_not_equals(stats[op], nil, "Statistics is filled by name")
507+
end

0 commit comments

Comments
 (0)