ingress-nginx-helm/rootfs/etc/nginx/lua/monitor.lua

89 lines
2.3 KiB
Lua
Raw Normal View History

local socket = ngx.socket.tcp
2019-01-18 03:46:14 +00:00
local cjson = require("cjson.safe")
2018-06-14 02:54:09 +00:00
local assert = assert
local new_tab = require "table.new"
local clear_tab = require "table.clear"
local clone_tab = require "table.clone"
local nkeys = require "table.nkeys"
2018-06-14 02:54:09 +00:00
2018-08-18 00:01:50 +00:00
-- if an Nginx worker processes more than (MAX_BATCH_SIZE/FLUSH_INTERVAL) RPS then it will start dropping metrics
local MAX_BATCH_SIZE = 10000
local FLUSH_INTERVAL = 1 -- second
local metrics_batch = new_tab(MAX_BATCH_SIZE, 0)
2018-06-14 02:54:09 +00:00
local _M = {}
2018-08-18 00:01:50 +00:00
local function send(payload)
2018-06-14 02:54:09 +00:00
local s = assert(socket())
2018-08-18 00:01:50 +00:00
assert(s:connect("unix:/tmp/prometheus-nginx.socket"))
assert(s:send(payload))
2018-06-14 02:54:09 +00:00
assert(s:close())
end
2018-08-18 00:01:50 +00:00
local function metrics()
return {
2018-06-14 02:54:09 +00:00
host = ngx.var.host or "-",
2018-08-18 00:01:50 +00:00
namespace = ngx.var.namespace or "-",
ingress = ngx.var.ingress_name or "-",
service = ngx.var.service_name or "-",
path = ngx.var.location_path or "-",
2018-07-07 17:46:18 +00:00
2018-08-18 00:01:50 +00:00
method = ngx.var.request_method or "-",
2018-07-07 17:46:18 +00:00
status = ngx.var.status or "-",
2018-06-14 02:54:09 +00:00
requestLength = tonumber(ngx.var.request_length) or -1,
requestTime = tonumber(ngx.var.request_time) or -1,
2018-07-07 17:46:18 +00:00
responseLength = tonumber(ngx.var.bytes_sent) or -1,
upstreamLatency = tonumber(ngx.var.upstream_connect_time) or -1,
2018-06-14 02:54:09 +00:00
upstreamResponseTime = tonumber(ngx.var.upstream_response_time) or -1,
2018-07-07 17:46:18 +00:00
upstreamResponseLength = tonumber(ngx.var.upstream_response_length) or -1,
--upstreamStatus = ngx.var.upstream_status or "-",
2018-08-18 00:01:50 +00:00
}
end
2018-07-07 17:46:18 +00:00
2018-08-18 00:01:50 +00:00
local function flush(premature)
if premature then
return
end
if #metrics_batch == 0 then
return
end
local current_metrics_batch = clone_tab(metrics_batch)
clear_tab(metrics_batch)
2018-08-18 00:01:50 +00:00
2019-01-18 03:46:14 +00:00
local payload, err = cjson.encode(current_metrics_batch)
if not payload then
ngx.log(ngx.ERR, "error while encoding metrics: ", err)
2018-08-18 00:01:50 +00:00
return
end
send(payload)
end
function _M.init_worker()
local _, err = ngx.timer.every(FLUSH_INTERVAL, flush)
if err then
ngx.log(ngx.ERR, string.format("error when setting up timer.every: %s", tostring(err)))
end
2018-06-14 02:54:09 +00:00
end
function _M.call()
local metrics_size = nkeys(metrics_batch)
if metrics_size >= MAX_BATCH_SIZE then
ngx.log(ngx.WARN, "omitting metrics for the request, current batch is full")
2018-06-14 02:54:09 +00:00
return
end
2018-08-18 00:01:50 +00:00
metrics_batch[metrics_size + 1] = metrics()
2018-08-18 00:01:50 +00:00
end
if _TEST then
_M.flush = flush
_M.get_metrics_batch = function() return metrics_batch end
2018-06-14 02:54:09 +00:00
end
return _M