Skip to content
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

Revert "feat(http-logger): support for specified the log formats via … #2307

Merged
merged 1 commit into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apisix/core/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local ngx_re = require("ngx.re")


local _M = {
version = 0.2,
new = new_tab,
clear = require("table.clear"),
nkeys = nkeys,
Expand All @@ -34,7 +35,6 @@ local _M = {
sort = table.sort,
clone = require("table.clone"),
isarray = require("table.isarray"),
empty_tab = {},
}


Expand Down
16 changes: 0 additions & 16 deletions apisix/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ local type = type
local local_plugins = core.table.new(32, 0)
local ngx = ngx
local tostring = tostring
local error = error
local local_plugins_hash = core.table.new(0, 32)
local stream_local_plugins = core.table.new(32, 0)
local stream_local_plugins_hash = core.table.new(0, 32)
Expand Down Expand Up @@ -392,21 +391,6 @@ end

function _M.init_worker()
_M.load()

local plugin_metadatas, err = core.config.new("/plugin_metadata",
{automatic = true}
)
if not plugin_metadatas then
error("failed to create etcd instance for fetching /plugin_metadatas : "
.. err)
end

_M.plugin_metadatas = plugin_metadatas
end


function _M.plugin_metadata(name)
return _M.plugin_metadatas:get(name)
end


Expand Down
83 changes: 9 additions & 74 deletions apisix/plugins/http-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,16 @@
-- 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 log_util = require("apisix.utils.log-util")
local core = require("apisix.core")
local http = require("resty.http")
local url = require("net.url")
local plugin = require("apisix.plugin")
local ngx = ngx
local tostring = tostring
local pairs = pairs
local ipairs = ipairs


local plugin_name = "http-logger"
local ngx = ngx
local tostring = tostring
local http = require "resty.http"
local url = require "net.url"
local buffers = {}
local lru_log_format = core.lrucache.new({
ttl = 300, count = 512
})

local ipairs = ipairs

local schema = {
type = "object",
Expand All @@ -54,24 +45,11 @@ local schema = {
}


local metadata_schema = {
type = "object",
properties = {
log_format = {
type = "object",
default = {},
},
},
additionalProperties = false,
}


local _M = {
version = 0.1,
priority = 410,
name = plugin_name,
schema = schema,
metadata_schema = metadata_schema,
}


Expand Down Expand Up @@ -139,51 +117,8 @@ local function send_http_data(conf, log_message)
end


local function gen_log_format(metadata)
local log_format = core.table.empty_tab
if not metadata or
not metadata.value or
not metadata.value.log_format or
core.table.nkeys(metadata.value.log_format) == 0
then
return log_format
end

for k, var_name in pairs(metadata.value.log_format) do
if var_name:sub(1, 1) == "$" then
log_format[k] = {true, var_name:sub(2)}
else
log_format[k] = {false, var_name}
end
end
core.log.info("log_format: ", core.json.delay_encode(log_format))
return log_format
end


function _M.log(conf, ctx)
local metadata = plugin.plugin_metadata(plugin_name)
core.log.info("metadata: ", core.json.delay_encode(metadata))

local entry
local log_format = lru_log_format(metadata or "", nil, gen_log_format,
metadata)
if log_format ~= core.table.empty_tab then
entry = core.table.new(0, core.table.nkeys(log_format))
for k, var_attr in pairs(log_format) do
if var_attr[1] then
entry[k] = ctx.var[var_attr[2]]
else
entry[k] = var_attr[2]
end
end
local matched_route = ctx.matched_route and ctx.matched_route.value

entry.service_id = matched_route.service_id
entry.route_id = matched_route.id
else
entry = log_util.get_full_log(ngx, conf)
end
function _M.log(conf)
local entry = log_util.get_full_log(ngx, conf)

if not entry.route_id then
core.log.error("failed to obtain the route id for http logger")
Expand Down
2 changes: 1 addition & 1 deletion bin/apisix
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ local function init_etcd(show_output)
for _, dir_name in ipairs({"/routes", "/upstreams", "/services",
"/plugins", "/consumers", "/node_status",
"/ssl", "/global_rules", "/stream_routes",
"/proto", "/plugin_metadata"}) do
"/proto"}) do
local key = (etcd_conf.prefix or "") .. dir_name .. "/"

local base64_encode = require("base64").encode
Expand Down
28 changes: 1 addition & 27 deletions doc/zh-cn/plugins/http-logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

## 如何开启

这是有关如何为特定路由启用 http-logger 插件的示例。
1. 这是有关如何为特定路由启用 http-logger 插件的示例。

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
Expand Down Expand Up @@ -82,32 +82,6 @@ HTTP/1.1 200 OK
hello, world
```

## 插件元数据设置

| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
| ---------------- | ------- | ------ | ------------- | ------- | ------------------------------------------------ |
| log_format | object | 可选 | | | 以 Hash 对象方式声明日志格式。对 value 部分,仅支持字符串。如果是以`$`开头,则表明是要获取 [Nginx 内置变量](http://nginx.org/en/docs/varindex.html)。特别的,该设置是全局生效的,意味着指定 log_format 后,将对所有绑定 http-logger 的 Route 或 Service 生效。 |

### 设置日志格式示例

```shell
curl http://127.0.0.1:9080/apisix/admin/plugin_metadata/http-logger -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"log_format": {
"host": "$host",
"@timestamp": "$time_iso8601",
"client_ip": "$remote_addr"
}
}'
```

在日志收集处,将得到类似下面的日志:

```shell
{"host":"localhost","@timestamp":"2020-09-23T19:05:05-04:00","client_ip":"127.0.0.1","route_id":"1"}
{"host":"localhost","@timestamp":"2020-09-23T19:05:05-04:00","client_ip":"127.0.0.1","route_id":"1"}
```

## 禁用插件

在插件配置中删除相应的 json 配置以禁用 http-logger。APISIX 插件是热重载的,因此无需重新启动 APISIX:
Expand Down
4 changes: 2 additions & 2 deletions t/node/route-domain-with-local-dns.t
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ __DATA__
"upstream": {
"nodes": {
"127.0.0.1:1980": 1,
"apple.com:80": 0
"baidu.com:80": 0
},
"type": "roundrobin"
},
Expand Down Expand Up @@ -89,4 +89,4 @@ hello world
--- no_error_log
[error]
--- error_log eval
qr/dns resolver domain: apple.com to \d+.\d+.\d+.\d+/
qr/dns resolver domain: baidu.com to \d+.\d+.\d+.\d+/
6 changes: 3 additions & 3 deletions t/node/route-domain.t
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ __DATA__
"upstream": {
"nodes": {
"127.0.0.1:1980": 1,
"apple.com:80": 0
"baidu.com:80": 0
},
"type": "roundrobin"
},
Expand Down Expand Up @@ -79,7 +79,7 @@ hello world
--- no_error_log
[error]
--- error_log eval
qr/dns resolver domain: apple.com to \d+.\d+.\d+.\d+/
qr/dns resolver domain: baidu.com to \d+.\d+.\d+.\d+/



Expand All @@ -97,7 +97,7 @@ qr/dns resolver domain: apple.com to \d+.\d+.\d+.\d+/
},
"type": "roundrobin",
"pass_host": "rewrite",
"upstream_host": "httpbin.org"
"upstream_host": "httpbin.org"
},
"uri": "/uri"
}]]
Expand Down
147 changes: 0 additions & 147 deletions t/plugin/http-logger-log-format.t

This file was deleted.