|
5 | 5 | local clock = require('clock')
|
6 | 6 | local checks = require('checks')
|
7 | 7 | local fun = require('fun')
|
| 8 | +local log = require('log') |
| 9 | +local vshard = require('vshard') |
8 | 10 |
|
9 | 11 | local dev_checks = require('crud.common.dev_checks')
|
10 | 12 | local stash = require('crud.common.stash')
|
| 13 | +local utils = require('crud.common.utils') |
11 | 14 | local op_module = require('crud.stats.operation')
|
12 | 15 | local registry = require('crud.stats.local_registry')
|
13 | 16 |
|
@@ -107,6 +110,22 @@ function stats.get(space_name)
|
107 | 110 | return registry.get(space_name)
|
108 | 111 | end
|
109 | 112 |
|
| 113 | +local function resolve_space_name(space_id) |
| 114 | + local replicasets = vshard.router.routeall() |
| 115 | + if next(replicasets) == nil then |
| 116 | + log.warn('Failed to resolve space name for stats: no replicasets found') |
| 117 | + return nil |
| 118 | + end |
| 119 | + |
| 120 | + local space = utils.get_space(space_id, replicasets) |
| 121 | + if space == nil then |
| 122 | + log.warn('Failed to resolve space name for stats: no space found for id %d', space_id) |
| 123 | + return nil |
| 124 | + end |
| 125 | + |
| 126 | + return space.name |
| 127 | +end |
| 128 | + |
110 | 129 | -- Hack to set __gc for a table in Lua 5.1
|
111 | 130 | -- See https://stackoverflow.com/questions/27426704/lua-5-1-workaround-for-gc-metamethod-for-tables
|
112 | 131 | -- or https://habr.com/ru/post/346892/
|
@@ -175,11 +194,24 @@ local function wrap_pairs_gen(build_latency, space_name, op, gen, param, state)
|
175 | 194 | end
|
176 | 195 |
|
177 | 196 | local function wrap_tail(space_name, op, pairs, start_time, call_status, ...)
|
178 |
| - dev_checks('string', 'string', 'boolean', 'number', 'boolean') |
| 197 | + dev_checks('string|number', 'string', 'boolean', 'number', 'boolean') |
179 | 198 |
|
180 | 199 | local finish_time = clock.monotonic()
|
181 | 200 | local latency = finish_time - start_time
|
182 | 201 |
|
| 202 | + -- If space id is provided instead of name, try to resolve name. |
| 203 | + -- If resolve have failed, use id as string to observe space. |
| 204 | + -- If using space id will be deprecated, remove this code as well, |
| 205 | + -- see https://github.com/tarantool/crud/issues/255 |
| 206 | + if type(space_name) ~= 'string' then |
| 207 | + local name = resolve_space_name(space_name) |
| 208 | + if name ~= nil then |
| 209 | + space_name = name |
| 210 | + else |
| 211 | + space_name = tostring(space_name) |
| 212 | + end |
| 213 | + end |
| 214 | + |
183 | 215 | if call_status == false then
|
184 | 216 | registry.observe(latency, space_name, op, 'error')
|
185 | 217 | error((...), 2)
|
|
0 commit comments