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

fix(error-log-logger): avoid sending stale error log #4690

Merged
merged 1 commit into from
Aug 2, 2021
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
14 changes: 9 additions & 5 deletions apisix/plugins/error-log-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ local table = core.table
local schema_def = core.schema
local ngx = ngx
local tcp = ngx.socket.tcp
local string = string
local tostring = tostring
local ipairs = ipairs
local lrucache = core.lrucache.new({
Expand Down Expand Up @@ -122,12 +121,12 @@ end


local function update_filter(value)
local level = log_level[string.upper(value.level)]
local level = log_level[value.level]
local status, err = errlog.set_filter_level(level)
if not status then
return nil, "failed to set filter level by ngx.errlog, the error is :" .. err
else
core.log.debug("set the filter_level to ", config.level)
core.log.notice("set the filter_level to ", value.level)
end

return value
Expand All @@ -149,12 +148,17 @@ local function process()

end

local err_level = log_level[metadata.value.level]
local entries = {}
local logs = errlog.get_logs(9)
while ( logs and #logs>0 ) do
for i = 1, #logs, 3 do
table.insert(entries, logs[i + 2])
table.insert(entries, "\n")
-- There will be some stale error logs after the filter level changed.
-- We should avoid reporting them.
if logs[i] <= err_level then
table.insert(entries, logs[i + 2])
table.insert(entries, "\n")
end
end
logs = errlog.get_logs(9)
end
Expand Down
39 changes: 38 additions & 1 deletion t/plugin/error-log-logger.t
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,44 @@ qr/please set the correct plugin_metadata for error-log-logger/



=== TEST 10: delete the route
=== TEST 10: avoid sending stale error log
--- 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
core.log.warn("this is a warning message for test.")
local code, body = t('/apisix/admin/plugin_metadata/error-log-logger',
ngx.HTTP_PUT,
[[{
"host": "127.0.0.1",
"port": 1999,
"level": "ERROR",
"inactive_timeout": 1
}]]
)
ngx.sleep(2)
core.log.error("this is an error message for test.")
}
}
--- request
GET /tg
--- response_body
--- no_error_log eval
qr/\[Server\] receive data:.*this is a warning message for test./
--- error_log eval
qr/\[Server\] receive data:.*this is an error message for test./
--- wait: 5



=== TEST 11: delete the route
--- yaml_config
apisix:
enable_admin: true
Expand Down