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,47 +86,50 @@ function MetricsGraphite:worker() | |||||||
|     end |     end | ||||||
|  |  | ||||||
|     -- then do the work which might incur delays |     -- then do the work which might incur delays | ||||||
|     local sock, err = ngx.socket.tcp() |     -- submit the metrics to each configured carbon host | ||||||
|     if err then |     for i,carbon_host in this.carbon_hosts do | ||||||
|       ngx.log(ngx.ERR, "nginx-metrics-graphite callback failed to create carbon socket: ", err) |       local sock, err = ngx.socket.tcp() | ||||||
|       return |       if err then | ||||||
|  |         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(carbon_host, 2003) | ||||||
|  |       if not ok then | ||||||
|  |         ngx.log(ngx.ERR, "nginx-metrics-graphite callback failed to connect carbon host #" .. i .. " socket: ", err) | ||||||
|  |         return | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       local avg_request_time = this.stats:get("request_time_sum") / this.stats:get("request_time_num") | ||||||
|  |       self.stats:set("request_time_sum", 0) | ||||||
|  |       self.stats:set("request_time_num", 0) | ||||||
|  |  | ||||||
|  |       -- submit metrics | ||||||
|  |       sock:send(this.mbase .. ".nginx_metrics.num_requests " .. this.stats:get("requests") .. " " .. ngx.time() .. "\n") | ||||||
|  |       sock:send(this.mbase .. ".nginx_metrics.num_upstream_requests " .. this.stats:get("upstream_requests") .. " " .. ngx.time() .. "\n") | ||||||
|  |       sock:send(this.mbase .. ".nginx_metrics.num_gzip_requests " .. this.stats:get("gzip_requests") .. " " .. ngx.time() .. "\n") | ||||||
|  |       sock:send(this.mbase .. ".nginx_metrics.num_ssl_requests " .. this.stats:get("ssl_requests") .. " " .. ngx.time() .. "\n") | ||||||
|  |  | ||||||
|  |       sock:send(this.mbase .. ".nginx_metrics.acc_request_length " .. this.stats:get("request_length") .. " " .. ngx.time() .. "\n") | ||||||
|  |       sock:send(this.mbase .. ".nginx_metrics.acc_bytes_sent " .. this.stats:get("bytes_sent") .. " " .. ngx.time() .. "\n") | ||||||
|  |  | ||||||
|  |       sock:send(this.mbase .. ".nginx_metrics.avg_request_time " .. avg_request_time .. " " .. ngx.time() .. "\n") | ||||||
|  |  | ||||||
|  |       for k,v in pairs(self.query_status) do | ||||||
|  |         sock:send(this.mbase .. ".nginx_metrics.num_" .. k .. " " .. this.stats:get(k) .. " " .. ngx.time() .. "\n") | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       for k,v in pairs(self.query_method) do | ||||||
|  |         sock:send(this.mbase .. ".nginx_metrics.num_" .. k .. " " .. this.stats:get(k) .. " " .. ngx.time() .. "\n") | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       for k,v in pairs(self.query_http) do | ||||||
|  |         sock:send(this.mbase .. ".nginx_metrics.num_" .. k .. " " .. this.stats:get(k) .. " " .. ngx.time() .. "\n") | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       sock:close() | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     -- connect to carbon host with submission port via TCP |  | ||||||
|     local ok, err = sock:connect(this.carbon_host, 2003) |  | ||||||
|     if not ok then |  | ||||||
|       ngx.log(ngx.ERR, "nginx-metrics-graphite callback failed to connect carbon socket: ", err) |  | ||||||
|       return |  | ||||||
|     end |  | ||||||
|  |  | ||||||
|     local avg_request_time = this.stats:get("request_time_sum") / this.stats:get("request_time_num") |  | ||||||
|     self.stats:set("request_time_sum", 0) |  | ||||||
|     self.stats:set("request_time_num", 0) |  | ||||||
|  |  | ||||||
|     -- submit metrics |  | ||||||
|     sock:send(this.mbase .. ".nginx_metrics.num_requests " .. this.stats:get("requests") .. " " .. ngx.time() .. "\n") |  | ||||||
|     sock:send(this.mbase .. ".nginx_metrics.num_upstream_requests " .. this.stats:get("upstream_requests") .. " " .. ngx.time() .. "\n") |  | ||||||
|     sock:send(this.mbase .. ".nginx_metrics.num_gzip_requests " .. this.stats:get("gzip_requests") .. " " .. ngx.time() .. "\n") |  | ||||||
|     sock:send(this.mbase .. ".nginx_metrics.num_ssl_requests " .. this.stats:get("ssl_requests") .. " " .. ngx.time() .. "\n") |  | ||||||
|  |  | ||||||
|     sock:send(this.mbase .. ".nginx_metrics.acc_request_length " .. this.stats:get("request_length") .. " " .. ngx.time() .. "\n") |  | ||||||
|     sock:send(this.mbase .. ".nginx_metrics.acc_bytes_sent " .. this.stats:get("bytes_sent") .. " " .. ngx.time() .. "\n") |  | ||||||
|  |  | ||||||
|     sock:send(this.mbase .. ".nginx_metrics.avg_request_time " .. avg_request_time .. " " .. ngx.time() .. "\n") |  | ||||||
|  |  | ||||||
|     for k,v in pairs(self.query_status) do |  | ||||||
|       sock:send(this.mbase .. ".nginx_metrics.num_" .. k .. " " .. this.stats:get(k) .. " " .. ngx.time() .. "\n") |  | ||||||
|     end |  | ||||||
|  |  | ||||||
|     for k,v in pairs(self.query_method) do |  | ||||||
|       sock:send(this.mbase .. ".nginx_metrics.num_" .. k .. " " .. this.stats:get(k) .. " " .. ngx.time() .. "\n") |  | ||||||
|     end |  | ||||||
|  |  | ||||||
|     for k,v in pairs(self.query_http) do |  | ||||||
|       sock:send(this.mbase .. ".nginx_metrics.num_" .. k .. " " .. this.stats:get(k) .. " " .. ngx.time() .. "\n") |  | ||||||
|     end |  | ||||||
|  |  | ||||||
|     sock:close() |  | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   -- start first timer |   -- start first timer | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user