Comparison

mod_statistics_statsd/mod_statistics_statsd.lua @ 1622:b59812aaabad

mod_statistics_statsd: Module for pushing from util.statistics (0.10) to statsd
author Matthew Wild <mwild1@gmail.com>
date Tue, 10 Mar 2015 14:44:55 +0000
child 1656:98a186874806
comparison
equal deleted inserted replaced
1621:738e9874a374 1622:b59812aaabad
1 local statsmanager = require "core.statsmanager";
2 local udp = require "socket".udp();
3
4 local server = module:get_option_string("statsd_server_ip", "127.0.0.1");
5 local server_port = module:get_option_number("statsd_server_port", 8124);
6 local max_datagram_size = module:get_option_number("statds_packet_size", 512);
7
8 function push_stats(stats, meta)
9 local metric_strings, remaining_bytes = {}, max_datagram_size;
10 for name, value in pairs(stats) do
11 local value_meta = meta[name];
12 log("warn", "%s %s", name, tostring(value_meta));
13 --if not value_meta then
14 -- Simple value (gauge)
15 local metric_string = ("%s|%d|g"):format(name, value);
16 if #metric_string > remaining_bytes then
17 udp:sendto(table.concat(metric_strings, "\n"), server, server_port);
18 metric_strings, remaining_bytes = {}, max_datagram_size;
19 end
20 table.insert(metric_strings, metric_string);
21 remaining_bytes = remaining_bytes - (#metric_string + 1); -- +1 for newline
22 --end
23 end
24 if #metric_strings > 0 then
25 udp:sendto(table.concat(metric_strings, "\n"), server, server_port);
26 end
27 end
28
29 module:hook_global("stats-updated", function (event)
30 push_stats(event.changed_stats, event.stats_extra);
31 end);
32
33 function module.load()
34 local all, changed, extra = statsmanager.get_stats();
35 push_stats(all, extra);
36 end