Skip to content

Commit 9fadca8

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 012fe9f commit 9fadca8

File tree

11 files changed

+1184
-135
lines changed

11 files changed

+1184
-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

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 (gh-224).
12+
* Integrate CRUD statistics with [`metrics`](https://github.com/tarantool/metrics) (gh-224).
1213

1314
### Changed
1415

README.md

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,28 @@ crud.cfg{ stats = false }
621621
crud.reset_stats()
622622
```
623623

624+
If [`metrics`](https://github.com/tarantool/metrics) `0.10.0` or greater
625+
found, metrics collectors will be used by default to store statistics
626+
instead of local collectors. Quantiles in metrics summary collections
627+
are disabled by default. You can manually choose driver and enable quantiles.
628+
```lua
629+
-- Use metrics collectors. (Default if metrics found).
630+
crud.cfg{ stats = true, stats_driver = 'metrics' }
631+
632+
-- Use metrics collectors with 0.99 quantiles.
633+
crud.cfg{ stats = true, stats_driver = 'metrics', stats_quantiles = true }
634+
635+
-- Use simple local collectors.
636+
crud.cfg{ stats = true, stats_driver = 'local' }
637+
```
638+
624639
You can use `crud.cfg` to check current stats state.
625640
```lua
626641
crud.cfg
627642
---
628-
- stats: true
643+
- stats_quantiles: true
644+
stats: true
645+
stats_driver: local
629646
...
630647
```
631648

@@ -675,9 +692,39 @@ Possible statistics operation labels are
675692
Each operation section contains of different collectors
676693
for success calls and error (both error throw and `nil, err`)
677694
returns. `count` is total requests count since instance start
678-
or stats restart. `latency` is average time of requests execution,
695+
or stats restart. `latency` is 0.99 quantile of request execution
696+
time if `metrics` driver used and quantiles enabled,
697+
otherwise `latency` is total average.
679698
`time` is the total time of requests execution.
680699

700+
In [`metrics`](https://www.tarantool.io/en/doc/latest/book/monitoring/)
701+
registry statistics are stored as `tnt_crud_stats` metrics
702+
with `operation`, `status` and `name` labels.
703+
```
704+
metrics:collect()
705+
---
706+
- - label_pairs:
707+
status: ok
708+
operation: insert
709+
name: customers
710+
value: 221411
711+
metric_name: tnt_crud_stats_count
712+
- label_pairs:
713+
status: ok
714+
operation: insert
715+
name: customers
716+
value: 10.49834896344692
717+
metric_name: tnt_crud_stats_sum
718+
- label_pairs:
719+
status: ok
720+
operation: insert
721+
name: customers
722+
quantile: 0.99
723+
value: 0.00023606420935973
724+
metric_name: tnt_crud_stats
725+
...
726+
```
727+
681728
`select` section additionally contains `details` collectors.
682729
```lua
683730
crud.stats('my_space').select.details
@@ -694,6 +741,10 @@ looked up on storages while collecting responses for calls (including
694741
scrolls for multibatch requests). Details data is updated as part of
695742
the request process, so you may get new details before `select`/`pairs`
696743
call is finished and observed with count, latency and time collectors.
744+
In [`metrics`](https://www.tarantool.io/en/doc/latest/book/monitoring/)
745+
registry they are stored as `tnt_crud_map_reduces`,
746+
`tnt_crud_tuples_fetched` and `tnt_crud_tuples_lookup` metrics
747+
with `{ operation = 'select', name = space_name }` labels.
697748

698749
Since `pairs` request behavior differs from any other crud request, its
699750
statistics collection also has specific behavior. Statistics (`select`
@@ -705,7 +756,10 @@ collector.
705756

706757
Statistics are preserved between package reloads. Statistics are preserved
707758
between [Tarantool Cartridge role reloads](https://www.tarantool.io/en/doc/latest/book/cartridge/cartridge_api/modules/cartridge.roles/#reload)
708-
if you use CRUD Cartridge roles.
759+
if you use CRUD Cartridge roles. Beware that metrics 0.12.0 and below do not
760+
support preserving stats between role reload
761+
(see [tarantool/metrics#334](https://github.com/tarantool/metrics/issues/334)),
762+
thus this feature will be unsupported for `metrics` driver.
709763

710764
## Cartridge roles
711765

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 Copy of 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)