From faa0d98d778a843cca0b80c6cf411385888e34fe Mon Sep 17 00:00:00 2001 From: Daming Date: Wed, 4 Aug 2021 10:11:41 +0800 Subject: [PATCH] feat: add error log skywalking reporter (#4633) --- apisix/plugins/error-log-logger.lua | 151 ++++++++++++-- docs/en/latest/plugins/error-log-logger.md | 61 ++++-- docs/zh/latest/plugins/error-log-logger.md | 59 ++++-- t/plugin/error-log-logger-skywalking.t | 225 +++++++++++++++++++++ t/plugin/error-log-logger.t | 136 ++++++++++++- 5 files changed, 567 insertions(+), 65 deletions(-) create mode 100644 t/plugin/error-log-logger-skywalking.t diff --git a/apisix/plugins/error-log-logger.lua b/apisix/plugins/error-log-logger.lua index d7b04b65..c65a8d59 100644 --- a/apisix/plugins/error-log-logger.lua +++ b/apisix/plugins/error-log-logger.lua @@ -20,13 +20,15 @@ local errlog = require("ngx.errlog") local batch_processor = require("apisix.utils.batch-processor") local plugin = require("apisix.plugin") local timers = require("apisix.timers") +local http = require("resty.http") local plugin_name = "error-log-logger" local table = core.table local schema_def = core.schema local ngx = ngx local tcp = ngx.socket.tcp local tostring = tostring -local ipairs = ipairs +local ipairs = ipairs +local string = require("string") local lrucache = core.lrucache.new({ ttl = 300, count = 32 }) @@ -35,23 +37,50 @@ local lrucache = core.lrucache.new({ local metadata_schema = { type = "object", properties = { - host = schema_def.host_def, - port = {type = "integer", minimum = 0}, - tls = {type = "boolean", default = false}, - tls_server_name = {type = "string"}, - timeout = {type = "integer", minimum = 1, default = 3}, - keepalive = {type = "integer", minimum = 1, default = 30}, + tcp = { + type = "object", + properties = { + host = schema_def.host_def, + port = {type = "integer", minimum = 0}, + tls = {type = "boolean", default = false}, + tls_server_name = {type = "string"}, + }, + required = {"host", "port"} + }, + skywalking = { + type = "object", + properties = { + endpoint_addr = {schema_def.uri, default = "http://127.0.0.1:12900/v3/logs"}, + service_name = {type = "string", default = "APISIX"}, + service_instance_name = {type="string", default = "APISIX Service Instance"}, + }, + }, + host = {schema_def.host_def, description = "Deprecated, use `tcp.host` instead."}, + port = {type = "integer", minimum = 0, description = "Deprecated, use `tcp.port` instead."}, + tls = {type = "boolean", default = false, + description = "Deprecated, use `tcp.tls` instead."}, + tls_server_name = {type = "string", + description = "Deprecated, use `tcp.tls_server_name` instead."}, name = {type = "string", default = plugin_name}, level = {type = "string", default = "WARN", enum = {"STDERR", "EMERG", "ALERT", "CRIT", "ERR", "ERROR", "WARN", "NOTICE", "INFO", "DEBUG"}}, + timeout = {type = "integer", minimum = 1, default = 3}, + keepalive = {type = "integer", minimum = 1, default = 30}, batch_max_size = {type = "integer", minimum = 0, default = 1000}, 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 = 3}, }, - required = {"host", "port"} + oneOf = { + {required = {"skywalking"}}, + {required = {"tcp"}}, + -- for compatible with old schema + {required = {"host", "port"}} + } } + + local schema = { type = "object", } @@ -84,7 +113,15 @@ local _M = { } -local function send_to_server(data) +function _M.check_schema(conf, schema_type) + if schema_type == core.schema.TYPE_METADATA then + return core.schema.check(metadata_schema, conf) + end + return core.schema.check(schema, conf) +end + + +local function send_to_tcp_server(data) local sock, soc_err = tcp() if not sock then @@ -93,26 +130,27 @@ local function send_to_server(data) sock:settimeout(config.timeout * 1000) - local ok, err = sock:connect(config.host, config.port) + local tcp_config = config.tcp + local ok, err = sock:connect(tcp_config.host, tcp_config.port) if not ok then - return false, "failed to connect the TCP server: host[" .. config.host - .. "] port[" .. tostring(config.port) .. "] err: " .. err + return false, "failed to connect the TCP server: host[" .. tcp_config.host + .. "] port[" .. tostring(tcp_config.port) .. "] err: " .. err end - if config.tls then - ok, err = sock:sslhandshake(false, config.tls_server_name, false) + if tcp_config.tls then + ok, err = sock:sslhandshake(false, tcp_config.tls_server_name, false) if not ok then sock:close() return false, "failed to perform TLS handshake to TCP server: host[" - .. config.host .. "] port[" .. tostring(config.port) .. "] err: " .. err + .. tcp_config.host .. "] port[" .. tostring(tcp_config.port) .. "] err: " .. err end end local bytes, err = sock:send(data) if not bytes then sock:close() - return false, "failed to send data to TCP server: host[" .. config.host - .. "] port[" .. tostring(config.port) .. "] err: " .. err + return false, "failed to send data to TCP server: host[" .. tcp_config.host + .. "] port[" .. tostring(tcp_config.port) .. "] err: " .. err end sock:setkeepalive(config.keepalive * 1000) @@ -120,6 +158,61 @@ local function send_to_server(data) end +local function send_to_skywalking(log_message) + local err_msg + local res = true + core.log.info("sending a batch logs to ", config.skywalking.endpoint_addr) + + local httpc = http.new() + httpc:set_timeout(config.timeout * 1000) + + local entries = {} + for i = 1, #log_message, 2 do + local content = { + service = config.skywalking.service_name, + serviceInstance = config.skywalking.service_instance_name, + endpoint = "", + body = { + text = { + text = log_message[i] + } + } + } + table.insert(entries, content) + end + + local httpc_res, httpc_err = httpc:request_uri( + config.skywalking.endpoint_addr, + { + method = "POST", + body = core.json.encode(entries), + keepalive_timeout = config.keepalive * 1000, + headers = { + ["Content-Type"] = "application/json", + } + } + ) + + if not httpc_res then + return false, "error while sending data to skywalking[" + .. config.skywalking.endpoint_addr .. "] " .. httpc_err + end + + -- some error occurred in the server + if httpc_res.status >= 400 then + res = false + err_msg = string.format( + "server returned status code[%s] skywalking[%s] body[%s]", + httpc_res.status, + config.skywalking.endpoint_addr.endpoint_addr, + httpc_res:read_body() + ) + end + + return res, err_msg +end + + local function update_filter(value) local level = log_level[value.level] local status, err = errlog.set_filter_level(level) @@ -133,6 +226,14 @@ local function update_filter(value) end +local function send(data) + if config.skywalking then + return send_to_skywalking(data) + end + return send_to_tcp_server(data) +end + + local function process() local metadata = plugin.plugin_metadata(plugin_name) if not (metadata and metadata.value and metadata.modifiedIndex) then @@ -145,7 +246,19 @@ local function process() core.log.warn("set log filter failed for ", err) return end - + if not (config.tcp or config.skywalking) then + config.tcp = { + host = config.host, + port = config.port, + tls = config.tls, + tls_server_name = config.tls_server_name + } + core.log.warn( + string.format("The schema is out of date. Please update to the new configuration, " + .. "for example: {\"tcp\": {\"host\": \"%s\", \"port\": \"%s\"}}", + config.host, config.port + )) + end end local err_level = log_level[metadata.value.level] @@ -184,7 +297,7 @@ local function process() } local err - log_buffer, err = batch_processor:new(send_to_server, config_bat) + log_buffer, err = batch_processor:new(send, config_bat) if not log_buffer then core.log.warn("error when creating the batch processor: ", err) diff --git a/docs/en/latest/plugins/error-log-logger.md b/docs/en/latest/plugins/error-log-logger.md index 6ceab8dc..a5649cff 100644 --- a/docs/en/latest/plugins/error-log-logger.md +++ b/docs/en/latest/plugins/error-log-logger.md @@ -30,31 +30,38 @@ title: error-log-logger ## Name -`error-log-logger` is a plugin which pushes the log data of APISIX's error.log to TCP servers. +`error-log-logger` is a plugin which pushes the log data of APISIX's `error.log` to TCP servers or [Apache SkyWalking](https://skywalking.apache.org/). -This plugin will provide the ability to send the log data which selected by the level to Monitoring tools and other TCP servers. +This plugin will provide the ability to send the log data which selected by the level to Monitoring tools and other TCP servers, and SkyWalking over HTTP. -This plugin provides the ability as a batch to push the log data to your external TCP servers. If not receive the log data, don't worry, it will automatically send the logs after the timer function expires in our Batch Processor. +This plugin provides the ability as a batch to push the log data to your external TCP servers or monitoring tools. If not receive the log data, don't worry, it will automatically send the logs after the timer function expires in our Batch Processor. For more info on Batch-Processor in Apache APISIX please refer. [Batch-Processor](../batch-processor.md) ## Attributes -| Name | Type | Requirement | Default | Valid | Description | -| ---------------- | ------- | ----------- | ------- | ------- | ---------------------------------------------------------------------------------------- | -| host | string | required | | | IP address or the Hostname of the TCP server. | -| port | integer | required | | [0,...] | Target upstream port. | -| timeout | integer | optional | 3 | [1,...] | Timeout for the upstream to connect and send, unit: second. | -| keepalive | integer | optional | 30 | [1,...] | Time for keeping the cosocket alive, unit: second. | -| level | string | optional | WARN | | The filter's log level, default warn, choose the level in ["STDERR", "EMERG", "ALERT", "CRIT", "ERR", "ERROR", "WARN", "NOTICE", "INFO", "DEBUG"], the value ERR equals ERROR. | -| tls | boolean | optional | false | | Control whether to perform SSL verification | -| tls_server_name | string | optional | | | The server name for the new TLS extension SNI | -| batch_max_size | integer | optional | 1000 | [1,...] | Max size of each batch | -| inactive_timeout | integer | optional | 3 | [1,...] | Maximum age in seconds when the buffer will be flushed if inactive | -| buffer_duration | integer | optional | 60 | [1,...] | Maximum age in seconds of the oldest entry in a batch before the batch must be processed | -| max_retry_count | integer | optional | 0 | [0,...] | Maximum number of retries before removing from the processing pipe line | -| retry_delay | integer | optional | 1 | [0,...] | Number of seconds the process execution should be delayed if the execution fails | +| Name | Type | Requirement | Default | Valid | Description | +| -------------------------------- | ------- | ----------- | ------------------------------ | ------- | ---------------------------------------------------------------------------------------------------- | +| tcp.host | string | required | | | IP address or the Hostname of the TCP server. | +| tcp.port | integer | required | | [0,...] | Target upstream port. | +| tcp.tls | boolean | optional | false | | Control whether to perform SSL verification. | +| tcp.tls_server_name | string | optional | | | The server name for the new TLS extension SNI. | +| skywalking.endpoint_addr | string | optional | http://127.0.0.1:12900/v3/logs | | the http endpoint of Skywalking. | +| skywalking.service_name | string | optional | APISIX | | service name for skywalking reporter | +| skywalking.service_instance_name | String | optional | APISIX Instance Name | | Service instance name for skywalking reporter, set it to `$hostname` to get local hostname directly. | +| host | string | optional | | | (`Deprecated`, use `tcp.host` instead) IP address or the Hostname of the TCP server. | +| port | integer | optional | | [0,...] | (`Deprecated`, use `tcp.port` instead) Target upstream port. | +| tls | boolean | optional | false | | (`Deprecated`, use `tcp.tls` instread) Control whether to perform SSL verification. | +| tls_server_name | string | optional | | | (`Deprecated`, use `tcp.tls_server_name` instead) The server name for the new TLS extension SNI. | +| timeout | integer | optional | 3 | [1,...] | Timeout for the upstream to connect and send, unit: second. | +| keepalive | integer | optional | 30 | [1,...] | Time for keeping the cosocket alive, unit: second. | +| level | string | optional | WARN | | The filter's log level, default warn, choose the level in ["STDERR", "EMERG", "ALERT", "CRIT", "ERR", "ERROR", "WARN", "NOTICE", "INFO", "DEBUG"], the value ERR equals ERROR. | +| batch_max_size | integer | optional | 1000 | [1,...] | Max size of each batch. | +| inactive_timeout | integer | optional | 3 | [1,...] | Maximum age in seconds when the buffer will be flushed if inactive. | +| buffer_duration | integer | optional | 60 | [1,...] | Maximum age in seconds of the oldest entry in a batch before the batch must be processed. | +| max_retry_count | integer | optional | 0 | [0,...] | Maximum number of retries before removing from the processing pipe line. | +| retry_delay | integer | optional | 1 | [0,...] | Number of seconds the process execution should be delayed if the execution fails. | ## How To Enable And Disable @@ -96,8 +103,24 @@ Step: update the attributes of the plugin ```shell curl http://127.0.0.1:9080/apisix/admin/plugin_metadata/error-log-logger -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { - "host": "127.0.0.1", - "port": 1999, + "tcp": { + "host": "127.0.0.1", + "port": 1999 + }, + "inactive_timeout": 1 +}' +``` + +## How to set the SkyWalking OAP server address + +Step: update the attributes of the plugin + +```shell +curl http://127.0.0.1:9080/apisix/admin/plugin_metadata/error-log-logger -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' +{ + "skywalking": { + "endpoint_addr":"http://127.0.0.1:12800/v3/logs" + }, "inactive_timeout": 1 }' ``` diff --git a/docs/zh/latest/plugins/error-log-logger.md b/docs/zh/latest/plugins/error-log-logger.md index fd19c1bf..a9fb0bc4 100644 --- a/docs/zh/latest/plugins/error-log-logger.md +++ b/docs/zh/latest/plugins/error-log-logger.md @@ -30,9 +30,9 @@ title: error-log-logger ## 定义 -`error-log-logger` 是用于按用户设定的 log 级别对 APISIX 的 error.log 进行筛选,继而将筛选的数据发送到 TCP 服务的插件。 +`error-log-logger` 是用于按用户设定的 log 级别对 APISIX 的 `error.log` 进行筛选,继而将筛选的数据发送到 TCP 服务或者 [Apache SkyWalking](https://skywalking.apache.org/) 的插件。 -以实现将 error.log 中的数据进行筛选并发送到监控工具或其它 TCP 服务的能力。 +以实现将 error.log 中的数据进行筛选并发送到监控工具或其它 TCP 服务,和通过 HTTP 发送到 SkyWalking 的能力。 该插件提供了将日志数据作为批处理推送到外部 TCP 服务器的功能。如果您没有收到日志数据,请放心一些时间,它会在我们的批处理处理器中的计时器功能到期后自动发送日志。 @@ -41,20 +41,27 @@ title: error-log-logger ## 属性列表 -| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 | -| ---------------- | ------- | ------ | ------ | ------- | ------------------------------------------------ | -| host | string | 必须 | | | TCP 服务的IP地址或主机名 | -| port | integer | 必须 | | [0,...] | 目标端口 | -| timeout | integer | 可选 | 3 | [1,...] | 连接和发送数据超时间(以秒为单位) | -| keepalive | integer | 可选 | 30 | [1,...] | 复用连接时,连接保持的时间(以秒为单位) | -| level | string | 可选 | WARN | | 进行错误日志筛选的级别,缺省WARN,取值["STDERR", "EMERG", "ALERT", "CRIT", "ERR", "ERROR", "WARN", "NOTICE", "INFO", "DEBUG"],其中 ERR 与 ERROR 级别一致 | -| tls | boolean | 可选 | false | | 用于控制是否执行SSL验证 | -| tls_server_name | string | 可选 | | | TLS 服务名称标记 | -| inactive_timeout | integer | 可选 | 3 | [1,...] | 刷新缓冲区的最大时间(以秒为单位) | -| batch_max_size | integer | 可选 | 1000 | [1,...] | 每批的最大大小 | -| buffer_duration | integer | 可选 | 60 | [1,...] | 必须先处理批次中最旧条目的最长期限(以秒为单位) | -| max_retry_count | integer | 可选 | 0 | [0,...] | 从处理管道中移除之前的最大重试次数 | -| retry_delay | integer | 可选 | 1 | [0,...] | 如果执行失败,则应延迟执行流程的秒数 | +| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 | +| -------------------------------- | ------- | ----- | ------------------------------ | ---------------- | -------------------------------------------------------------------------| +| tcp.host | string | 必须 | | | TCP 服务的IP地址或主机名 | +| tcp.port | integer | 必须 | | [0,...] | 目标端口 | +| tcp.tls | boolean | 可选 | false | | 用于控制是否执行SSL验证 | +| tcp.tls_server_name | string | 可选 | | | TLS 服务名称标记 | +| skywalking.endpoint_addr | string | 可选 | http://127.0.0.1:12900/v3/logs | | Skywalking 的 HTTP endpoint 地址,例如:http://127.0.0.1:12800 | +| skywalking.service_name | string | 可选 | APISIX | | skywalking 上报的 service 名称 | +| skywalking.service_instance_name | String | 可选 | APISIX Instance Name | | skywalking 上报的 service 实例名, 如果期望直接获取本机主机名则设置为 `$hostname` | +| host | string | 可选 | | | (`弃用`,替换成`tcp.host`) TCP 服务的IP地址或主机名 | +| port | integer | 可选 | | [0,...] | (`弃用`,替换成`tcp.port`) 目标端口 | +| tls | boolean | 可选 | false | | (`弃用`,替换成`tcp.tls`) 用于控制是否执行SSL验证 | +| tls_server_name | string | 可选 | | | (`弃用`,替换成`tcp.tls_server_name`) TLS 服务名称标记 | +| timeout | integer | 可选 | 3 | [1,...] | 连接和发送数据超时间(以秒为单位) | +| keepalive | integer | 可选 | 30 | [1,...] | 复用连接时,连接保持的时间(以秒为单位) | +| level | string | 可选 | WARN | | 进行错误日志筛选的级别,缺省WARN,取值["STDERR", "EMERG", "ALERT", "CRIT", "ERR", "ERROR", "WARN", "NOTICE", "INFO", "DEBUG"],其中 ERR 与 ERROR 级别一致 | +| inactive_timeout | integer | 可选 | 3 | [1,...] | 刷新缓冲区的最大时间(以秒为单位) | +| batch_max_size | integer | 可选 | 1000 | [1,...] | 每批的最大大小 | +| buffer_duration | integer | 可选 | 60 | [1,...] | 必须先处理批次中最旧条目的最长期限(以秒为单位) | +| max_retry_count | integer | 可选 | 0 | [0,...] | 从处理管道中移除之前的最大重试次数 | +| retry_delay | integer | 可选 | 1 | [0,...] | 如果执行失败,则应延迟执行流程的秒数 | ## 如何开启和禁用 @@ -94,8 +101,24 @@ plugins: # plugin list ```shell curl http://127.0.0.1:9080/apisix/admin/plugin_metadata/error-log-logger -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { - "host": "127.0.0.1", - "port": 1999, + "tcp": { + "host": "127.0.0.1", + "port": 1999 + }, + "inactive_timeout": 1 +}' +``` + +## 如何设置接收日志的 SkyWalking OAP 服务器 + +步骤:更新插件属性 + +```shell +curl http://127.0.0.1:9080/apisix/admin/plugin_metadata/error-log-logger -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' +{ + "skywalking": { + "endpoint_addr": "http://127.0.0.1:12800/v3/logs" + }, "inactive_timeout": 1 }' ``` diff --git a/t/plugin/error-log-logger-skywalking.t b/t/plugin/error-log-logger-skywalking.t new file mode 100644 index 00000000..0e5f5dce --- /dev/null +++ b/t/plugin/error-log-logger-skywalking.t @@ -0,0 +1,225 @@ +# +# 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('debug'); +repeat_each(1); +no_long_string(); +no_root_location(); +worker_connections(128); +run_tests; + +__DATA__ + +=== TEST 1: test schema checker +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local plugin = require("apisix.plugins.error-log-logger") + local ok, err = plugin.check_schema( + { + skywalking = { + endpoint_addr = "http://127.0.0.1" + } + }, + core.schema.TYPE_METADATA + ) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- request +GET /t +--- response_body +done +--- no_error_log +[error] + + + +=== TEST 2: test unreachable server +--- yaml_config +apisix: + enable_admin: true + admin_key: null +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/error-log-logger', + ngx.HTTP_PUT, + [[{ + "skywalking": { + "endpoint_addr": "http://127.0.0.1:1988/log" + }, + "inactive_timeout": 1 + }]] + ) + ngx.sleep(2) + core.log.warn("this is a warning message for test.") + } + } +--- request +GET /tg +--- response_body +--- error_log eval +qr/.*\[lua\] batch-processor.lua:63: Batch Processor\[error-log-logger\] failed to process entries: error while sending data to skywalking\[http:\/\/127.0.0.1:1988\/log\] connection refused, context: ngx.timer/ +--- wait: 3 + + + +=== TEST 3: put plugin metadata and log an error level message +--- yaml_config +apisix: + enable_admin: true + admin_key: null +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/error-log-logger', + ngx.HTTP_PUT, + [[{ + "skywalking": { + "endpoint_addr": "http://127.0.0.1:1982/log", + "service_instance_name": "instance" + }, + "batch_max_size": 15, + "inactive_timeout": 1 + }]] + ) + ngx.sleep(2) + core.log.error("this is an error message for test.") + } + } +--- request +GET /tg +--- response_body +--- error_log eval +qr/.*\[\{\"body\":\{\"text\":\{\"text\":\".*this is an error message for test.*\"\}\},\"endpoint\":\"\",\"service\":\"APISIX\",\"serviceInstance\":\"instance\".*/ +--- wait: 5 + + + +=== TEST 4: log a warn level message +--- yaml_config +apisix: + enable_admin: true + admin_key: null +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + core.log.warn("this is a warning message for test.") + } + } +--- request +GET /tg +--- response_body +--- error_log eval +qr/.*\[\{\"body\":\{\"text\":\{\"text\":\".*this is a warning message for test.*\"\}\},\"endpoint\":\"\",\"service\":\"APISIX\",\"serviceInstance\":\"instance\".*/ +--- wait: 5 + + + +=== TEST 5: log some messages +--- yaml_config +apisix: + enable_admin: true + admin_key: null +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + core.log.error("this is an error message for test.") + core.log.warn("this is a warning message for test.") + } + } +--- request +GET /tg +--- response_body +--- error_log eval +qr/.*\[\{\"body\":\{\"text\":\{\"text\":\".*this is an error message for test.*\"\}\},\"endpoint\":\"\",\"service\":\"APISIX\",\"serviceInstance\":\"instance\".*\},\{\"body\":\{\"text\":\{\"text\":\".*this is a warning message for test.*\"\}\}.*/ +--- wait: 5 + + + +=== TEST 6: log an info level message +--- yaml_config +apisix: + enable_admin: true + admin_key: null +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + core.log.info("this is an info message for test.") + } + } +--- request +GET /tg +--- response_body +--- no_error_log eval +qr/.*\[\{\"body\":\{\"text\":\{\"text\":\".*this is an info message for test.*\"\}\},\"endpoint\":\"\",\"service\":\"APISIX\",\"serviceInstance\":\"instance\".*/ +--- wait: 5 + + + +=== TEST 7: delete metadata for the plugin, recover to the default +--- yaml_config +apisix: + enable_admin: true + admin_key: null +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/error-log-logger', + ngx.HTTP_DELETE) + + if code >= 300 then + ngx.status = code + end + + ngx.say(body) + } + } +--- request +GET /tg +--- response_body +passed +--- no_error_log +[error] diff --git a/t/plugin/error-log-logger.t b/t/plugin/error-log-logger.t index 2d2792f9..9dd1c139 100644 --- a/t/plugin/error-log-logger.t +++ b/t/plugin/error-log-logger.t @@ -121,7 +121,9 @@ plugins: local code, body = t('/apisix/admin/plugin_metadata/error-log-logger', ngx.HTTP_PUT, [[{ - "port": 1999, + "tcp": { + "port": 1999 + }, "inactive_timeout": 1 }]] ) @@ -157,8 +159,10 @@ plugins: local code, body = t('/apisix/admin/plugin_metadata/error-log-logger', ngx.HTTP_PUT, [[{ - "host": "127.0.0.1", - "port": 9999, + "tcp": { + "host": "127.0.0.1", + "port": 2999 + }, "inactive_timeout": 1 }]] ) @@ -190,8 +194,10 @@ plugins: local code, body = t('/apisix/admin/plugin_metadata/error-log-logger', ngx.HTTP_PUT, [[{ - "host": "127.0.0.1", - "port": 1999, + "tcp": { + "host": "127.0.0.1", + "port": 1999 + }, "inactive_timeout": 1 }]] ) @@ -298,8 +304,10 @@ plugins: [[{ "plugins": { "error-log-logger": { - "host": "127.0.0.1", - "port": 1999, + "tcp": { + "host": "127.0.0.1", + "port": 1999 + }, "inactive_timeout": 1 } }, @@ -343,8 +351,10 @@ plugins: local code, body = t('/apisix/admin/plugin_metadata/error-log-logger', ngx.HTTP_PUT, [[{ - "host": "127.0.0.1", - "port": 1999, + "tcp": { + "host": "127.0.0.1", + "port": 1999 + }, "level": "ERROR", "inactive_timeout": 1 }]] @@ -392,3 +402,111 @@ GET /tg passed --- no_error_log [error] + + + +=== TEST 12: log a warn level message (schema compatibility testing) +--- yaml_config +apisix: + enable_admin: true + admin_key: null +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/error-log-logger', + ngx.HTTP_PUT, + [[{ + "tcp": { + "host": "127.0.0.1", + "port": 1999 + }, + "inactive_timeout": 1 + }]] + ) + ngx.sleep(2) + core.log.warn("this is a warning message for test.") + } + } +--- request +GET /tg +--- response_body +--- error_log eval +qr/\[Server\] receive data:.*this is a warning message for test./ +--- wait: 5 + + + +=== TEST 13: log an error level message (schema compatibility testing) +--- yaml_config +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + ngx.sleep(2) + core.log.error("this is an error message for test.") + } + } +--- request +GET /tg +--- response_body +--- error_log eval +qr/\[Server\] receive data:.*this is an error message for test./ +--- wait: 5 + + + +=== TEST 14: log an info level message (schema compatibility testing) +--- yaml_config +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + ngx.sleep(2) + core.log.info("this is an info message for test.") + } + } +--- request +GET /tg +--- response_body +--- no_error_log eval +qr/\[Server\] receive data:.*this is an info message for test./ +--- wait: 5 + + + +=== TEST 15: delete metadata for the plugin, recover to the default (schema compatibility testing) +--- yaml_config +apisix: + enable_admin: true + admin_key: null +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/plugin_metadata/error-log-logger', + ngx.HTTP_DELETE) + + if code >= 300 then + ngx.status = code + end + + ngx.say(body) + } + } +--- request +GET /tg +--- response_body +passed +--- no_error_log +[error]