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