通过 LUA 记录 Envoy 请求日志

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
function trim_prefix(s, p)
return (s:sub(0, #p) == p) and s:sub(#p + 1) or s
end

function envoy_on_request(request_handle)
local idx = 0
for chunk in request_handle:bodyChunks() do
local len = chunk:length()
if len == 0 then
break
end
local body = chunk:getBytes(idx, len)
idx = idx + len
local hds = {}
for key, value in pairs(request_handle:headers()) do
hds["y-" .. trim_prefix(key, ":")] = value
end
hds[":method"] = "POST"
hds[":path"] = "/log"
hds[":authority"] = "envoy"
request_handle:httpCall("http-log-service", hds, body, 10000, true)
end
end

function envoy_on_response(response_handle)
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
static_resources:
clusters:
- name: http-log-service
type: STRICT_DNS
connect_timeout: 10s
load_assignment:
cluster_name: http-log-service
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 127.0.0.1
port_value: 9709