Skip to content

Commit 8499ae2

Browse files
stats: integrate with metrics rock
If `metrics` [1] found, you can use metrics collectors to store statistics. `metrics >= 0.10.0` is required to use metrics driver. (`metrics >= 0.9.0` is required to use summary quantiles with age buckets. `metrics >= 0.5.0, < 0.9.0` is unsupported due to quantile overflow bug [2]. `metrics == 0.9.0` has bug that do not permits to create summary collector without quantiles [3]. In fact, user may use `metrics >= 0.5.0`, `metrics != 0.9.0` if he wants to use metrics without quantiles, and `metrics >= 0.9.0` if he wants to use metrics with quantiles. But this is confusing, so let's use a single restriction for both cases.) The metrics are part of global registry and can be exported together (e.g. to Prometheus) with default tools without any additional configuration. Disabling stats destroys the collectors. Metrics collectors are used by default if supported. To explicitly set driver, call `crud.cfg{ stats = true, stats_driver = driver }` ('local' or 'metrics'). To enable quantiles, call ``` crud.cfg{ stats = true, stats_driver = 'metrics', stats_quantiles = true, } ``` With quantiles, `latency` statistics are changed to 0.99 quantile of request execution time (with aging). Quantiles computations increases performance overhead up to 10% when used in statistics. Add CI matrix to run tests with `metrics` installed. To get full coverage on coveralls, #248 must be resolved. 1. https://github.com/tarantool/metrics 2. tarantool/metrics#235 3. tarantool/metrics#262 Closes #224
1 parent e21d1bb commit 8499ae2

File tree

12 files changed

+1192
-135
lines changed

12 files changed

+1192
-135
lines changed

.github/workflows/test_on_push.yaml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@ jobs:
1313
matrix:
1414
# We need 1.10.6 here to check that module works with
1515
# old Tarantool versions that don't have "tuple-keydef"/"tuple-merger" support.
16-
tarantool-version: ["1.10.6", "1.10", "2.2", "2.3", "2.4", "2.5", "2.6", "2.7"]
16+
tarantool-version: ["1.10.6", "1.10", "2.2", "2.3", "2.4", "2.5", "2.6", "2.7", "2.8"]
17+
metrics-version: [""]
1718
remove-merger: [false]
1819
include:
20+
- tarantool-version: "1.10"
21+
metrics-version: "0.12.0"
1922
- tarantool-version: "2.7"
2023
remove-merger: true
24+
- tarantool-version: "2.8"
25+
metrics-version: "0.1.8"
26+
- tarantool-version: "2.8"
27+
metrics-version: "0.10.0"
2128
- tarantool-version: "2.8"
2229
coveralls: true
30+
metrics-version: "0.12.0"
2331
fail-fast: false
2432
runs-on: [ubuntu-latest]
2533
steps:
@@ -47,6 +55,10 @@ jobs:
4755
tarantool --version
4856
./deps.sh
4957
58+
- name: Install metrics
59+
if: matrix.metrics-version != ''
60+
run: tarantoolctl rocks install metrics ${{ matrix.metrics-version }}
61+
5062
- name: Remove external merger if needed
5163
if: ${{ matrix.remove-merger }}
5264
run: rm .rocks/lib/tarantool/tuple/merger.so
@@ -71,6 +83,7 @@ jobs:
7183
strategy:
7284
matrix:
7385
bundle_version: [ "1.10.11-0-gf0b0e7ecf-r422", "2.7.3-0-gdddf926c3-r422" ]
86+
metrics-version: ["", "0.12.0"]
7487
fail-fast: false
7588
runs-on: [ ubuntu-latest ]
7689
steps:
@@ -86,6 +99,12 @@ jobs:
8699
tarantool --version
87100
./deps.sh
88101
102+
- name: Install metrics
103+
if: matrix.metrics-version != ''
104+
run: |
105+
source tarantool-enterprise/env.sh
106+
tarantoolctl rocks install metrics ${{ matrix.metrics-version }}
107+
89108
# This server starts and listen on 8084 port that is used for tests
90109
- name: Stop Mono server
91110
run: sudo kill -9 $(sudo lsof -t -i tcp:8084) || true

.luacheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ globals = {'box', 'utf8', 'checkers', '_TARANTOOL'}
33
include_files = {'**/*.lua', '*.luacheckrc', '*.rockspec'}
44
exclude_files = {'**/*.rocks/', 'tmp/', 'tarantool-enterprise/'}
55
max_line_length = 120
6+
max_comment_line_length = 150

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Added
1111
* Statistics for CRUD operations on router (#224).
12+
* Integrate CRUD statistics with [`metrics`](https://github.com/tarantool/metrics) (#224).
1213

1314
### Changed
1415

README.md

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,28 @@ crud.cfg{ stats = false }
694694
crud.reset_stats()
695695
```
696696

697+
If [`metrics`](https://github.com/tarantool/metrics) `0.10.0` or greater
698+
found, metrics collectors will be used by default to store statistics
699+
instead of local collectors. Quantiles in metrics summary collections
700+
are disabled by default. You can manually choose driver and enable quantiles.
701+
```lua
702+
-- Use metrics collectors. (Default if metrics found).
703+
crud.cfg{ stats = true, stats_driver = 'metrics' }
704+
705+
-- Use metrics collectors with 0.99 quantiles.
706+
crud.cfg{ stats = true, stats_driver = 'metrics', stats_quantiles = true }
707+
708+
-- Use simple local collectors.
709+
crud.cfg{ stats = true, stats_driver = 'local' }
710+
```
711+
697712
You can use `crud.cfg` to check current stats state.
698713
```lua
699714
crud.cfg
700715
---
701-
- stats: true
716+
- stats_quantiles: true
717+
stats: true
718+
stats_driver: local
702719
...
703720
```
704721
Beware that iterating through `crud.cfg` with pairs is not supported yet,
@@ -750,9 +767,39 @@ and `borders` (for `min` and `max` calls).
750767
Each operation section contains of different collectors
751768
for success calls and error (both error throw and `nil, err`)
752769
returns. `count` is total requests count since instance start
753-
or stats restart. `latency` is average time of requests execution,
770+
or stats restart. `latency` is 0.99 quantile of request execution
771+
time if `metrics` driver used and quantiles enabled,
772+
otherwise `latency` is total average.
754773
`time` is the total time of requests execution.
755774

775+
In [`metrics`](https://www.tarantool.io/en/doc/latest/book/monitoring/)
776+
registry statistics are stored as `tnt_crud_stats` metrics
777+
with `operation`, `status` and `name` labels.
778+
```
779+
metrics:collect()
780+
---
781+
- - label_pairs:
782+
status: ok
783+
operation: insert
784+
name: customers
785+
value: 221411
786+
metric_name: tnt_crud_stats_count
787+
- label_pairs:
788+
status: ok
789+
operation: insert
790+
name: customers
791+
value: 10.49834896344692
792+
metric_name: tnt_crud_stats_sum
793+
- label_pairs:
794+
status: ok
795+
operation: insert
796+
name: customers
797+
quantile: 0.99
798+
value: 0.00023606420935973
799+
metric_name: tnt_crud_stats
800+
...
801+
```
802+
756803
`select` section additionally contains `details` collectors.
757804
```lua
758805
crud.stats('my_space').select.details
@@ -769,6 +816,10 @@ looked up on storages while collecting responses for calls (including
769816
scrolls for multibatch requests). Details data is updated as part of
770817
the request process, so you may get new details before `select`/`pairs`
771818
call is finished and observed with count, latency and time collectors.
819+
In [`metrics`](https://www.tarantool.io/en/doc/latest/book/monitoring/)
820+
registry they are stored as `tnt_crud_map_reduces`,
821+
`tnt_crud_tuples_fetched` and `tnt_crud_tuples_lookup` metrics
822+
with `{ operation = 'select', name = space_name }` labels.
772823

773824
Since `pairs` request behavior differs from any other crud request, its
774825
statistics collection also has specific behavior. Statistics (`select`
@@ -780,7 +831,10 @@ collector.
780831

781832
Statistics are preserved between package reloads. Statistics are preserved
782833
between [Tarantool Cartridge role reloads](https://www.tarantool.io/en/doc/latest/book/cartridge/cartridge_api/modules/cartridge.roles/#reload)
783-
if you use CRUD Cartridge roles.
834+
if you use CRUD Cartridge roles. Beware that metrics 0.12.0 and below do not
835+
support preserving stats between role reload
836+
(see [tarantool/metrics#334](https://github.com/tarantool/metrics/issues/334)),
837+
thus this feature will be unsupported for `metrics` driver.
784838

785839
## Cartridge roles
786840

crud/cfg.lua

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,49 @@ local function set_defaults_if_empty(cfg)
1717
cfg.stats = false
1818
end
1919

20+
if cfg.stats_driver == nil then
21+
cfg.stats_driver = stats.get_default_driver()
22+
end
23+
24+
if cfg.stats_quantiles == nil then
25+
cfg.stats_quantiles = false
26+
end
27+
2028
return cfg
2129
end
2230

2331
local cfg = set_defaults_if_empty(stash.get(stash.name.cfg))
2432

33+
local function configure_stats(cfg, opts)
34+
if (opts.stats == nil)
35+
and (opts.stats_driver == nil)
36+
and (opts.stats_quantiles == nil) then
37+
return
38+
end
39+
40+
if opts.stats == nil then
41+
opts.stats = cfg.stats
42+
end
43+
44+
if opts.stats_driver == nil then
45+
opts.stats_driver = cfg.stats_driver
46+
end
47+
48+
if opts.stats_quantiles == nil then
49+
opts.stats_quantiles = cfg.stats_quantiles
50+
end
51+
52+
if opts.stats == true then
53+
stats.enable{ driver = opts.stats_driver, quantiles = opts.stats_quantiles }
54+
else
55+
stats.disable()
56+
end
57+
58+
rawset(cfg, 'stats', opts.stats)
59+
rawset(cfg, 'stats_driver', opts.stats_driver)
60+
rawset(cfg, 'stats_quantiles', opts.stats_quantiles)
61+
end
62+
2563
--- Configure CRUD module.
2664
--
2765
-- @function __call
@@ -34,22 +72,32 @@ local cfg = set_defaults_if_empty(stash.get(stash.name.cfg))
3472
-- Enable or disable statistics collect.
3573
-- Statistics are observed only on router instances.
3674
--
75+
-- @string[opt] opts.stats_driver
76+
-- `'local'` or `'metrics'`.
77+
-- If `'local'`, stores statistics in local registry (some Lua tables)
78+
-- and computes latency as overall average. `'metrics'` requires
79+
-- `metrics >= 0.10.0` installed and stores statistics in
80+
-- global metrics registry (integrated with exporters).
81+
-- `'metrics'` driver supports computing latency as 0.99 quantile with aging.
82+
-- If `'metrics'` driver is available, it is used by default,
83+
-- otherwise `'local'` is used.
84+
--
85+
-- @bool[opt] opts.stats_quantiles
86+
-- Enable or disable statistics quantiles (only for metrics driver).
87+
-- Quantiles computations increases performance overhead up to 10%.
88+
--
3789
-- @return Configuration table.
3890
--
3991
local function __call(self, opts)
40-
checks('table', { stats = '?boolean' })
92+
checks('table', {
93+
stats = '?boolean',
94+
stats_driver = '?string',
95+
stats_quantiles = '?boolean'
96+
})
4197

42-
opts = opts or {}
98+
opts = table.deepcopy(opts) or {}
4399

44-
if opts.stats ~= nil then
45-
if opts.stats == true then
46-
stats.enable()
47-
else
48-
stats.disable()
49-
end
50-
51-
rawset(cfg, 'stats', opts.stats)
52-
end
100+
configure_stats(cfg, opts)
53101

54102
return self
55103
end

crud/common/stash.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ local stash = {}
1616
-- @tfield string stats_local_registry
1717
-- Stash for local metrics registry.
1818
--
19+
-- @tfield string stats_metrics_registry
20+
-- Stash for metrics rocks statistics registry.
21+
--
1922
stash.name = {
2023
cfg = '__crud_cfg',
2124
stats_internal = '__crud_stats_internal',
22-
stats_local_registry = '__crud_stats_local_registry'
25+
stats_local_registry = '__crud_stats_local_registry',
26+
stats_metrics_registry = '__crud_stats_metrics_registry'
2327
}
2428

2529
--- Setup Tarantool Cartridge reload.

0 commit comments

Comments
 (0)