feature: add option to include request body in log util (#1545)

This commit is contained in:
Nirojan Selvanathan 2020-05-29 10:15:16 +02:00 committed by GitHub
parent 6cf21d3c2f
commit 39ef6fb2eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 7 deletions

View File

@ -36,6 +36,7 @@ local schema = {
buffer_duration = {type = "integer", minimum = 1, default = 60},
inactive_timeout = {type = "integer", minimum = 1, default = 5},
batch_max_size = {type = "integer", minimum = 1, default = 1000},
include_req_body = {type = "boolean", default = false}
},
required = {"uri"}
}
@ -121,7 +122,7 @@ end
function _M.log(conf)
local entry = log_util.get_full_log(ngx)
local entry = log_util.get_full_log(ngx, conf)
if not entry.route_id then
core.log.error("failed to obtain the route id for http logger")

View File

@ -44,6 +44,7 @@ local schema = {
buffer_duration = {type = "integer", minimum = 1, default = 60},
inactive_timeout = {type = "integer", minimum = 1, default = 5},
batch_max_size = {type = "integer", minimum = 1, default = 1000},
include_req_body = {type = "boolean", default = false}
},
required = {"broker_list", "kafka_topic", "key"}
}
@ -111,7 +112,7 @@ end
function _M.log(conf)
local entry = log_util.get_full_log(ngx)
local entry = log_util.get_full_log(ngx, conf)
if not entry.route_id then
core.log.error("failed to obtain the route id for kafka logger")

View File

@ -42,6 +42,7 @@ local schema = {
tls = {type = "boolean", default = false},
batch_max_size = {type = "integer", minimum = 1, default = 1000},
buffer_duration = {type = "integer", minimum = 1, default = 60},
include_req_body = {type = "boolean", default = false}
},
required = {"host", "port"}
}
@ -127,7 +128,7 @@ end
-- log phase in APISIX
function _M.log(conf)
local entry = log_util.get_full_log(ngx)
local entry = log_util.get_full_log(ngx, conf)
if not entry.route_id then
core.log.error("failed to obtain the route id for sys logger")

View File

@ -40,6 +40,7 @@ local schema = {
buffer_duration = {type = "integer", minimum = 1, default = 60},
inactive_timeout = {type = "integer", minimum = 1, default = 5},
batch_max_size = {type = "integer", minimum = 1, default = 1000},
include_req_body = {type = "boolean", default = false}
},
required = {"host", "port"}
}
@ -115,7 +116,7 @@ end
function _M.log(conf)
local entry = log_util.get_full_log(ngx)
local entry = log_util.get_full_log(ngx, conf)
if not entry.route_id then
core.log.error("failed to obtain the route id for tcp logger")

View File

@ -36,6 +36,7 @@ local schema = {
buffer_duration = {type = "integer", minimum = 1, default = 60},
inactive_timeout = {type = "integer", minimum = 1, default = 5},
batch_max_size = {type = "integer", minimum = 1, default = 1000},
include_req_body = {type = "boolean", default = false}
},
required = {"host", "port"}
}
@ -98,7 +99,7 @@ end
function _M.log(conf)
local entry = log_util.get_full_log(ngx)
local entry = log_util.get_full_log(ngx, conf)
if not entry.route_id then
core.log.error("failed to obtain the route id for udp logger")

View File

@ -18,7 +18,7 @@ local core = require("apisix.core")
local _M = {}
local function get_full_log(ngx)
local function get_full_log(ngx, conf)
local ctx = ngx.ctx.api_ctx
local var = ctx.var
local service_id
@ -34,7 +34,7 @@ local function get_full_log(ngx)
service_id = var.host
end
return {
local log = {
request = {
url = url,
uri = var.request_uri,
@ -56,6 +56,20 @@ local function get_full_log(ngx)
start_time = ngx.req.start_time() * 1000,
latency = (ngx.now() - ngx.req.start_time()) * 1000
}
if conf.include_req_body then
local body = ngx.req.get_body_data()
if body then
log.request.body = body
else
local body_file = ngx.req.get_body_file()
if body_file then
log.request.body_file = body_file
end
end
end
return log
end
_M.get_full_log = get_full_log