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(sls-logger): log entry unable get millisecond timestamp #5820

Merged
merged 5 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 4 additions & 5 deletions apisix/plugins/slslog/rfc5424.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,15 @@ local Severity = {
DEBUG = LOG_DEBUG,
}

local os_date = os.date
local ngx = ngx
local rfc5424_timestamp_format = "!%Y-%m-%dT%H:%M:%S.000Z"
local log_util = require("apisix.utils.log-util")


local _M = { version = 0.1 }

function _M.encode(facility, severity, hostname, appname, pid, project,
logstore, access_key_id, access_key_secret, msg)
local pri = (Facility[facility] * 8 + Severity[severity])
ngx.update_time()
local t = os_date(rfc5424_timestamp_format, ngx.now())
local t = log_util.get_rfc3339_zulu_timestamp()
if not hostname then
hostname = "-"
end
Expand Down
71 changes: 48 additions & 23 deletions t/plugin/sls-logger.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ use t::APISIX 'no_plan';
repeat_each(1);
no_long_string();
no_root_location();
run_tests;

add_block_preprocessor(sub {
my ($block) = @_;

if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]");
}

if (!defined $block->request) {
$block->set_value("request", "GET /t");
}

});

run_tests();

__DATA__

Expand All @@ -37,12 +51,8 @@ __DATA__
ngx.say("done")
}
}
--- request
GET /t
--- response_body
done
--- no_error_log
[error]



Expand All @@ -60,13 +70,9 @@ done
ngx.say("done")
}
}
--- request
GET /t
--- response_body
property "access_key_secret" is required
done
--- no_error_log
[error]



Expand All @@ -84,13 +90,9 @@ done
ngx.say("done")
}
}
--- request
GET /t
--- response_body
property "timeout" validation failed: wrong type: expected integer, got string
done
--- no_error_log
[error]



Expand Down Expand Up @@ -155,12 +157,8 @@ done
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



Expand All @@ -169,8 +167,6 @@ passed
GET /hello
--- response_body
hello world
--- no_error_log
[error]
--- wait: 1


Expand All @@ -188,9 +184,38 @@ hello world
ngx.say(data)
}
}
--- request
GET /t
--- response_body
123
--- no_error_log
[error]



=== TEST 7: sls log get milliseconds
--- config
location /t {
content_by_lua_block {
local function get_syslog_timestamp_millisecond(sls_log)
local first_idx = string.find(sls_log, " ") + 1
local last_idx2 = string.find(sls_log, " ", first_idx)
local rfc3339_date = string.sub(sls_log, first_idx, last_idx2)
local rfc3339_len = string.len(rfc3339_date)
local rfc3339_millisecond = string.sub(rfc3339_date, rfc3339_len - 4, rfc3339_len - 2)
return tonumber(rfc3339_millisecond)
end

local rfc5424 = require("apisix.plugins.slslog.rfc5424")
local m = 0
for _ = 1, 10 do
local sls_log = rfc5424.encode("SYSLOG", "INFO", "localhost", "apisix",
123456, "apisix.apache.org", "apisix.apache.log",
"apisix.sls.logger", "BD274822-96AA-4DA6-90EC-15940FB24444",
"hello world")
shuaijinchao marked this conversation as resolved.
Show resolved Hide resolved
m = get_syslog_timestamp_millisecond(sls_log) + m
end

if m > 0 then
ngx.say("passed")
end
}
}
--- response_body
passed