-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Http logging #251
Merged
Merged
Http logging #251
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e21d3e4
http logging
12ca497
updated statics_spec.lua with new plugin
69b3022
http logging
eea1cb2
Merge branch 'http-logging' of github.com:Mashape/kong into http-logging
78647bd
added test cases
f633a7d
updated test case
bc1b0b2
http logging
f8b9bfb
updated statics_spec.lua with new plugin
31a10d4
Merge branch 'http-logging' of github.com:Mashape/kong into http-logging
422cc8d
Merge branch 'master' into http-logging
subnetmarco af31b0e
merging with master and fixing tests
subnetmarco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the benefit of putting this into a timer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thibaultcha you don't have access to the cosocket API into the
log_by_lua
handler, that's why a timer is needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok