-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Reapplied Runscope plugin to next branch #863
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab6db70
Re-applied Runscope plugin to most recent next branch
mansilladev 52e59b1
Merge branch 'next' of https://github.com/Mashape/kong into next
mansilladev e08f77a
Fix module name on Runscope log serializer
mansilladev 75745d4
Simplified timings for request & response
mansilladev 42d14e9
Remove unused var around timing
mansilladev a21ad03
Include form data in request
mansilladev 55f64c1
Fix response.size_bytes to be measure body size. Refactor response_time
mansilladev 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
local _M = {} | ||
|
||
function _M.serialize(ngx) | ||
|
||
-- JSON format below based on Runscope Messages resource: | ||
-- https://www.runscope.com/docs/api/messages#message-create | ||
-- Not sent: req/res:body_encoding, req:form, res:reason | ||
|
||
-- timers | ||
-- @see core.handler for their definition | ||
-- local req_send_time = ngx.ctx.KONG_PROXY_LATENCY or -1 | ||
-- local req_wait_time = ngx.ctx.KONG_WAITING_TIME or -1 | ||
-- local req_receive_time = ngx.ctx.KONG_RECEIVE_TIME or -1 | ||
|
||
-- Compute the total time. If some properties were unavailable | ||
-- (because the proxying was aborted), then don't add the value. | ||
---- local req_time = 0 | ||
-- for _, timer in ipairs({req_send_time, req_wait_time, req_receive_time}) do | ||
-- if timer > 0 then | ||
-- req_time = req_time + timer | ||
-- end | ||
-- end | ||
return { | ||
request = { | ||
url = ngx.var.scheme.."://"..ngx.var.host..":"..ngx.var.server_port..ngx.var.request_uri, | ||
method = ngx.req.get_method(), | ||
headers = ngx.req.get_headers(), | ||
body = ngx.ctx.runscope.req_body, | ||
timestamp = ngx.req.start_time(), | ||
form = ngx.ctx.runscope.req_post_args | ||
}, | ||
response = { | ||
status = ngx.status, | ||
headers = ngx.resp.get_headers(), | ||
size_bytes = ngx.var.body_bytes_sent, | ||
body = ngx.ctx.runscope.res_body, | ||
timestamp = ngx.now(), | ||
response_time = ngx.var.request_time * 1 | ||
} | ||
} | ||
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,71 @@ | ||
local runscope_serializer = require "kong.plugins.log-serializers.runscope" | ||
local BasePlugin = require "kong.plugins.base_plugin" | ||
local log = require "kong.plugins.runscope.log" | ||
|
||
local ngx_now = ngx.now | ||
local ngx_log = ngx.log | ||
local ngx_log_ERR = ngx.ERR | ||
local string_find = string.find | ||
local pcall = pcall | ||
|
||
local RunscopeLogHandler = BasePlugin:extend() | ||
|
||
function RunscopeLogHandler:new() | ||
RunscopeLogHandler.super.new(self, "runscope") | ||
end | ||
|
||
function RunscopeLogHandler:access(conf) | ||
RunscopeLogHandler.super.access(self) | ||
|
||
local req_body = "" | ||
local res_body = "" | ||
local req_post_args = {} | ||
|
||
if conf.log_body then | ||
ngx.req.read_body() | ||
req_body = ngx.req.get_body_data() | ||
|
||
local headers = ngx.req.get_headers() | ||
local content_type = headers["content-type"] | ||
if content_type and string_find(content_type:lower(), "application/x-www-form-urlencoded", nil, true) then | ||
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_log_ERR, "[runscope] cannot read request body from temporary file. Try increasing the client_body_buffer_size directive.") | ||
else | ||
ngx_log(ngx_log_ERR, res) | ||
end | ||
else | ||
req_post_args = res | ||
end | ||
end | ||
end | ||
|
||
-- keep in memory the bodies for this request | ||
ngx.ctx.runscope = { | ||
req_body = req_body, | ||
res_body = res_body, | ||
req_post_args = req_post_args | ||
} | ||
end | ||
|
||
function RunscopeLogHandler:body_filter(conf) | ||
RunscopeLogHandler.super.body_filter(self) | ||
|
||
local chunk, eof = ngx.arg[1], ngx.arg[2] | ||
if conf.log_body then | ||
ngx.ctx.runscope.res_body = ngx.ctx.runscope.res_body..chunk | ||
end | ||
end | ||
|
||
|
||
function RunscopeLogHandler:log(conf) | ||
RunscopeLogHandler.super.log(self) | ||
|
||
local message = runscope_serializer.serialize(ngx) | ||
log.execute(conf, message) | ||
end | ||
|
||
RunscopeLogHandler.PRIORITY = 1 | ||
|
||
return RunscopeLogHandler |
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,94 @@ | ||
local cjson = require "cjson" | ||
local url = require "socket.url" | ||
|
||
local _M = {} | ||
|
||
local HTTPS = "https" | ||
local ngx_log = ngx.log | ||
local ngx_log_ERR = ngx.ERR | ||
local ngx_timer_at = ngx.timer.at | ||
local string_format = string.format | ||
local string_len = string.len | ||
|
||
-- 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(parsed_url, access_token, message) | ||
local body = cjson.encode(message) | ||
local payload = string_format( | ||
"%s %s HTTP/1.1\r\nHost: %s\r\nConnection: Keep-Alive\r\nAuthorization: Bearer %s\r\nContent-Type: application/json\r\nContent-Length: %s\r\n\r\n%s", | ||
"POST", parsed_url.path, parsed_url.host, access_token, 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) | ||
if premature then | ||
return | ||
end | ||
|
||
local ok, err | ||
local parsed_url = parse_url(conf.api_endpoint.."/buckets/"..conf.bucket_key.."/messages") | ||
local access_token = conf.access_token | ||
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_log_ERR, "[runscope] 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_log_ERR, "[runscope] failed to do SSL handshake with "..host..":"..tostring(port)..": ", err) | ||
end | ||
end | ||
|
||
ok, err = sock:send(generate_post_payload(parsed_url, access_token, message).."\r\n") | ||
if not ok then | ||
ngx_log(ngx_log_ERR, "[runscope] failed to send data to "..host..":"..tostring(port)..": ", err) | ||
end | ||
|
||
ok, err = sock:setkeepalive(conf.keepalive) | ||
if not ok then | ||
ngx_log(ngx_log_ERR, "[runscope] failed to keepalive to "..host..":"..tostring(port)..": ", err) | ||
return | ||
end | ||
end | ||
|
||
function _M.execute(conf, message) | ||
local ok, err = ngx_timer_at(0, log, conf, message) | ||
if not ok then | ||
ngx_log(ngx_log_ERR, "[runscope] 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,10 @@ | ||
return { | ||
fields = { | ||
api_endpoint = { required = true, type = "url", default = "https://api.runscope.com" }, | ||
bucket_key = { required = true, type = "string" }, | ||
access_token = { required = true, default = "", type = "string" }, | ||
timeout = { default = 10000, type = "number" }, | ||
keepalive = { default = 30, type = "number" }, | ||
log_body = { default = false, type="boolean" } | ||
} | ||
} |
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.
Any reason for not including
request.form
since the form parameters are available inngx.ctx.runscope.req_post_args
?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.
Now including form request data in API call