From 9285a13ffe7e46c2869ca6e8c63848be928a40cb Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Wed, 23 Sep 2020 21:49:17 +0800 Subject: [PATCH 01/11] test: add test. --- t/plugin/http-logger-new-line.t | 223 ++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 t/plugin/http-logger-new-line.t diff --git a/t/plugin/http-logger-new-line.t b/t/plugin/http-logger-new-line.t new file mode 100644 index 000000000000..85d48ed9b212 --- /dev/null +++ b/t/plugin/http-logger-new-line.t @@ -0,0 +1,223 @@ +# +# 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. +# +use t::APISIX 'no_plan'; + +log_level('info'); +repeat_each(1); +no_long_string(); +no_root_location(); +run_tests; + +__DATA__ + +=== TEST 1: sanity, batch_max_size=1 +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "http-logger": { + "uri": "http://127.0.0.1:1980/log", + "batch_max_size": 1, + "max_retry_count": 1, + "retry_delay": 2, + "buffer_duration": 2, + "inactive_timeout": 2, + "concat_method": "new_line" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1982": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 2: hit route and report http logger +--- request +GET /hello +--- response_body +hello world +--- wait: 0.5 +--- no_error_log +[error] +--- error_log +request log: {"upstream":"127.0.0.1:1982" + + + +=== TEST 3: sanity, batch_max_size=1 +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "http-logger": { + "uri": "http://127.0.0.1:1980/log", + "batch_max_size": 3, + "max_retry_count": 3, + "retry_delay": 2, + "buffer_duration": 2, + "inactive_timeout": 1, + "concat_method": "new_line" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1982": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 4: hit route, and no report log +--- request +GET /hello +--- response_body +hello world +--- no_error_log +[error] +request log: + + + +=== TEST 5: hit route, and report log +--- config +location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + + for i = 1, 6 do + t('/hello', ngx.HTTP_GET) + end + + ngx.sleep(3) + ngx.say("done") + } +} +--- request +GET /t +--- timeout: 4 +--- no_error_log +[error] +--- grep_error_log eval +qr/request log:/ +--- grep_error_log_out +request log: +request log: + + + +=== TEST 6: hit route, and report log +--- config +location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + + for i = 1, 6 do + t('/hello', ngx.HTTP_GET) + end + + ngx.sleep(3) + ngx.say("done") + } +} +--- request +GET /t +--- timeout: 4 +--- no_error_log +[error] +--- grep_error_log eval +qr/"upstream":"127.0.0.1:1982"/ +--- grep_error_log_out +"upstream":"127.0.0.1:1982" +"upstream":"127.0.0.1:1982" +"upstream":"127.0.0.1:1982" +"upstream":"127.0.0.1:1982" +"upstream":"127.0.0.1:1982" +"upstream":"127.0.0.1:1982" + + + +=== TEST 7: hit route, and report log +--- config +location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + + for i = 1, 5 do + t('/hello', ngx.HTTP_GET) + end + + ngx.sleep(3) + ngx.say("done") + } +} +--- request +GET /t +--- timeout: 4 +--- no_error_log +[error] +--- grep_error_log eval +qr/"upstream":"127.0.0.1:1982"/ +--- grep_error_log_out +"upstream":"127.0.0.1:1982" +"upstream":"127.0.0.1:1982" +"upstream":"127.0.0.1:1982" +"upstream":"127.0.0.1:1982" +"upstream":"127.0.0.1:1982" From 44b6843112ce231086a3e491b47618172fedb72a Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Wed, 23 Sep 2020 23:01:10 +0800 Subject: [PATCH 02/11] feat: support user-defined `log_format`, default is json encoding format. --- apisix/plugin.lua | 16 +++ apisix/plugins/http-logger.lua | 70 ++++++++-- bin/apisix | 2 +- t/plugin/http-logger-log-format.t | 122 ++++++++++++++++ t/plugin/http-logger-new-line.t | 223 ------------------------------ t/plugin/tcp-logger.t | 2 +- 6 files changed, 202 insertions(+), 233 deletions(-) create mode 100644 t/plugin/http-logger-log-format.t delete mode 100644 t/plugin/http-logger-new-line.t diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 8c498831ef05..c2d40ce5ab0f 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -25,6 +25,7 @@ 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) @@ -391,6 +392,21 @@ 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 diff --git a/apisix/plugins/http-logger.lua b/apisix/plugins/http-logger.lua index 44df6aeff99c..5b8c1ca29619 100644 --- a/apisix/plugins/http-logger.lua +++ b/apisix/plugins/http-logger.lua @@ -14,15 +14,24 @@ -- 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 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 http = require "resty.http" -local url = require "net.url" +local pairs = pairs + + +local plugin_name = "http-logger" local buffers = {} +local lru_log_format = core.lrucache.new({ + ttl = 300, count = 512 +}) + local schema = { type = "object", @@ -42,11 +51,24 @@ 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, } @@ -121,8 +143,40 @@ local function send_http_data(conf, log_message) end -function _M.log(conf) - local entry = log_util.get_full_log(ngx, conf) +function _M.log(conf, ctx) + local metadata = plugin.plugin_metadata(plugin_name) + core.log.info("metadata: ", core.json.delay_encode(metadata)) + + local log_format = lru_log_format(metadata, nil, function() + local log_format = {} + 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) + + local entry + if log_format 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 if not entry.route_id then core.log.error("failed to obtain the route id for http logger") diff --git a/bin/apisix b/bin/apisix index 342c0fc58fca..bab34c6890cd 100755 --- a/bin/apisix +++ b/bin/apisix @@ -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"}) do + "/proto", "/plugin_metadata"}) do local key = (etcd_conf.prefix or "") .. dir_name .. "/" local base64_encode = require("base64").encode diff --git a/t/plugin/http-logger-log-format.t b/t/plugin/http-logger-log-format.t new file mode 100644 index 000000000000..babed4b31128 --- /dev/null +++ b/t/plugin/http-logger-log-format.t @@ -0,0 +1,122 @@ +# +# 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. +# +use t::APISIX 'no_plan'; + +log_level('info'); +repeat_each(1); +no_long_string(); +no_root_location(); + +run_tests; + +__DATA__ + +=== TEST 1: add plugin metadata +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/http-logger', + ngx.HTTP_PUT, + [[{ + "log_format": { + "host": "$host", + "@timestamp": "$time_iso8601", + "client_ip": "$remote_addr" + } + }]], + [[{ + "node": { + "value": { + "log_format": { + "host": "$host", + "@timestamp": "$time_iso8601", + "client_ip": "$remote_addr" + } + } + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 2: sanity, batch_max_size=1 +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "http-logger": { + "uri": "http://127.0.0.1:1980/log", + "batch_max_size": 1, + "max_retry_count": 1, + "retry_delay": 2, + "buffer_duration": 2, + "inactive_timeout": 2, + "concat_method": "new_line" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1982": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 3: hit route and report http logger +--- request +GET /hello +--- response_body +hello world +--- wait: 0.5 +--- no_error_log +[error] +--- error_log +request log: {"host":"localhost" diff --git a/t/plugin/http-logger-new-line.t b/t/plugin/http-logger-new-line.t deleted file mode 100644 index 85d48ed9b212..000000000000 --- a/t/plugin/http-logger-new-line.t +++ /dev/null @@ -1,223 +0,0 @@ -# -# 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. -# -use t::APISIX 'no_plan'; - -log_level('info'); -repeat_each(1); -no_long_string(); -no_root_location(); -run_tests; - -__DATA__ - -=== TEST 1: sanity, batch_max_size=1 ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "plugins": { - "http-logger": { - "uri": "http://127.0.0.1:1980/log", - "batch_max_size": 1, - "max_retry_count": 1, - "retry_delay": 2, - "buffer_duration": 2, - "inactive_timeout": 2, - "concat_method": "new_line" - } - }, - "upstream": { - "nodes": { - "127.0.0.1:1982": 1 - }, - "type": "roundrobin" - }, - "uri": "/hello" - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 2: hit route and report http logger ---- request -GET /hello ---- response_body -hello world ---- wait: 0.5 ---- no_error_log -[error] ---- error_log -request log: {"upstream":"127.0.0.1:1982" - - - -=== TEST 3: sanity, batch_max_size=1 ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "plugins": { - "http-logger": { - "uri": "http://127.0.0.1:1980/log", - "batch_max_size": 3, - "max_retry_count": 3, - "retry_delay": 2, - "buffer_duration": 2, - "inactive_timeout": 1, - "concat_method": "new_line" - } - }, - "upstream": { - "nodes": { - "127.0.0.1:1982": 1 - }, - "type": "roundrobin" - }, - "uri": "/hello" - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 4: hit route, and no report log ---- request -GET /hello ---- response_body -hello world ---- no_error_log -[error] -request log: - - - -=== TEST 5: hit route, and report log ---- config -location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - - for i = 1, 6 do - t('/hello', ngx.HTTP_GET) - end - - ngx.sleep(3) - ngx.say("done") - } -} ---- request -GET /t ---- timeout: 4 ---- no_error_log -[error] ---- grep_error_log eval -qr/request log:/ ---- grep_error_log_out -request log: -request log: - - - -=== TEST 6: hit route, and report log ---- config -location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - - for i = 1, 6 do - t('/hello', ngx.HTTP_GET) - end - - ngx.sleep(3) - ngx.say("done") - } -} ---- request -GET /t ---- timeout: 4 ---- no_error_log -[error] ---- grep_error_log eval -qr/"upstream":"127.0.0.1:1982"/ ---- grep_error_log_out -"upstream":"127.0.0.1:1982" -"upstream":"127.0.0.1:1982" -"upstream":"127.0.0.1:1982" -"upstream":"127.0.0.1:1982" -"upstream":"127.0.0.1:1982" -"upstream":"127.0.0.1:1982" - - - -=== TEST 7: hit route, and report log ---- config -location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - - for i = 1, 5 do - t('/hello', ngx.HTTP_GET) - end - - ngx.sleep(3) - ngx.say("done") - } -} ---- request -GET /t ---- timeout: 4 ---- no_error_log -[error] ---- grep_error_log eval -qr/"upstream":"127.0.0.1:1982"/ ---- grep_error_log_out -"upstream":"127.0.0.1:1982" -"upstream":"127.0.0.1:1982" -"upstream":"127.0.0.1:1982" -"upstream":"127.0.0.1:1982" -"upstream":"127.0.0.1:1982" diff --git a/t/plugin/tcp-logger.t b/t/plugin/tcp-logger.t index 7343f0ddf5f9..05bd3c470685 100644 --- a/t/plugin/tcp-logger.t +++ b/t/plugin/tcp-logger.t @@ -233,4 +233,4 @@ GET /t --- error_log failed to connect to TCP server: host[312.0.0.1] port[2000] [error] ---- wait: 1.5 +--- wait: 3 From a5766a4100249f102262f9b3fea995c4299862f3 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Sep 2020 06:47:08 +0800 Subject: [PATCH 03/11] revert code. --- t/plugin/tcp-logger.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/plugin/tcp-logger.t b/t/plugin/tcp-logger.t index 05bd3c470685..7343f0ddf5f9 100644 --- a/t/plugin/tcp-logger.t +++ b/t/plugin/tcp-logger.t @@ -233,4 +233,4 @@ GET /t --- error_log failed to connect to TCP server: host[312.0.0.1] port[2000] [error] ---- wait: 3 +--- wait: 1.5 From 4e202b1e9a0e58928d9c5035cb0fe705d3d3a647 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Sep 2020 07:05:37 +0800 Subject: [PATCH 04/11] test: added test api. --- apisix/plugins/http-logger.lua | 28 +++++++++++++++------------- t/lib/server.lua | 6 ++++++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/apisix/plugins/http-logger.lua b/apisix/plugins/http-logger.lua index 5b8c1ca29619..6d4b0f00fbe7 100644 --- a/apisix/plugins/http-logger.lua +++ b/apisix/plugins/http-logger.lua @@ -143,24 +143,26 @@ local function send_http_data(conf, log_message) end +local function gen_log_format(metadata) + local log_format = {} + 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 log_format = lru_log_format(metadata, nil, function() - local log_format = {} - 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) - local entry + local log_format = lru_log_format(metadata, nil, gen_log_format, metadata) if log_format then entry = core.table.new(0, core.table.nkeys(log_format)) for k, var_attr in pairs(log_format) do diff --git a/t/lib/server.lua b/t/lib/server.lua index c9e89e9af2f9..3a0edae251ad 100644 --- a/t/lib/server.lua +++ b/t/lib/server.lua @@ -282,4 +282,10 @@ function _M.headers() ngx.say("/headers") end +function _M.log() + ngx.req.read_body() + local body = ngx.req.get_body_data() + ngx.log(ngx.WARN, "request log: ", body or "nil") +end + return _M From 09d5b0c9510a19e7c2e74d2cfd52197394f7b75b Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Sep 2020 07:26:16 +0800 Subject: [PATCH 05/11] Chinese doc --- doc/zh-cn/plugins/http-logger.md | 52 ++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/doc/zh-cn/plugins/http-logger.md b/doc/zh-cn/plugins/http-logger.md index 7ea7dc11c8c0..cc37823abf76 100644 --- a/doc/zh-cn/plugins/http-logger.md +++ b/doc/zh-cn/plugins/http-logger.md @@ -50,23 +50,23 @@ ## 如何开启 -1. 这是有关如何为特定路由启用 http-logger 插件的示例。 +这是有关如何为特定路由启用 http-logger 插件的示例。 ```shell curl http://127.0.0.1:9080/apisix/admin/routes/1 -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" + "plugins": { + "http-logger": { + "uri": "127.0.0.1:80/postendpoint?param=1" + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:1980": 1 + } + }, + "uri": "/hello" }' ``` @@ -81,6 +81,32 @@ 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: From b939f6aa3b9198886a0e7b5236b149d612b196e6 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Sep 2020 09:55:22 +0800 Subject: [PATCH 06/11] test: delete the plugin metadata at the end of test file. --- t/plugin/http-logger-log-format.t | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/t/plugin/http-logger-log-format.t b/t/plugin/http-logger-log-format.t index babed4b31128..84849075925d 100644 --- a/t/plugin/http-logger-log-format.t +++ b/t/plugin/http-logger-log-format.t @@ -120,3 +120,28 @@ hello world [error] --- error_log request log: {"host":"localhost" + + + +=== TEST 4: remove plugin metadata +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/http-logger', + ngx.HTTP_DELETE + ) + + if code >= 300 then + ngx.status = code + end + + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] From 00ba1dc5df3fe91e0bbcb6d466c77a2614f68743 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Sep 2020 14:02:07 +0800 Subject: [PATCH 07/11] bug: use empty table if there was no plugin metadata. --- apisix/core/table.lua | 2 +- apisix/plugins/http-logger.lua | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/apisix/core/table.lua b/apisix/core/table.lua index 4ad92caa104d..23248408af8d 100644 --- a/apisix/core/table.lua +++ b/apisix/core/table.lua @@ -26,7 +26,6 @@ local ngx_re = require("ngx.re") local _M = { - version = 0.2, new = new_tab, clear = require("table.clear"), nkeys = nkeys, @@ -35,6 +34,7 @@ local _M = { sort = table.sort, clone = require("table.clone"), isarray = require("table.isarray"), + empty_tab = {}, } diff --git a/apisix/plugins/http-logger.lua b/apisix/plugins/http-logger.lua index 31dcfa089439..89ebdf8351d0 100644 --- a/apisix/plugins/http-logger.lua +++ b/apisix/plugins/http-logger.lua @@ -140,7 +140,15 @@ end local function gen_log_format(metadata) - local log_format = {} + local log_format = core.table.empty_tab + if not metadata or + not metadata.value or + not metadata.value.log_format or + core.talbe.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)} @@ -158,8 +166,8 @@ function _M.log(conf, ctx) core.log.info("metadata: ", core.json.delay_encode(metadata)) local entry - local log_format = lru_log_format(metadata, nil, gen_log_format, metadata) - if log_format then + 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 From 8750b97470f8516bb6c4f6d26e4bbb3653dd9d77 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Sep 2020 14:02:26 +0800 Subject: [PATCH 08/11] chore: code style. --- apisix/plugins/http-logger.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apisix/plugins/http-logger.lua b/apisix/plugins/http-logger.lua index 89ebdf8351d0..761f7543cf44 100644 --- a/apisix/plugins/http-logger.lua +++ b/apisix/plugins/http-logger.lua @@ -166,7 +166,8 @@ function _M.log(conf, ctx) core.log.info("metadata: ", core.json.delay_encode(metadata)) local entry - local log_format = lru_log_format(metadata or "", nil, gen_log_format, metadata) + 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 From 6f1ddf8125a6d8586f85502bdc3bf8f815b045f1 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Sep 2020 14:43:54 +0800 Subject: [PATCH 09/11] typo --- apisix/plugins/http-logger.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/plugins/http-logger.lua b/apisix/plugins/http-logger.lua index 761f7543cf44..a483ab1c4dcb 100644 --- a/apisix/plugins/http-logger.lua +++ b/apisix/plugins/http-logger.lua @@ -144,7 +144,7 @@ local function gen_log_format(metadata) if not metadata or not metadata.value or not metadata.value.log_format or - core.talbe.nkeys(metadata.value.log_format) == 0 + core.table.nkeys(metadata.value.log_format) == 0 then return log_format end From 90e307393d08f53a30a096363c009059459d1b54 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Sep 2020 15:25:01 +0800 Subject: [PATCH 10/11] test: update match context --- t/plugin/http-logger-log-format.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/plugin/http-logger-log-format.t b/t/plugin/http-logger-log-format.t index 84849075925d..d092eebba66f 100644 --- a/t/plugin/http-logger-log-format.t +++ b/t/plugin/http-logger-log-format.t @@ -119,7 +119,7 @@ hello world --- no_error_log [error] --- error_log -request log: {"host":"localhost" +request log: { From c8fe4c79e2041d90806fb954e2aeb721a439ed54 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Sep 2020 16:19:15 +0800 Subject: [PATCH 11/11] test: use `apple.com` to replace `baidu.com`, it is more stable in CI env. --- t/node/route-domain-with-local-dns.t | 4 ++-- t/node/route-domain.t | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/t/node/route-domain-with-local-dns.t b/t/node/route-domain-with-local-dns.t index d913c1d63d70..650247e44538 100644 --- a/t/node/route-domain-with-local-dns.t +++ b/t/node/route-domain-with-local-dns.t @@ -43,7 +43,7 @@ __DATA__ "upstream": { "nodes": { "127.0.0.1:1980": 1, - "baidu.com:80": 0 + "apple.com:80": 0 }, "type": "roundrobin" }, @@ -89,4 +89,4 @@ hello world --- no_error_log [error] --- error_log eval -qr/dns resolver domain: baidu.com to \d+.\d+.\d+.\d+/ +qr/dns resolver domain: apple.com to \d+.\d+.\d+.\d+/ diff --git a/t/node/route-domain.t b/t/node/route-domain.t index 6fbd31da4b13..6ca1c017af31 100644 --- a/t/node/route-domain.t +++ b/t/node/route-domain.t @@ -37,7 +37,7 @@ __DATA__ "upstream": { "nodes": { "127.0.0.1:1980": 1, - "baidu.com:80": 0 + "apple.com:80": 0 }, "type": "roundrobin" }, @@ -79,7 +79,7 @@ hello world --- no_error_log [error] --- error_log eval -qr/dns resolver domain: baidu.com to \d+.\d+.\d+.\d+/ +qr/dns resolver domain: apple.com to \d+.\d+.\d+.\d+/ @@ -97,7 +97,7 @@ qr/dns resolver domain: baidu.com to \d+.\d+.\d+.\d+/ }, "type": "roundrobin", "pass_host": "rewrite", - "upstream_host": "httpbin.org" + "upstream_host": "httpbin.org" }, "uri": "/uri" }]]