-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from Mashape/http-logging
Http logging
- Loading branch information
Showing
9 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ plugins_available: | |
- tcplog | ||
- udplog | ||
- filelog | ||
- httplog | ||
- cors | ||
- request_transformer | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
local BasePlugin = require "kong.plugins.base_plugin" | ||
local log = require "kong.plugins.httplog.log" | ||
|
||
local HttpLogHandler = BasePlugin:extend() | ||
|
||
function HttpLogHandler:new() | ||
HttpLogHandler.super.new(self, "httplog") | ||
end | ||
|
||
function HttpLogHandler:log(conf) | ||
HttpLogHandler.super.log(self) | ||
log.execute(conf) | ||
end | ||
|
||
return HttpLogHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
local cjson = require "cjson" | ||
local url = require "socket.url" | ||
|
||
local _M = {} | ||
|
||
-- Generates http payload . | ||
-- @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("%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 | ||
-- @param `url` host url | ||
-- @return `parsed_url` a table with host details like domain name, port, path etc | ||
local function parse_url(host_url) | ||
local parsed_url = url.parse(host_url) | ||
if not parsed_url.port then | ||
if parsed_url.scheme == "http" then | ||
parsed_url.port = 80 | ||
elseif parsed_url.scheme == "https" then | ||
parsed_url.port = 443 | ||
end | ||
end | ||
if not parsed_url.path then | ||
parsed_url.path = "/" | ||
end | ||
return parsed_url | ||
end | ||
|
||
-- Log to a Http end point. | ||
-- @param `premature` | ||
-- @param `conf` Configuration table, holds http endpoint details | ||
-- @param `message` Message to be logged | ||
local function log(premature, conf, message) | ||
local ok, err | ||
local parsed_url = parse_url(conf.http_endpoint) | ||
local host = parsed_url.host | ||
local port = tonumber(parsed_url.port) | ||
|
||
local sock = ngx.socket.tcp() | ||
sock:settimeout(conf.timeout) | ||
|
||
ok, err = sock:connect(host, port) | ||
if not ok then | ||
ngx.log(ngx.ERR, "failed to connect to "..host..":"..tostring(port)..": ", err) | ||
return | ||
end | ||
|
||
ok, err = sock:send(generate_post_payload(conf.method, parsed_url, message).."\r\n") | ||
if not ok then | ||
ngx.log(ngx.ERR, "failed to send data to "..host..":"..tostring(port)..": ", err) | ||
end | ||
|
||
ok, err = sock:setkeepalive(conf.keepalive) | ||
if not ok then | ||
ngx.log(ngx.ERR, "failed to keepalive to "..host..":"..tostring(port)..": ", err) | ||
return | ||
end | ||
end | ||
|
||
function _M.execute(conf) | ||
local ok, err = ngx.timer.at(0, log, conf, ngx.ctx.log_message) | ||
if not ok then | ||
ngx.log(ngx.ERR, "failed to create timer: ", err) | ||
end | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
return { | ||
http_endpoint = { required = true, type = "string" }, | ||
method = { default = "POST", enum = { "POST", "PUT", "PATCH" } }, | ||
timeout = { default = 10000, type = "number" }, | ||
keepalive = { default = 60000, type = "number" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ plugins_available: | |
- tcplog | ||
- udplog | ||
- filelog | ||
- httplog | ||
- cors | ||
- request_transformer | ||
|