Skip to content

Commit f3b4472

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 be9cb46 commit f3b4472

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 (#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

@@ -748,9 +765,39 @@ and `borders` (for `min` and `max` calls).
748765
Each operation section contains of different collectors
749766
for success calls and error (both error throw and `nil, err`)
750767
returns. `count` is total requests count since instance start
751-
or stats restart. `latency` is average time of requests execution,
768+
or stats restart. `latency` is 0.99 quantile of request execution
769+
time if `metrics` driver used and quantiles enabled,
770+
otherwise `latency` is total average.
752771
`time` is the total time of requests execution.
753772

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

771822
Since `pairs` request behavior differs from any other crud request, its
772823
statistics collection also has specific behavior. Statistics (`select`
@@ -778,7 +829,10 @@ collector.
778829

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

783837
## Cartridge roles
784838

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)