Skip to content

Commit

Permalink
refactor http-log plugin to reuse main http code for different serial…
Browse files Browse the repository at this point in the history
…ization formats
  • Loading branch information
Tieske committed May 13, 2016
1 parent e21c1d6 commit f674f88
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions kong/plugins/http-log/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ local HTTPS = "https"
-- @param `method` http method to be used to send data
-- @param `parsed_url` contains the host details
-- @param `message` Message to be logged
-- @return `payload` http payload
local function generate_post_payload(method, parsed_url, message)
local body = cjson.encode(message)
local payload = string.format(
-- @return `body` http payload
local function generate_post_payload(method, parsed_url, body)
return string.format(
"%s %s HTTP/1.1\r\nHost: %s\r\nConnection: Keep-Alive\r\nContent-Type: application/json\r\nContent-Length: %s\r\n\r\n%s",
method:upper(), parsed_url.path, parsed_url.host, string.len(body), body)
return payload
end

-- Parse host url
Expand All @@ -44,8 +42,9 @@ end
-- @param `premature`
-- @param `conf` Configuration table, holds http endpoint details
-- @param `message` Message to be logged
local function log(premature, conf, message)
local function log(premature, conf, body, name)
if premature then return end
name = "["..name.."] "

local ok, err
local parsed_url = parse_url(conf.http_endpoint)
Expand All @@ -57,40 +56,47 @@ local function log(premature, conf, message)

ok, err = sock:connect(host, port)
if not ok then
ngx.log(ngx.ERR, "[http-log] failed to connect to "..host..":"..tostring(port)..": ", err)
ngx.log(ngx.ERR, name.."failed to connect to "..host..":"..tostring(port)..": ", err)
return
end

if parsed_url.scheme == HTTPS then
local _, err = sock:sslhandshake(true, host, false)
if err then
ngx.log(ngx.ERR, "[http-log] failed to do SSL handshake with "..host..":"..tostring(port)..": ", err)
ngx.log(ngx.ERR, name.."failed to do SSL handshake with "..host..":"..tostring(port)..": ", err)
end
end

ok, err = sock:send(generate_post_payload(conf.method, parsed_url, message))
ok, err = sock:send(generate_post_payload(conf.method, parsed_url, body))
if not ok then
ngx.log(ngx.ERR, "[http-log] failed to send data to "..host..":"..tostring(port)..": ", err)
ngx.log(ngx.ERR, name.."failed to send data to "..host..":"..tostring(port)..": ", err)
end

ok, err = sock:setkeepalive(conf.keepalive)
if not ok then
ngx.log(ngx.ERR, "[http-log] failed to keepalive to "..host..":"..tostring(port)..": ", err)
ngx.log(ngx.ERR, name.."failed to keepalive to "..host..":"..tostring(port)..": ", err)
return
end
end

function HttpLogHandler:new()
HttpLogHandler.super.new(self, "http-log")
-- Only provide `name` when deriving from this class. Not when initializing an instance.
function HttpLogHandler:new(name)
HttpLogHandler.super.new(self, name or "http-log")
end

-- serializes context data into an html message body
-- @param `ngx` The context table for the request being logged
-- @return html body as string
function HttpLogHandler:serialize(ngx)
return cjson.encode(basic_serializer.serialize(ngx))
end

function HttpLogHandler:log(conf)
HttpLogHandler.super.log(self)

local message = basic_serializer.serialize(ngx)
local ok, err = ngx.timer.at(0, log, conf, message)
local ok, err = ngx.timer.at(0, log, conf, self:serialize(ngx), self._name)
if not ok then
ngx.log(ngx.ERR, "[http-log] failed to create timer: ", err)
ngx.log(ngx.ERR, "["..self._name.."] failed to create timer: ", err)
end
end

Expand Down

0 comments on commit f674f88

Please sign in to comment.