|
| 1 | +local is_package, metrics = pcall(require, 'metrics') |
| 2 | + |
| 3 | +local label = require('crud.stats.label') |
| 4 | +local dev_checks = require('crud.common.dev_checks') |
| 5 | +local registry_common = require('crud.stats.registry_common') |
| 6 | + |
| 7 | +local registry = {} |
| 8 | +local _registry = {} |
| 9 | + |
| 10 | +local metric_name = { |
| 11 | + -- Summary collector for all operations. |
| 12 | + op = 'tnt_crud_stats', |
| 13 | + -- `*_count` and `*_sum` are automatically created |
| 14 | + -- by summary collector. |
| 15 | + op_count = 'tnt_crud_stats_count', |
| 16 | + op_sum = 'tnt_crud_stats_sum', |
| 17 | + |
| 18 | + -- Counter collectors for select/pairs details. |
| 19 | + tuples_fetched = 'tnt_crud_tuples_fetched', |
| 20 | + tuples_lookup = 'tnt_crud_tuples_lookup', |
| 21 | + map_reduces = 'tnt_crud_map_reduces', |
| 22 | +} |
| 23 | + |
| 24 | +local LATENCY_QUANTILE = 0.99 |
| 25 | + |
| 26 | +-- Raising quantile tolerance (1e-2) may result in crucial |
| 27 | +-- performance drops. |
| 28 | +local DEFAULT_QUANTILES = { |
| 29 | + [LATENCY_QUANTILE] = 1e-2, |
| 30 | +} |
| 31 | + |
| 32 | +local DEFAULT_SUMMARY_PARAMS = { |
| 33 | + age_buckets_count = 2, |
| 34 | + max_age_time = 60, |
| 35 | +} |
| 36 | + |
| 37 | +--- Check if application supports metrics rock for registry |
| 38 | +-- |
| 39 | +-- `metrics >= 0.9.0` is required to use summary with |
| 40 | +-- age buckets. `metrics >= 0.5.0, < 0.9.0` is unsupported |
| 41 | +-- due to quantile overflow bug |
| 42 | +-- (https://github.com/tarantool/metrics/issues/235). |
| 43 | +-- |
| 44 | +-- @function is_supported |
| 45 | +-- |
| 46 | +-- @treturn boolean Returns true if `metrics >= 0.9.0` found, false otherwise. |
| 47 | +-- |
| 48 | +function registry.is_supported() |
| 49 | + if is_package == false then |
| 50 | + return false |
| 51 | + end |
| 52 | + |
| 53 | + -- Only metrics >= 0.9.0 supported. |
| 54 | + local is_summary, summary = pcall(require, 'metrics.collectors.summary') |
| 55 | + if is_summary == false or summary.rotate_age_buckets == nil then |
| 56 | + return false |
| 57 | + end |
| 58 | + |
| 59 | + return true |
| 60 | +end |
| 61 | + |
| 62 | + |
| 63 | +--- Initialize collectors in global metrics registry |
| 64 | +-- |
| 65 | +-- @function init |
| 66 | +-- |
| 67 | +-- @treturn boolean Returns true. |
| 68 | +-- |
| 69 | +function registry.init() |
| 70 | + _registry[metric_name.op] = metrics.summary( |
| 71 | + metric_name.op, |
| 72 | + 'CRUD router calls statistics', |
| 73 | + DEFAULT_QUANTILES, |
| 74 | + DEFAULT_SUMMARY_PARAMS) |
| 75 | + |
| 76 | + _registry[metric_name.tuples_fetched] = metrics.counter( |
| 77 | + metric_name.tuples_fetched, |
| 78 | + 'Tuples fetched from CRUD storages during select/pairs') |
| 79 | + |
| 80 | + _registry[metric_name.tuples_lookup] = metrics.counter( |
| 81 | + metric_name.tuples_lookup, |
| 82 | + 'Tuples looked up on CRUD storages while collecting response during select/pairs') |
| 83 | + |
| 84 | + _registry[metric_name.map_reduces] = metrics.counter( |
| 85 | + metric_name.map_reduces, |
| 86 | + 'Map reduces planned during CRUD select/pairs') |
| 87 | + |
| 88 | + return true |
| 89 | +end |
| 90 | + |
| 91 | +--- Unregister collectors in global metrics registry |
| 92 | +-- |
| 93 | +-- @function destroy |
| 94 | +-- |
| 95 | +-- @treturn boolean Returns true. |
| 96 | +-- |
| 97 | +function registry.destroy() |
| 98 | + for _, c in pairs(_registry) do |
| 99 | + metrics.registry:unregister(c) |
| 100 | + end |
| 101 | + |
| 102 | + _registry = {} |
| 103 | + return true |
| 104 | +end |
| 105 | + |
| 106 | +--- Get copy of global metrics registry |
| 107 | +-- |
| 108 | +-- @function get |
| 109 | +-- |
| 110 | +-- @treturn table Returns copy of metrics registry. |
| 111 | +function registry.get() |
| 112 | + local stats = {} |
| 113 | + |
| 114 | + -- Fill empty collectors with zero values. |
| 115 | + for _, op_label in pairs(label) do |
| 116 | + stats[op_label] = registry_common.build_collector(op_label) |
| 117 | + end |
| 118 | + |
| 119 | + for _, obs in ipairs(_registry[metric_name.op]:collect()) do |
| 120 | + local operation = obs.label_pairs.operation |
| 121 | + local status = obs.label_pairs.status |
| 122 | + if obs.metric_name == metric_name.op then |
| 123 | + if obs.label_pairs.quantile == LATENCY_QUANTILE then |
| 124 | + stats[operation][status].latency = obs.value |
| 125 | + end |
| 126 | + elseif obs.metric_name == metric_name.op_sum then |
| 127 | + stats[operation][status].time = obs.value |
| 128 | + elseif obs.metric_name == metric_name.op_count then |
| 129 | + stats[operation][status].count = obs.value |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + local _, obs_tuples_fetched = next(_registry[metric_name.tuples_fetched]:collect()) |
| 134 | + if obs_tuples_fetched ~= nil then |
| 135 | + stats[label.SELECT].details.tuples_fetched = obs_tuples_fetched.value |
| 136 | + end |
| 137 | + |
| 138 | + local _, obs_tuples_lookup = next(_registry[metric_name.tuples_lookup]:collect()) |
| 139 | + if obs_tuples_lookup ~= nil then |
| 140 | + stats[label.SELECT].details.tuples_lookup = obs_tuples_lookup.value |
| 141 | + end |
| 142 | + |
| 143 | + local _, obs_map_reduces = next(_registry[metric_name.map_reduces]:collect()) |
| 144 | + if obs_map_reduces ~= nil then |
| 145 | + stats[label.SELECT].details.map_reduces = obs_map_reduces.value |
| 146 | + end |
| 147 | + |
| 148 | + return stats |
| 149 | +end |
| 150 | + |
| 151 | +--- Increase requests count and update latency info |
| 152 | +-- |
| 153 | +-- @function observe |
| 154 | +-- |
| 155 | +-- @tparam string op_label |
| 156 | +-- Label of registry collectos. |
| 157 | +-- Use `require('crud.common.const').OP` to pick one. |
| 158 | +-- |
| 159 | +-- @tparam boolean success |
| 160 | +-- true if no errors on execution, false otherwise. |
| 161 | +-- |
| 162 | +-- @tparam number latency |
| 163 | +-- Time of call execution. |
| 164 | +-- |
| 165 | +-- @treturn boolean Returns true. |
| 166 | +-- |
| 167 | + |
| 168 | +local total = 0 |
| 169 | + |
| 170 | +function registry.observe(op_label, success, latency) |
| 171 | + dev_checks('string', 'boolean', 'number') |
| 172 | + |
| 173 | + local label_pairs = { operation = op_label } |
| 174 | + if success == true then |
| 175 | + label_pairs.status = 'ok' |
| 176 | + else |
| 177 | + label_pairs.status = 'error' |
| 178 | + end |
| 179 | + |
| 180 | + local clock = require('clock') |
| 181 | + local start = clock.monotonic() |
| 182 | + _registry[metric_name.op]:observe(latency, label_pairs) |
| 183 | + local diff = clock.monotonic() - start |
| 184 | + -- require('log').error("latency: %f", latency) |
| 185 | + -- require('log').error("diff: %f", diff) |
| 186 | + total = total + diff |
| 187 | + -- require('log').error("total: %f", total) |
| 188 | + |
| 189 | + return true |
| 190 | +end |
| 191 | + |
| 192 | +--- Increase statistics of storage select/pairs calls |
| 193 | +-- |
| 194 | +-- @function observe_fetch |
| 195 | +-- |
| 196 | +-- @tparam number tuples_fetched |
| 197 | +-- Count of tuples fetched during storage call. |
| 198 | +-- |
| 199 | +-- @tparam number tuples_lookup |
| 200 | +-- Count of tuples looked up on storages while collecting response. |
| 201 | +-- |
| 202 | +-- @treturn boolean Returns true. |
| 203 | +-- |
| 204 | +function registry.observe_fetch(tuples_fetched, tuples_lookup) |
| 205 | + dev_checks('number', 'number') |
| 206 | + |
| 207 | + local label_pairs = { operation = label.SELECT } |
| 208 | + |
| 209 | + _registry[metric_name.tuples_fetched]:inc(tuples_fetched, label_pairs) |
| 210 | + _registry[metric_name.tuples_lookup]:inc(tuples_lookup, label_pairs) |
| 211 | + return true |
| 212 | +end |
| 213 | + |
| 214 | +--- Increase statistics of planned map reduces during select/pairs |
| 215 | +-- |
| 216 | +-- @function observe_map_reduces |
| 217 | +-- |
| 218 | +-- @tparam number count |
| 219 | +-- Count of map reduces planned. |
| 220 | +-- |
| 221 | +-- @treturn boolean Returns true. |
| 222 | +-- |
| 223 | +function registry.observe_map_reduces(count) |
| 224 | + dev_checks('number') |
| 225 | + |
| 226 | + local label_pairs = { operation = label.SELECT } |
| 227 | + |
| 228 | + _registry[metric_name.map_reduces]:inc(count, label_pairs) |
| 229 | + return true |
| 230 | +end |
| 231 | + |
| 232 | +return registry |
0 commit comments