Skip to content
This repository was archived by the owner on Feb 11, 2022. It is now read-only.

Commit e2aec5b

Browse files
author
Konstantin Nazarov
committed
Add Tarantool metrics
1 parent f10bff9 commit e2aec5b

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

example.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ prometheus = require('tarantool-prometheus')
55
fiber = require('fiber')
66

77
box.cfg{}
8+
prometheus.init()
89

910
httpd = http.new('0.0.0.0', 8080)
1011

tarantool-metrics.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env tarantool
2+
3+
local prometheus = require('tarantool-prometheus')
4+
5+
local memory_limit_bytes = prometheus.gauge(
6+
'tarantool_memory_limit_bytes',
7+
'Maximum amount of memory Tarantool can use')
8+
local memory_used_bytes = prometheus.gauge(
9+
'tarantool_memory_used_bytes',
10+
'Amount of memory currently used by Tarantool')
11+
local tuples_memory_bytes = prometheus.gauge(
12+
'tarantool_tuples_memory_bytes',
13+
'Amount of memory allocated for Tarantool tuples')
14+
local system_memory_bytes = prometheus.gauge(
15+
'tarantool_system_memory_bytes',
16+
'Amount of memory used by Tarantool indexes and system')
17+
18+
local requests_total = prometheus.gauge(
19+
'tarantool_requests_total',
20+
'Total number of requests by request type',
21+
{'request_type'})
22+
23+
local uptime_seconds = prometheus.gauge(
24+
'tarantool_uptime_seconds',
25+
'Number of seconds since the server started')
26+
27+
local function measure_tarantool_memory_usage()
28+
local slabs = box.slab.info()
29+
local memory_limit = slabs.quota_size
30+
local memory_used = slabs.quota_used
31+
local tuples_memory = slabs.arena_used
32+
local system_memory = memory_used - tuples_memory
33+
34+
memory_limit_bytes:set(memory_limit)
35+
memory_used_bytes:set(memory_used)
36+
tuples_memory_bytes:set(tuples_memory)
37+
system_memory_bytes:set(system_memory)
38+
end
39+
40+
local function measure_tarantool_request_stats()
41+
local stat = box.stat()
42+
local request_types = {'delete', 'select', 'insert', 'eval', 'call',
43+
'replace', 'upsert', 'auth', 'error', 'update'}
44+
45+
for _, request_type in ipairs(request_types) do
46+
requests_total:set(stat[string.upper(request_type)].total,
47+
{request_type})
48+
end
49+
end
50+
51+
local function measure_tarantool_uptime()
52+
uptime_seconds:set(box.info.uptime)
53+
end
54+
55+
local function measure_tarantool_metrics()
56+
measure_tarantool_memory_usage()
57+
measure_tarantool_request_stats()
58+
measure_tarantool_uptime()
59+
end
60+
61+
return {measure_tarantool_metrics=measure_tarantool_metrics}

tarantool-prometheus.lua

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function Registry.new()
1414
local obj = {}
1515
setmetatable(obj, Registry)
1616
obj.collectors = {}
17+
obj.callbacks = {}
1718
return obj
1819
end
1920

@@ -32,6 +33,10 @@ function Registry:unregister(collector)
3233
end
3334

3435
function Registry:collect()
36+
for _, registered_callback in ipairs(self.callbacks) do
37+
registered_callback()
38+
end
39+
3540
local result = {}
3641
for _, collector in pairs(self.collectors) do
3742
for _, metric in ipairs(collector:collect()) do
@@ -41,6 +46,18 @@ function Registry:collect()
4146
return result
4247
end
4348

49+
function Registry:register_callback(callback)
50+
local found = false
51+
for _, registered_callback in ipairs(self.callbacks) do
52+
if registered_callback == calback then
53+
found = true
54+
end
55+
end
56+
if not found then
57+
table.insert(self.callbacks, callback)
58+
end
59+
end
60+
4461
local function get_registry()
4562
if not REGISTRY then
4663
REGISTRY = Registry.new()
@@ -55,6 +72,11 @@ local function register(collector)
5572
return collector
5673
end
5774

75+
local function register_callback(callback)
76+
local registry = get_registry()
77+
registry:register_callback(callback)
78+
end
79+
5880
function zip(lhs, rhs)
5981
if lhs == nil or rhs == nil then
6082
return {}
@@ -347,13 +369,21 @@ local function collect_http()
347369
end
348370

349371
local function clear()
350-
registry = get_registry()
372+
local registry = get_registry()
351373
registry.collectors = {}
374+
registry.callbacks = {}
375+
end
376+
377+
local function init()
378+
local registry = get_registry()
379+
local tarantool_metrics = require('tarantool-metrics')
380+
registry:register_callback(tarantool_metrics.measure_tarantool_metrics)
352381
end
353382

354383
return {counter=counter,
355384
gauge=gauge,
356385
histogram=histogram,
357386
collect=collect,
358387
collect_http=collect_http,
359-
clear=clear}
388+
clear=clear,
389+
init=init}

0 commit comments

Comments
 (0)