Skip to content

Commit

Permalink
perf(alf_serializer) cache global functions and version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Dec 3, 2015
1 parent 5c26056 commit 3cafb45
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
20 changes: 14 additions & 6 deletions kong/plugins/log-serializers/alf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@

local stringy = require "stringy"

local type = type
local pairs = pairs
local ipairs = ipairs
local os_date = os.date
local tostring = tostring
local tonumber = tonumber
local string_len = string.len
local table_insert = table.insert
local ngx_encode_base64 = ngx.encode_base64

local EMPTY_ARRAY_PLACEHOLDER = "__empty_array_placeholder__"
Expand All @@ -40,7 +48,7 @@ local function dic_to_array(hash, fn)
for _, val in ipairs(v) do
k = tostring(k)
val = tostring(val)
table.insert(arr, {name = k, value = val})
table_insert(arr, {name = k, value = val})
fn(k, val)
end
end
Expand Down Expand Up @@ -113,15 +121,15 @@ function _M.serialize_entry(ngx)

local alf_req_headers_arr = dic_to_array(req_headers, function(k, v) req_headers_str = req_headers_str..k..v end)
local alf_res_headers_arr = dic_to_array(res_headers, function(k, v) res_headers_str = res_headers_str..k..v end)
local alf_req_headers_size = string.len(req_headers_str)
local alf_res_headers_size = string.len(res_headers_str)
local alf_req_headers_size = string_len(req_headers_str)
local alf_res_headers_size = string_len(res_headers_str)

-- mimeType, defaulting to "application/octet-stream"
local alf_req_mimeType = req_headers["Content-Type"] and req_headers["Content-Type"] or "application/octet-stream"
local alf_res_mimeType = res_headers["Content-Type"] and res_headers["Content-Type"] or "application/octet-stream"

return {
startedDateTime = os.date("!%Y-%m-%dT%TZ", alf_started_at),
startedDateTime = os_date("!%Y-%m-%dT%TZ", alf_started_at),
time = alf_time,
request = {
method = ngx.req.get_method(),
Expand All @@ -131,7 +139,7 @@ function _M.serialize_entry(ngx)
headers = alf_req_headers_arr,
headersSize = alf_req_headers_size,
cookies = {EMPTY_ARRAY_PLACEHOLDER},
bodySize = string.len(alf_req_body),
bodySize = string_len(alf_req_body),
postData = {
mimeType = alf_req_mimeType,
params = dic_to_array(alf_req_post_args),
Expand Down Expand Up @@ -187,7 +195,7 @@ function _M.new_alf(ngx, token, environment)
log = {
version = "1.2",
creator = {
name = "mashape-analytics-agent-kong",
name = "galileo-agent-kong",
version = "1.1.0"
},
entries = {_M.serialize_entry(ngx)}
Expand Down
4 changes: 2 additions & 2 deletions spec/plugins/mashape-analytics/alf_serializer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ describe("ALF serializer", function()
assert.truthy(alf.har.log)
assert.equal("1.2", alf.har.log.version)
assert.truthy(alf.har.log.creator)
assert.equal("mashape-analytics-agent-kong", alf.har.log.creator.name)
assert.equal("1.0.2", alf.har.log.creator.version)
assert.equal("galileo-agent-kong", alf.har.log.creator.name)
assert.equal("1.1.0", alf.har.log.creator.version)
assert.truthy(alf.har.log.entries)
assert.equal(1, #(alf.har.log.entries))
end)
Expand Down

0 comments on commit 3cafb45

Please sign in to comment.