chore: limit/page to position/bytes (#6161)
This commit is contained in:
parent
0357f7ad85
commit
6bd5fd218e
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
-module(emqx_trace_api).
|
-module(emqx_trace_api).
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
-include_lib("kernel/include/file.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ list_trace/2
|
-export([ list_trace/2
|
||||||
|
@ -75,7 +76,7 @@ download_zip_log(#{name := Name}, _Param) ->
|
||||||
ZipDir = emqx_trace:zip_dir(),
|
ZipDir = emqx_trace:zip_dir(),
|
||||||
Zips = group_trace_file(ZipDir, TraceLog, TraceFiles),
|
Zips = group_trace_file(ZipDir, TraceLog, TraceFiles),
|
||||||
ZipFileName = ZipDir ++ TraceLog,
|
ZipFileName = ZipDir ++ TraceLog,
|
||||||
{ok, ZipFile} = zip:zip(ZipFileName, Zips),
|
{ok, ZipFile} = zip:zip(ZipFileName, Zips, [{cwd, ZipDir}]),
|
||||||
emqx_trace:delete_files_after_send(ZipFileName, Zips),
|
emqx_trace:delete_files_after_send(ZipFileName, Zips),
|
||||||
{ok, #{}, {sendfile, 0, filelib:file_size(ZipFile), ZipFile}};
|
{ok, #{}, {sendfile, 0, filelib:file_size(ZipFile), ZipFile}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
|
@ -88,7 +89,7 @@ group_trace_file(ZipDir, TraceLog, TraceFiles) ->
|
||||||
{ok, Node, Bin} ->
|
{ok, Node, Bin} ->
|
||||||
ZipName = ZipDir ++ Node ++ "-" ++ TraceLog,
|
ZipName = ZipDir ++ Node ++ "-" ++ TraceLog,
|
||||||
ok = file:write_file(ZipName, Bin),
|
ok = file:write_file(ZipName, Bin),
|
||||||
[ZipName | Acc];
|
[Node ++ "-" ++ TraceLog | Acc];
|
||||||
{error, Node, Reason} ->
|
{error, Node, Reason} ->
|
||||||
?LOG(error, "download trace log error:~p", [{Node, TraceLog, Reason}]),
|
?LOG(error, "download trace log error:~p", [{Node, TraceLog, Reason}]),
|
||||||
Acc
|
Acc
|
||||||
|
@ -101,20 +102,19 @@ collect_trace_file(TraceLog) ->
|
||||||
BadNodes =/= [] andalso ?LOG(error, "download log rpc failed on ~p", [BadNodes]),
|
BadNodes =/= [] andalso ?LOG(error, "download log rpc failed on ~p", [BadNodes]),
|
||||||
Files.
|
Files.
|
||||||
|
|
||||||
%% _page as position and _limit as bytes for front-end reusing components
|
|
||||||
stream_log_file(#{name := Name}, Params) ->
|
stream_log_file(#{name := Name}, Params) ->
|
||||||
Node0 = proplists:get_value(<<"node">>, Params, atom_to_binary(node())),
|
Node0 = proplists:get_value(<<"node">>, Params, atom_to_binary(node())),
|
||||||
Position0 = proplists:get_value(<<"_page">>, Params, <<"0">>),
|
Position0 = proplists:get_value(<<"position">>, Params, <<"0">>),
|
||||||
Bytes0 = proplists:get_value(<<"_limit">>, Params, <<"500">>),
|
Bytes0 = proplists:get_value(<<"bytes">>, Params, <<"1000">>),
|
||||||
Node = binary_to_existing_atom(Node0),
|
Node = binary_to_existing_atom(Node0),
|
||||||
Position = binary_to_integer(Position0),
|
Position = binary_to_integer(Position0),
|
||||||
Bytes = binary_to_integer(Bytes0),
|
Bytes = binary_to_integer(Bytes0),
|
||||||
case rpc:call(Node, ?MODULE, read_trace_file, [Name, Position, Bytes]) of
|
case rpc:call(Node, ?MODULE, read_trace_file, [Name, Position, Bytes]) of
|
||||||
{ok, Bin} ->
|
{ok, Bin} ->
|
||||||
Meta = #{<<"page">> => Position + byte_size(Bin), <<"limit">> => Bytes},
|
Meta = #{<<"position">> => Position + byte_size(Bin), <<"bytes">> => Bytes},
|
||||||
{ok, #{meta => Meta, items => Bin}};
|
{ok, #{meta => Meta, items => Bin}};
|
||||||
eof ->
|
{eof, Size} ->
|
||||||
Meta = #{<<"page">> => Position, <<"limit">> => Bytes},
|
Meta = #{<<"position">> => Size, <<"bytes">> => Bytes},
|
||||||
{ok, #{meta => Meta, items => <<"">>}};
|
{ok, #{meta => Meta, items => <<"">>}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
logger:log(error, "read_file_failed by ~p", [{Name, Reason, Position, Bytes}]),
|
logger:log(error, "read_file_failed by ~p", [{Name, Reason, Position, Bytes}]),
|
||||||
|
@ -134,6 +134,7 @@ read_trace_file(Name, Position, Limit) ->
|
||||||
[] -> {error, not_found}
|
[] -> {error, not_found}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
-dialyzer({nowarn_function, read_file/3}).
|
||||||
read_file(Path, Offset, Bytes) ->
|
read_file(Path, Offset, Bytes) ->
|
||||||
{ok, IoDevice} = file:open(Path, [read, raw, binary]),
|
{ok, IoDevice} = file:open(Path, [read, raw, binary]),
|
||||||
try
|
try
|
||||||
|
@ -141,7 +142,13 @@ read_file(Path, Offset, Bytes) ->
|
||||||
0 -> ok;
|
0 -> ok;
|
||||||
_ -> file:position(IoDevice, {bof, Offset})
|
_ -> file:position(IoDevice, {bof, Offset})
|
||||||
end,
|
end,
|
||||||
file:read(IoDevice, Bytes)
|
case file:read(IoDevice, Bytes) of
|
||||||
|
{ok, Bin} -> {ok, Bin};
|
||||||
|
{error, Reason} -> {error, Reason};
|
||||||
|
eof ->
|
||||||
|
#file_info{size = Size} = file:read_file_info(IoDevice),
|
||||||
|
{eof, Size}
|
||||||
|
end
|
||||||
after
|
after
|
||||||
file:close(IoDevice)
|
file:close(IoDevice)
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -124,14 +124,14 @@ t_stream_log(_Config) ->
|
||||||
{ok, FileBin} = file:read_file(File),
|
{ok, FileBin} = file:read_file(File),
|
||||||
ct:pal("FileBin: ~p ~s", [byte_size(FileBin), FileBin]),
|
ct:pal("FileBin: ~p ~s", [byte_size(FileBin), FileBin]),
|
||||||
Header = auth_header_(),
|
Header = auth_header_(),
|
||||||
{ok, Binary} = request_api(get, api_path("trace/test_stream_log/log?_limit=10"), Header),
|
{ok, Binary} = request_api(get, api_path("trace/test_stream_log/log?bytes=10"), Header),
|
||||||
#{<<"code">> := 0, <<"data">> := #{<<"meta">> := Meta, <<"items">> := Bin}} = json(Binary),
|
#{<<"code">> := 0, <<"data">> := #{<<"meta">> := Meta, <<"items">> := Bin}} = json(Binary),
|
||||||
?assertEqual(10, byte_size(Bin)),
|
?assertEqual(10, byte_size(Bin)),
|
||||||
?assertEqual(#{<<"page">> => 10, <<"limit">> => 10}, Meta),
|
?assertEqual(#{<<"position">> => 10, <<"bytes">> => 10}, Meta),
|
||||||
Path = api_path("trace/test_stream_log/log?_page=20&_limit=10"),
|
Path = api_path("trace/test_stream_log/log?position=20&bytes=10"),
|
||||||
{ok, Binary1} = request_api(get, Path, Header),
|
{ok, Binary1} = request_api(get, Path, Header),
|
||||||
#{<<"code">> := 0, <<"data">> := #{<<"meta">> := Meta1, <<"items">> := Bin1}} = json(Binary1),
|
#{<<"code">> := 0, <<"data">> := #{<<"meta">> := Meta1, <<"items">> := Bin1}} = json(Binary1),
|
||||||
?assertEqual(#{<<"page">> => 30, <<"limit">> => 10}, Meta1),
|
?assertEqual(#{<<"position">> => 30, <<"bytes">> => 10}, Meta1),
|
||||||
?assertEqual(10, byte_size(Bin1)),
|
?assertEqual(10, byte_size(Bin1)),
|
||||||
unload(),
|
unload(),
|
||||||
ok.
|
ok.
|
||||||
|
|
Loading…
Reference in New Issue