Skip to content

Commit

Permalink
fix(analytics) warn why request body can't be read
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Aug 13, 2015
1 parent fc99855 commit 20a3513
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion kong/plugins/log_serializers/alf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function _M.serialize_entry(ngx)

local alf_req_body = analytics_data.req_body or ""
local alf_res_body = analytics_data.res_body or ""
local alf_req_post_args = analytics_data.req_post_args or {}

-- timers
local proxy_started_at, proxy_ended_at = ngx.ctx.proxy_started_at, ngx.ctx.proxy_ended_at
Expand Down Expand Up @@ -128,7 +129,7 @@ function _M.serialize_entry(ngx)
bodySize = string.len(alf_req_body),
postData = {
mimeType = alf_req_mimeType,
params = dic_to_array(ngx.req.get_post_args()),
params = dic_to_array(alf_req_post_args),
text = alf_req_body
}
},
Expand Down
17 changes: 15 additions & 2 deletions kong/plugins/mashape-analytics/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,24 @@ function AnalyticsHandler:access(conf)
-- Retrieve and keep in memory the bodies for this request
ngx.ctx.analytics = {
req_body = "",
res_body = ""
res_body = "",
req_post_args = {}
}

ngx.req.read_body()

local status, res = pcall(ngx.req.get_post_args)
if not status then
if res == "requesty body in temp file not supported" then
ngx.log(ngx.ERR, "[mashape-analytics] cannot read request body from temporary file. Try increasing the client_body_buffer_size directive.")
else
ngx.log(ngx.ERR, res)
end
else
ngx.ctx.analytics.req_post_args = res
end

if conf.log_body then
ngx.req.read_body()
ngx.ctx.analytics.req_body = ngx.req.get_body_data()
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/plugins/mashape-analytics/fixtures/requests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ return {
get_method = function() return "GET" end,
http_version = function() return 1.1 end,
get_headers = function() return {["Accept"] = "/*/", ["Host"] = "mockbin.com"} end,
get_uri_args = function() return {["hello"] = "world", ["foo"] = "bar", ["number"] = 2} end,
get_post_args = function() return {["hello"] = {"world", "earth"}} end
get_uri_args = function() return {["hello"] = "world", ["foo"] = "bar", ["number"] = 2} end
},
resp = {
get_headers = function() return {["Connection"] = "close", ["Content-Type"] = "application/json", ["Content-Length"] = "934"} end
Expand All @@ -30,6 +29,7 @@ return {
analytics = {
req_body = "hello=world&hello=earth",
res_body = "{\"message\":\"response body\"}",
req_post_args = {["hello"] = {"world", "earth"}},
response_received = 143284457211
}
}
Expand Down Expand Up @@ -100,8 +100,7 @@ return {
get_method = function() return "GET" end,
http_version = function() return 1.1 end,
get_headers = function() return {["Accept"] = "/*/", ["Host"] = "mockbin.com"} end,
get_uri_args = function() return {["hello"] = "world", ["foo"] = "bar"} end,
get_post_args = function() return {["hello"] = {"world", "earth"}} end
get_uri_args = function() return {["hello"] = "world", ["foo"] = "bar"} end
},
resp = {
get_headers = function() return {["Connection"] = "close", ["Content-Type"] = "application/json", ["Content-Length"] = "934"} end
Expand All @@ -122,6 +121,7 @@ return {
analytics = {
req_body = "hello=world&hello=earth",
res_body = "{\"message\":\"response body\"}",
req_post_args = {["hello"] = {"world", "earth"}},
response_received = 143284457211
}
}
Expand Down

0 comments on commit 20a3513

Please sign in to comment.