/
handler.lua
86 lines (66 loc) 路 1.96 KB
/
handler.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
local cjson = require "cjson"
local sandbox = require "kong.tools.sandbox".sandbox
local kong_meta = require "kong.meta"
local kong = kong
local ngx = ngx
local timer_at = ngx.timer.at
local sandbox_opts = { env = { kong = kong, ngx = ngx } }
local function log(premature, conf, message)
if premature then
return
end
local host = conf.host
local port = conf.port
local timeout = conf.timeout
local keepalive = conf.keepalive
local sock = ngx.socket.tcp()
sock:settimeout(timeout)
local ok, err = sock:connect(host, port)
if not ok then
kong.log.err("failed to connect to ", host, ":", tostring(port), ": ", err)
sock:close()
return
end
local times, err = sock:getreusedtimes()
if not times then
kong.log.err("failed to get socket reused time to ", host, ":", tostring(port), ": ", err)
sock:close()
return
end
if conf.tls and times == 0 then
ok, err = sock:sslhandshake(false, conf.tls_sni, false)
if not ok then
kong.log.err("failed to perform TLS handshake to ", host, ":", port, ": ", err)
sock:close()
return
end
end
ok, err = sock:send(cjson.encode(message) .. "\n")
if not ok then
kong.log.err("failed to send data to ", host, ":", tostring(port), ": ", err)
end
ok, err = sock:setkeepalive(keepalive)
if not ok then
kong.log.err("failed to keepalive to ", host, ":", tostring(port), ": ", err)
sock:close()
return
end
end
local TcpLogHandler = {
PRIORITY = 7,
VERSION = kong_meta.version,
}
function TcpLogHandler:log(conf)
if conf.custom_fields_by_lua then
local set_serialize_value = kong.log.set_serialize_value
for key, expression in pairs(conf.custom_fields_by_lua) do
set_serialize_value(key, sandbox(expression, sandbox_opts)())
end
end
local message = kong.log.serialize()
local ok, err = timer_at(0, log, conf, message)
if not ok then
kong.log.err("failed to create timer: ", err)
end
end
return TcpLogHandler