mirror of
				https://github.com/cmur2/nginx-metrics-graphite.git
				synced 2025-10-31 00:25:07 +01:00 
			
		
		
		
	Add support for multiple carbon hosts
Each carbon host will receive all metrics individually so redundancy can be achieved.
This commit is contained in:
		| @@ -37,7 +37,7 @@ Intermittent network errors while communicating with Graphite might leed to perm | |||||||
|  |  | ||||||
|     lua_shared_dict metrics_graphite 128k; |     lua_shared_dict metrics_graphite 128k; | ||||||
|     lua_package_path ";;/opt/nginx-metrics-graphite/?.lua"; |     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()'; |     init_worker_by_lua 'metrics_graphite:worker()'; | ||||||
|     ``` |     ``` | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,10 +2,10 @@ | |||||||
| local MetricsGraphite = {} | local MetricsGraphite = {} | ||||||
| MetricsGraphite.__index = MetricsGraphite | MetricsGraphite.__index = MetricsGraphite | ||||||
|  |  | ||||||
| function MetricsGraphite.init(carbon_host, interval, mbase) | function MetricsGraphite.init(carbon_hosts, interval, mbase) | ||||||
|   local self = setmetatable({}, MetricsGraphite) |   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) |   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.interval = interval | ||||||
|   self.mbase = mbase |   self.mbase = mbase | ||||||
|  |  | ||||||
| @@ -86,16 +86,18 @@ function MetricsGraphite:worker() | |||||||
|     end |     end | ||||||
|  |  | ||||||
|     -- then do the work which might incur delays |     -- 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() |       local sock, err = ngx.socket.tcp() | ||||||
|       if err then |       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 |         return | ||||||
|       end |       end | ||||||
|  |  | ||||||
|       -- connect to carbon host with submission port via TCP |       -- 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 |       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 |         return | ||||||
|       end |       end | ||||||
|  |  | ||||||
| @@ -128,6 +130,7 @@ function MetricsGraphite:worker() | |||||||
|  |  | ||||||
|       sock:close() |       sock:close() | ||||||
|     end |     end | ||||||
|  |   end | ||||||
|  |  | ||||||
|   -- start first timer |   -- start first timer | ||||||
|   local ok, err = ngx.timer.at(this.interval, callback) |   local ok, err = ngx.timer.at(this.interval, callback) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user