Skip to content

Commit

Permalink
Merge pull request #9781 from olcai/delete-zip-file-from-trace-log-do…
Browse files Browse the repository at this point in the history
…wnload

fix(emqx_management): delete files after trace log download
  • Loading branch information
olcai committed Jan 18, 2023
2 parents 8e1475a + 4218227 commit 46fc69c
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 103 deletions.
34 changes: 25 additions & 9 deletions apps/emqx_management/src/emqx_mgmt_api_trace.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
-include_lib("kernel/include/file.hrl").
-include_lib("typerefl/include/types.hrl").
-include_lib("emqx/include/logger.hrl").
-include_lib("snabbkaffe/include/snabbkaffe.hrl").

-export([
api_spec/0,
Expand Down Expand Up @@ -461,16 +462,31 @@ download_trace_log(get, #{bindings := #{name := Name}, query_string := Query}) -
case parse_node(Query, undefined) of
{ok, Node} ->
TraceFiles = collect_trace_file(Node, TraceLog),
ZipDir = emqx_trace:zip_dir(),
%% We generate a session ID so that we name files
%% with unique names. Then we won't cause
%% overwrites for concurrent requests.
SessionId = emqx_misc:gen_id(),
ZipDir = filename:join([emqx_trace:zip_dir(), SessionId]),
ok = file:make_dir(ZipDir),
%% Write files to ZipDir and create an in-memory zip file
Zips = group_trace_file(ZipDir, TraceLog, TraceFiles),
FileName = binary_to_list(Name) ++ ".zip",
ZipFileName = filename:join([ZipDir, FileName]),
{ok, ZipFile} = zip:zip(ZipFileName, Zips, [{cwd, ZipDir}]),
%% emqx_trace:delete_files_after_send(ZipFileName, Zips),
%% TODO use file replace file_binary.(delete file after send is not ready now).
{ok, Binary} = file:read_file(ZipFile),
ZipName = filename:basename(ZipFile),
_ = file:delete(ZipFile),
ZipName = binary_to_list(Name) ++ ".zip",
Binary =
try
{ok, {ZipName, Bin}} = zip:zip(ZipName, Zips, [memory, {cwd, ZipDir}]),
Bin
after
%% emqx_trace:delete_files_after_send(ZipFileName, Zips),
%% TODO use file replace file_binary.(delete file after send is not ready now).
ok = file:del_dir_r(ZipDir)
end,
?tp(trace_api_download_trace_log, #{
files => Zips,
name => Name,
session_id => SessionId,
zip_dir => ZipDir,
zip_name => ZipName
}),
Headers = #{
<<"content-type">> => <<"application/x-zip">>,
<<"content-disposition">> => iolist_to_binary(
Expand Down
15 changes: 5 additions & 10 deletions apps/emqx_management/test/emqx_mgmt_api_test_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ uri(Parts) ->

%% compatible_mode will return as same as 'emqx_dashboard_api_test_helpers:request'
request_api_with_body(Method, Url, Body) ->
request_api(Method, Url, [], auth_header_(), Body, #{compatible_mode => true}).
Opts = #{compatible_mode => true, httpc_req_opts => [{body_format, binary}]},
request_api(Method, Url, [], auth_header_(), Body, Opts).

request_api(Method, Url) ->
request_api(Method, Url, auth_header_()).
Expand Down Expand Up @@ -111,15 +112,9 @@ request_api(Method, Url, QueryParams, AuthOrHeaders, Body, Opts) when
do_request_api(Method, Request, Opts) ->
ReturnAll = maps:get(return_all, Opts, false),
CompatibleMode = maps:get(compatible_mode, Opts, false),
ReqOpts =
case CompatibleMode of
true ->
[{body_format, binary}];
_ ->
[]
end,
ct:pal("Method: ~p, Request: ~p", [Method, Request]),
case httpc:request(Method, Request, [], ReqOpts) of
HttpcReqOpts = maps:get(httpc_req_opts, Opts, []),
ct:pal("Method: ~p, Request: ~p, Opts: ~p", [Method, Request, Opts]),
case httpc:request(Method, Request, [], HttpcReqOpts) of
{error, socket_closed_remotely} ->
{error, socket_closed_remotely};
{ok, {{_, Code, _}, _Headers, Body}} when CompatibleMode ->
Expand Down

0 comments on commit 46fc69c

Please sign in to comment.