Skip to content

Commit 805caa4

Browse files
graphite: multiple servers support
This patch adds the ability to send metrics to the multiple servers. Part of #TNTP-6584
1 parent 9c744c2 commit 805caa4

3 files changed

Lines changed: 98 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010

11+
- `graphite`: ability to send metrics to the multiple servers.
12+
1113
### Changed
1214

1315
### Fixed

metrics/plugins/graphite.lua

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,26 @@ function graphite.format_observation(prefix, obs)
3636
return graph
3737
end
3838

39+
local function create_fiber_name(opts)
40+
local fiber_name
41+
42+
if opts ~= nil then
43+
local prefix = opts.prefix or DEFAULT_PREFIX
44+
local host = opts.host or DEFAULT_HOST
45+
local port = opts.port or DEFAULT_PORT
46+
local send_interval = opts.send_interval or DEFAULT_SEND_INTERVAL
47+
48+
fiber_name = 'metrics_graphite_worker' .. '_' ..
49+
prefix .. '_' .. host .. '_' .. port .. '_' .. send_interval
50+
else
51+
fiber_name = 'metrics_graphite_worker'
52+
end
53+
54+
return fiber_name
55+
end
56+
3957
local function graphite_worker(opts)
40-
fiber.name('metrics_graphite_worker')
58+
fiber.name(create_fiber_name(opts))
4159

4260
while true do
4361
metrics.invoke_callbacks()
@@ -73,7 +91,7 @@ function graphite.init(opts)
7391
local send_interval = opts.send_interval or DEFAULT_SEND_INTERVAL
7492

7593
fun.iter(fiber.info()):
76-
filter(function(_, x) return x.name == 'metrics_graphite_worker' end):
94+
filter(function(_, x) return x.name == create_fiber_name(opts) end):
7795
each(function(x) fiber.kill(x) end)
7896

7997
fiber.create(graphite_worker, {
@@ -85,4 +103,17 @@ function graphite.init(opts)
85103
})
86104
end
87105

106+
function graphite.stop(opts)
107+
checks {
108+
prefix = '?string',
109+
host = '?string',
110+
port = '?number',
111+
send_interval = '?number'
112+
}
113+
114+
fun.iter(fiber.info()):
115+
filter(function(_, x) return x.name == create_fiber_name(opts) end):
116+
each(function(x) fiber.kill(x) end)
117+
end
118+
88119
return graphite

test/plugins/graphite_test.lua

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ g.after_each(function(cg)
4141
local metrics = require('metrics')
4242
-- Delete all collectors and global labels
4343
metrics.clear()
44+
4445
fun.iter(fiber.info()):
45-
filter(function(_, x) return x.name == 'metrics_graphite_worker' end):
46+
filter(function(_, x) return string.find(x.name, 'metrics_graphite_worker') end):
4647
each(function(x) fiber.kill(x) end)
4748
fiber.yield() -- let cancelled fibers disappear from fiber.info()
4849
end)
@@ -171,17 +172,19 @@ g.test_graphite_kills_previous_fibers_on_init = function(cg)
171172

172173
local function mock_graphite_worker()
173174
fiber.create(function()
174-
fiber.name('metrics_graphite_worker')
175+
fiber.name('metrics_graphite_worker_tarantool_127.0.0.1_2003_2')
175176
fiber.sleep(math.huge)
176177
end)
177178
end
178179

179180
local function count_workers()
180181
return fun.iter(fiber.info()):
181-
filter(function(_, x) return x.name == 'metrics_graphite_worker' end):
182+
filter(function(_, x) return string.find(x.name, 'metrics_graphite_worker') end):
182183
length()
183184
end
184185

186+
graphite.stop()
187+
185188
t.assert_equals(count_workers(), 0)
186189
mock_graphite_worker()
187190
mock_graphite_worker()
@@ -192,3 +195,60 @@ g.test_graphite_kills_previous_fibers_on_init = function(cg)
192195
t.assert_equals(count_workers(), 1)
193196
end)
194197
end
198+
199+
g.test_graphite_stop_default_fibers = function(cg)
200+
cg.server:exec(function()
201+
local fiber = require('fiber')
202+
local fun = require('fun')
203+
local graphite = require('metrics.plugins.graphite')
204+
205+
local function count_workers()
206+
return fun.iter(fiber.info()):
207+
filter(function(_, x) return string.find(x.name, 'metrics_graphite_worker') end):
208+
length()
209+
end
210+
211+
t.assert_equals(count_workers(), 0)
212+
213+
graphite.init({})
214+
t.assert_equals(count_workers(), 1)
215+
216+
graphite.stop({})
217+
fiber.yield() -- let cancelled fibers disappear from fiber.info()
218+
t.assert_equals(count_workers(), 0)
219+
end)
220+
end
221+
222+
g.test_graphite_stop_custom_fiber = function(cg)
223+
cg.server:exec(function()
224+
local fiber = require('fiber')
225+
local fun = require('fun')
226+
local graphite = require('metrics.plugins.graphite')
227+
228+
local function count_workers()
229+
return fun.iter(fiber.info()):
230+
filter(function(_, x) return string.find(x.name, 'metrics_graphite_worker') end):
231+
length()
232+
end
233+
234+
local opts = {
235+
prefix = "master",
236+
host = "127.0.0.1",
237+
port = 3333,
238+
send_interval = 1,
239+
}
240+
241+
t.assert_equals(count_workers(), 0)
242+
243+
graphite.init(opts)
244+
245+
t.assert_equals(count_workers(), 1)
246+
247+
graphite.stop()
248+
t.assert_equals(count_workers(), 1) -- fiber still works
249+
250+
graphite.stop(opts)
251+
fiber.yield() -- let cancelled fibers disappear from fiber.info()
252+
t.assert_equals(count_workers(), 0)
253+
end)
254+
end

0 commit comments

Comments
 (0)