Add support for multiple carbon hosts

Each carbon host will receive all metrics individually so redundancy can be achieved.
This commit is contained in:
cn 2017-03-01 23:10:09 +01:00
parent e1b7f809ca
commit 9dd92660b9
2 changed files with 46 additions and 43 deletions

View File

@ -37,7 +37,7 @@ Intermittent network errors while communicating with Graphite might leed to perm
lua_shared_dict metrics_graphite 128k;
lua_package_path ";;/opt/nginx-metrics-graphite/?.lua";
init_by_lua 'metrics_graphite = require("metrics_graphite").init("graphite.example.net", 300, "my.node.prefix")';
init_by_lua 'metrics_graphite = require("metrics_graphite").init({"graphite.example.net"}, 300, "my.node.prefix")';
init_worker_by_lua 'metrics_graphite:worker()';
```

View File

@ -2,10 +2,10 @@
local MetricsGraphite = {}
MetricsGraphite.__index = MetricsGraphite
function MetricsGraphite.init(carbon_host, interval, mbase)
function MetricsGraphite.init(carbon_hosts, interval, mbase)
local self = setmetatable({}, MetricsGraphite)
ngx.log(ngx.INFO, "nginx-metrics-graphite initializing on nginx version " .. ngx.config.nginx_version .. " with ngx_lua version " .. ngx.config.ngx_lua_version)
self.carbon_host = carbon_host
self.carbon_hosts = carbon_hosts
self.interval = interval
self.mbase = mbase
@ -86,16 +86,18 @@ function MetricsGraphite:worker()
end
-- then do the work which might incur delays
-- submit the metrics to each configured carbon host
for i,carbon_host in this.carbon_hosts do
local sock, err = ngx.socket.tcp()
if err then
ngx.log(ngx.ERR, "nginx-metrics-graphite callback failed to create carbon socket: ", err)
ngx.log(ngx.ERR, "nginx-metrics-graphite callback failed to create carbon host #" .. i .. " socket: ", err)
return
end
-- connect to carbon host with submission port via TCP
local ok, err = sock:connect(this.carbon_host, 2003)
local ok, err = sock:connect(carbon_host, 2003)
if not ok then
ngx.log(ngx.ERR, "nginx-metrics-graphite callback failed to connect carbon socket: ", err)
ngx.log(ngx.ERR, "nginx-metrics-graphite callback failed to connect carbon host #" .. i .. " socket: ", err)
return
end
@ -128,6 +130,7 @@ function MetricsGraphite:worker()
sock:close()
end
end
-- start first timer
local ok, err = ngx.timer.at(this.interval, callback)