-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin: add HTTP logger for APISIX (#1396)
- Loading branch information
Showing
8 changed files
with
877 additions
and
1 deletion.
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
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,175 @@ | ||
-- | ||
-- Licensed to the Apache Software Foundation (ASF) under one or more | ||
-- contributor license agreements. See the NOTICE file distributed with | ||
-- this work for additional information regarding copyright ownership. | ||
-- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
-- (the "License"); you may not use this file except in compliance with | ||
-- the License. You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
local core = require("apisix.core") | ||
local log_util = require("apisix.utils.log-util") | ||
local batch_processor = require("apisix.utils.batch-processor") | ||
local plugin_name = "http-logger" | ||
local ngx = ngx | ||
local tostring = tostring | ||
local http = require "resty.http" | ||
local url = require "net.url" | ||
local buffers = {} | ||
|
||
local schema = { | ||
type = "object", | ||
properties = { | ||
uri = {type = "string"}, | ||
auth_header = {type = "string", default = ""}, | ||
timeout = {type = "integer", minimum = 1, default = 3}, | ||
name = {type = "string", default = "http logger"}, | ||
max_retry_count = {type = "integer", minimum = 0, default = 0}, | ||
retry_delay = {type = "integer", minimum = 0, default = 1}, | ||
buffer_duration = {type = "integer", minimum = 1, default = 60}, | ||
inactive_timeout = {type = "integer", minimum = 1, default = 5}, | ||
batch_max_size = {type = "integer", minimum = 1, default = 1000}, | ||
}, | ||
required = {"uri"} | ||
} | ||
|
||
|
||
local _M = { | ||
version = 0.1, | ||
priority = 410, | ||
name = plugin_name, | ||
schema = schema, | ||
} | ||
|
||
|
||
function _M.check_schema(conf) | ||
return core.schema.check(schema, conf) | ||
end | ||
|
||
|
||
local function send_http_data(conf, log_message) | ||
local err_msg | ||
local res = true | ||
local url_decoded = url.parse(conf.uri) | ||
local host = url_decoded.host | ||
local port = url_decoded.port | ||
|
||
if ((not port) and url_decoded.scheme == "https") then | ||
port = 443 | ||
elseif not port then | ||
port = 80 | ||
end | ||
|
||
local httpc = http.new() | ||
httpc:set_timeout(conf.timeout * 1000) | ||
local ok, err = httpc:connect(host, port) | ||
|
||
if not ok then | ||
return false, "failed to connect to host[" .. host .. "] port[" | ||
.. tostring(port) .. "] " .. err | ||
end | ||
|
||
if url_decoded.scheme == "https" then | ||
ok, err = httpc:ssl_handshake(true, host, false) | ||
if not ok then | ||
return nil, "failed to perform SSL with host[" .. host .. "] " | ||
.. "port[" .. tostring(port) .. "] " .. err | ||
end | ||
end | ||
|
||
local httpc_res, httpc_err = httpc:request({ | ||
method = "POST", | ||
path = url_decoded.path, | ||
query = url_decoded.query, | ||
body = log_message, | ||
headers = { | ||
["Host"] = url_decoded.host, | ||
["Content-Type"] = "application/json", | ||
["Authorization"] = conf.auth_header | ||
} | ||
}) | ||
|
||
if not httpc_res then | ||
return false, "error while sending data to [" .. host .. "] port[" | ||
.. tostring(port) .. "] " .. httpc_err | ||
end | ||
|
||
-- some error occurred in the server | ||
if httpc_res.status >= 400 then | ||
res = false | ||
err_msg = "server returned status code[" .. httpc_res.status .. "] host[" | ||
.. host .. "] port[" .. tostring(port) .. "] " | ||
.. "body[" .. httpc_res:read_body() .. "]" | ||
end | ||
|
||
-- keep the connection alive | ||
ok, err = httpc:set_keepalive(conf.keepalive) | ||
|
||
if not ok then | ||
core.log.debug("failed to keep the connection alive", err) | ||
end | ||
|
||
return res, err_msg | ||
end | ||
|
||
|
||
function _M.log(conf) | ||
local entry = log_util.get_full_log(ngx) | ||
|
||
if not entry.route_id then | ||
core.log.error("failed to obtain the route id for http logger") | ||
return | ||
end | ||
|
||
local log_buffer = buffers[entry.route_id] | ||
|
||
if log_buffer then | ||
log_buffer:push(entry) | ||
return | ||
end | ||
|
||
-- Generate a function to be executed by the batch processor | ||
local func = function(entries, batch_max_size) | ||
local data, err | ||
if batch_max_size == 1 then | ||
data, err = core.json.encode(entries[1]) -- encode as single {} | ||
else | ||
data, err = core.json.encode(entries) -- encode as array [{}] | ||
end | ||
|
||
if not data then | ||
return false, 'error occurred while encoding the data: ' .. err | ||
end | ||
|
||
return send_http_data(conf, data) | ||
end | ||
|
||
local config = { | ||
name = conf.name, | ||
retry_delay = conf.retry_delay, | ||
batch_max_size = conf.batch_max_size, | ||
max_retry_count = conf.max_retry_count, | ||
buffer_duration = conf.buffer_duration, | ||
inactive_timeout = conf.inactive_timeout, | ||
} | ||
|
||
local err | ||
log_buffer, err = batch_processor:new(func, config) | ||
|
||
if not log_buffer then | ||
core.log.error("error when creating the batch processor: ", err) | ||
return | ||
end | ||
|
||
buffers[entry.route_id] = log_buffer | ||
log_buffer:push(entry) | ||
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 |
---|---|---|
|
@@ -147,5 +147,6 @@ plugins: # plugin list | |
- cors | ||
- syslog | ||
- batch-requests | ||
- http-logger | ||
stream_plugins: | ||
- mqtt-proxy |
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,100 @@ | ||
<!-- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
--> | ||
|
||
# Summary | ||
- [**Name**](#name) | ||
- [**Attributes**](#attributes) | ||
- [**How To Enable**](#how-to-enable) | ||
- [**Test Plugin**](#test-plugin) | ||
- [**Disable Plugin**](#disable-plugin) | ||
|
||
|
||
## Name | ||
|
||
`http-logger` is a plugin which push Log data requests to HTTP/HTTPS servers. | ||
|
||
This will provide the ability to send Log data requests as JSON objects to Monitoring tools and other HTTP servers. | ||
|
||
## Attributes | ||
|
||
|Name |Requirement |Description| | ||
|--------- |-------- |-----------| | ||
|uri |required |URI of the server| | ||
|authorization |optional |Any authorization headers| | ||
|keepalive |optional |Time to keep the connection alive after sending a request| | ||
|name |optional |A unique identifier to identity the logger| | ||
|batch_max_size |optional |Max size of each batch, default is 1000| | ||
|inactive_timeout|optional |maximum age in seconds when the buffer will be flushed if inactive, default is 5s| | ||
|buffer_duration|optional |Maximum age in seconds of the oldest entry in a batch before the batch must be processed, default is 5| | ||
|max_retry_count|optional |Maximum number of retries before removing from the processing pipe line; default is zero| | ||
|retry_delay |optional |Number of seconds the process execution should be delayed if the execution fails; default is 1| | ||
|
||
|
||
## How To Enable | ||
|
||
The following is an example on how to enable the http-logger for a specific route. | ||
|
||
```shell | ||
curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' | ||
{ | ||
"plugins": { | ||
"http-logger": { | ||
"uri": "127.0.0.1:80/postendpoint?param=1", | ||
} | ||
}, | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
} | ||
}, | ||
"uri": "/hello" | ||
}' | ||
``` | ||
|
||
## Test Plugin | ||
|
||
* success: | ||
|
||
```shell | ||
$ curl -i http://127.0.0.1:9080/hello | ||
HTTP/1.1 200 OK | ||
... | ||
hello, world | ||
``` | ||
|
||
## Disable Plugin | ||
|
||
Remove the corresponding json configuration in the plugin configuration to disable the `http-logger`. | ||
APISIX plugins are hot-reloaded, therefore no need to restart APISIX. | ||
|
||
```shell | ||
$ curl http://127.0.0.1:2379/apisix/admin/routes/1 -X PUT -d value=' | ||
{ | ||
"methods": ["GET"], | ||
"uri": "/hello", | ||
"plugins": {}, | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
} | ||
} | ||
}' | ||
``` |
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
Oops, something went wrong.