Merge pull request #7921 from zhongwencool/add-node-query-string-to-download-tracelog

feat: add node query_string to download logs for specific nodes
This commit is contained in:
zhongwencool 2022-05-12 10:26:47 +08:00 committed by GitHub
commit 16b6c2f849
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View File

@ -116,7 +116,7 @@ schema("/trace/:name/download") ->
'operationId' => download_trace_log, 'operationId' => download_trace_log,
get => #{ get => #{
description => "Download trace log by name", description => "Download trace log by name",
parameters => [hoconsc:ref(name)], parameters => [hoconsc:ref(name), hoconsc:ref(node)],
responses => #{ responses => #{
200 => 200 =>
#{ #{
@ -408,10 +408,16 @@ update_trace(put, #{bindings := #{name := Name}}) ->
%% if HTTP request headers include accept-encoding: gzip and file size > 300 bytes. %% if HTTP request headers include accept-encoding: gzip and file size > 300 bytes.
%% cowboy_compress_h will auto encode gzip format. %% cowboy_compress_h will auto encode gzip format.
download_trace_log(get, #{bindings := #{name := Name}}) -> download_trace_log(get, #{bindings := #{name := Name}, query_string := Query}) ->
Nodes =
case parse_node(Query, undefined) of
{ok, undefined} -> mria_mnesia:running_nodes();
{ok, Node0} -> [Node0];
{error, not_found} -> mria_mnesia:running_nodes()
end,
case emqx_trace:get_trace_filename(Name) of case emqx_trace:get_trace_filename(Name) of
{ok, TraceLog} -> {ok, TraceLog} ->
TraceFiles = collect_trace_file(TraceLog), TraceFiles = collect_trace_file(Nodes, TraceLog),
ZipDir = emqx_trace:zip_dir(), ZipDir = emqx_trace:zip_dir(),
Zips = group_trace_file(ZipDir, TraceLog, TraceFiles), Zips = group_trace_file(ZipDir, TraceLog, TraceFiles),
FileName = binary_to_list(Name) ++ ".zip", FileName = binary_to_list(Name) ++ ".zip",
@ -456,8 +462,7 @@ group_trace_file(ZipDir, TraceLog, TraceFiles) ->
TraceFiles TraceFiles
). ).
collect_trace_file(TraceLog) -> collect_trace_file(Nodes, TraceLog) ->
Nodes = mria_mnesia:running_nodes(),
wrap_rpc(emqx_mgmt_trace_proto_v1:trace_file(Nodes, TraceLog)). wrap_rpc(emqx_mgmt_trace_proto_v1:trace_file(Nodes, TraceLog)).
wrap_rpc({GoodRes, BadNodes}) -> wrap_rpc({GoodRes, BadNodes}) ->
@ -466,10 +471,9 @@ wrap_rpc({GoodRes, BadNodes}) ->
GoodRes. GoodRes.
stream_log_file(get, #{bindings := #{name := Name}, query_string := Query}) -> stream_log_file(get, #{bindings := #{name := Name}, query_string := Query}) ->
Node0 = maps:get(<<"node">>, Query, atom_to_binary(node())),
Position = maps:get(<<"position">>, Query, 0), Position = maps:get(<<"position">>, Query, 0),
Bytes = maps:get(<<"bytes">>, Query, 1000), Bytes = maps:get(<<"bytes">>, Query, 1000),
case to_node(Node0) of case parse_node(Query, node()) of
{ok, Node} -> {ok, Node} ->
case emqx_mgmt_trace_proto_v1:read_trace_file(Node, Name, Position, Bytes) of case emqx_mgmt_trace_proto_v1:read_trace_file(Node, Name, Position, Bytes) of
{ok, Bin} -> {ok, Bin} ->
@ -561,9 +565,12 @@ read_file(Path, Offset, Bytes) ->
{error, Reason} {error, Reason}
end. end.
to_node(Node) -> parse_node(Query, Default) ->
try try
{ok, binary_to_existing_atom(Node)} case maps:find(<<"node">>, Query) of
error -> {ok, Default};
{ok, Node} -> {ok, binary_to_existing_atom(Node)}
end
catch catch
_:_ -> _:_ ->
{error, not_found} {error, not_found}

View File

@ -199,10 +199,14 @@ t_download_log(_Config) ->
info = #file_info{size = Size, type = regular, access = read_write} info = #file_info{size = Size, type = regular, access = read_write}
} }
]} = ]} =
ZipTab =
zip:table(Binary), zip:table(Binary),
?assert(Size > 0), ?assert(Size > 0),
ZipNamePrefix = lists:flatten(io_lib:format("~s-trace_~s", [node(), Name])), ZipNamePrefix = lists:flatten(io_lib:format("~s-trace_~s", [node(), Name])),
?assertNotEqual(nomatch, re:run(ZipName, [ZipNamePrefix])), ?assertNotEqual(nomatch, re:run(ZipName, [ZipNamePrefix])),
Path = api_path("trace/test_client_id/download?node=" ++ atom_to_list(node())),
{ok, Binary2} = request_api(get, Path, Header),
?assertEqual(ZipTab, zip:table(Binary2)),
ok = emqtt:disconnect(Client), ok = emqtt:disconnect(Client),
ok. ok.