Skip to content

Commit a11724b

Browse files
Added docs and unregister callback function
1 parent 8a5b7c1 commit a11724b

File tree

7 files changed

+111
-19
lines changed

7 files changed

+111
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
### Added
2020
- `tnt_clock_delta` metric to compute clock difference on instances
2121
- set custom global labels in config and with `set_labels` function [#259](https://github.com/tarantool/metrics/issues/259)
22+
- allow to include and exclude default metrics in config and in `enable_default_metrics` function
23+
[#222](https://github.com/tarantool/metrics/issues/222)
24+
25+
### Deprecated
26+
- `enable_cartridge_metrics` function
2227

2328
## [0.9.0] - 2021-05-28
2429
### Fixed

cartridge/roles/metrics.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ local function apply_config(conf)
157157
end
158158
end
159159
apply_routes(paths)
160-
metrics.clear()
161160
metrics.enable_default_metrics(metrics_conf.include, metrics_conf.exclude)
162-
metrics.enable_cartridge_metrics()
163161
set_labels(metrics_conf['global-labels'])
164162
end
165163

@@ -194,7 +192,6 @@ end
194192
local function init()
195193
set_labels(metrics_vars.custom_labels)
196194
metrics.enable_default_metrics()
197-
metrics.enable_cartridge_metrics()
198195
local current_paths = table.copy(metrics_vars.config)
199196
for path, format in pairs(metrics_vars.default) do
200197
if current_paths[path] == nil then

doc/monitoring/api_reference.rst

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,33 @@ You can also set global labels by calling
266266
Metrics functions
267267
-------------------------------------------------------------------------------
268268

269-
.. function:: enable_default_metrics()
269+
.. function:: enable_default_metrics(include, exclude)
270270

271-
Enables Tarantool metrics collections. See :ref:`metrics reference <metrics-reference>`
272-
for details.
271+
Enables Tarantool metrics collections.
273272

274-
.. function:: enable_cartridge_metrics()
273+
:param table include: Table containing names of default metrics which you need to enable.
275274

276-
Enables Cartridge metrics collections. See :ref:`metrics reference <metrics-cartridge>`
277-
for details.
275+
:param table exclude: Table containing names of default metrics which you need to exclude.
276+
277+
Default metrics names:
278+
279+
* "network"
280+
* "operations"
281+
* "system"
282+
* "replicas"
283+
* "info"
284+
* "slab"
285+
* "runtime"
286+
* "memory"
287+
* "spaces"
288+
* "fibers"
289+
* "cpu"
290+
* "vinyl"
291+
* "luajit"
292+
* "cartridge_issues"
293+
* "clock"
294+
295+
See :ref:`metrics reference <metrics-reference>` for details.
278296

279297
.. function:: metrics.set_global_labels(label_pairs)
280298

@@ -301,6 +319,15 @@ Metrics functions
301319

302320
Most common usage is for gauge metrics updates.
303321

322+
.. function:: unregister_callback(callback)
323+
324+
Unregisters a function ``callback`` which will be called right before metrics
325+
collection on plugin export.
326+
327+
:param function callback: Function which takes no parameters.
328+
329+
Most common usage is for unregister enabled callbacks.
330+
304331
.. _collecting-http-statistics:
305332

306333
-------------------------------------------------------------------------------

doc/monitoring/getting_started.rst

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,36 @@ via configuration.
217217
.. code-block:: lua
218218
219219
local metrics = require('cartridge.roles.metrics')
220-
metrics.set_labels({ ['my-custom-label'] = 'label-value'} )
220+
metrics.set_labels({ ['my-custom-label'] = 'label-value' })
221+
222+
#. To choose which default metrics are exported, you may use the following configuration.
223+
224+
When you add include section, only metrics from this section are exported:
225+
226+
.. code-block:: yaml
227+
228+
metrics:
229+
export:
230+
- path: '/metrics'
231+
format: 'json'
232+
# export only vinyl, luajit and memory metrics:
233+
include:
234+
- vinyl
235+
- luajit
236+
- memory
237+
238+
When you add exclude section, metrics from this section are removed from default metrics list:
239+
240+
.. code-block:: yaml
241+
242+
metrics:
243+
export:
244+
- path: '/metrics'
245+
format: 'json'
246+
# export all metrics except vinyl, luajit and memory:
247+
exclude:
248+
- vinyl
249+
- luajit
250+
- memory
251+
252+
You can see list of defaul metrics in :ref:`API reference <metrics-functions>`.

metrics/default_metrics/tarantool/init.lua

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,38 @@ local default_metrics = {
1414
cpu = require('metrics.default_metrics.tarantool.cpu'),
1515
vinyl = require('metrics.tarantool.vinyl'),
1616
luajit = require('metrics.tarantool.luajit'),
17+
cartridge_issues = require('metrics.cartridge.issues'),
18+
clock = require('metrics.cartridge.clock'),
1719
}
1820

1921
local function enable(include, exclude)
22+
include = include or {}
23+
exclude = exclude or {}
24+
2025
local exclude_map = {}
21-
for _, name in ipairs(exclude or {}) do
26+
for _, name in ipairs(exclude) do
2227
exclude_map[name] = true
2328
end
24-
if include then
25-
for _, name in ipairs(include) do
26-
metrics.register_callback(default_metrics[name].update)
27-
end
28-
else
29-
for name, metric in pairs(default_metrics) do
30-
if not exclude_map[name] then
31-
metrics.register_callback(metric.update)
29+
local include_map = {}
30+
for _, name in ipairs(include) do
31+
include_map[name] = true
32+
end
33+
34+
for name, value in pairs(default_metrics) do
35+
if #include > 0 then
36+
if include_map[name] ~= nil then
37+
metrics.register_callback(value.update)
38+
else
39+
metrics.unregister_callback(value.update)
40+
end
41+
elseif #exclude > 0 then
42+
if exclude_map[name] ~= nil then
43+
metrics.unregister_callback(value.update)
44+
else
45+
metrics.register_callback(value.update)
3246
end
47+
else
48+
metrics.register_callback(value.update)
3349
end
3450
end
3551
end

metrics/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- vim: ts=4:sw=4:sts=4:expandtab
22

33
local checks = require('checks')
4+
local log = require('log')
45

56
local Registry = require('metrics.registry')
67

@@ -23,6 +24,10 @@ local function register_callback(...)
2324
return registry:register_callback(...)
2425
end
2526

27+
local function unregister_callback(...)
28+
return registry:unregister_callback(...)
29+
end
30+
2631
local function invoke_callbacks()
2732
return registry:invoke_callbacks()
2833
end
@@ -109,12 +114,14 @@ return {
109114
clear = clear,
110115
collectors = collectors,
111116
register_callback = register_callback,
117+
unregister_callback = unregister_callback,
112118
invoke_callbacks = invoke_callbacks,
113119
set_global_labels = set_global_labels,
114120
enable_default_metrics = function(include, exclude)
115121
require('metrics.default_metrics.tarantool').enable(include, exclude)
116122
end,
117123
enable_cartridge_metrics = function()
124+
log.warn('metrics.enable_cartridge_metrics() is deprecated. Use metrics.enable_default_metrics() instead.')
118125
return require('metrics.cartridge').enable()
119126
end,
120127
http_middleware = require('metrics.http_middleware'),

metrics/registry.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ function Registry:register_callback(callback)
7070
end
7171
end
7272

73+
function Registry:unregister_callback(callback)
74+
for i, registered_callback in ipairs(self.callbacks) do
75+
if registered_callback == callback then
76+
table.remove(self.callbacks, i)
77+
end
78+
end
79+
end
80+
7381
function Registry:set_labels(label_pairs)
7482
self.label_pairs = table.copy(label_pairs)
7583
end

0 commit comments

Comments
 (0)