Merge pull request #7347 from zhongwencool/trace-disable

fix: make all traces stopped when disable emqx_trace_module
This commit is contained in:
zhongwencool 2022-03-23 14:26:45 +08:00 committed by GitHub
commit db5944e169
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 5 deletions

View File

@ -48,6 +48,7 @@
* Fix user or appid created, name only allow `^[A-Za-z]+[A-Za-z0-9-_]*$`
* Fix subscribe http api crash by bad_qos `/mqtt/subscribe`,`/mqtt/subscribe_batch`.
* Send DISCONNECT packet with reason code 0x98 if connection has been kicked [#7309]
* Fix make all traces stopped when emqx_trace_module is disabled.
## v4.4.1

View File

@ -43,8 +43,10 @@
, trace_dir/0
, trace_file/1
, delete_files_after_send/2
, is_enable/0
]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-define(TRACE, ?MODULE).
@ -102,6 +104,10 @@ start_link() ->
list() ->
ets:match_object(?TRACE, #?TRACE{_ = '_'}).
-spec is_enable() -> boolean().
is_enable() ->
undefined =/= erlang:whereis(?MODULE).
-spec list(boolean()) -> [tuple()].
list(Enable) ->
ets:match_object(?TRACE, #?TRACE{enable = Enable, _ = '_'}).

View File

@ -50,11 +50,12 @@ list_trace(_, _Params) ->
FileName = emqx_trace:filename(Name, Start),
LogSize = collect_file_size(Nodes, FileName, AllFileSize),
Trace0 = maps:without([enable, filter], Trace),
ModEnable = emqx_trace:is_enable(),
Trace0#{ log_size => LogSize
, Type => iolist_to_binary(Filter)
, start_at => list_to_binary(calendar:system_time_to_rfc3339(Start))
, end_at => list_to_binary(calendar:system_time_to_rfc3339(End))
, status => status(Enable, Start, End, Now)
, status => status(ModEnable, Enable, Start, End, Now)
}
end, List),
{ok, Traces}
@ -208,7 +209,9 @@ collect_file_size(Nodes, FileName, AllFiles) ->
Acc#{Node => Size}
end, #{}, Nodes).
status(false, _Start, _End, _Now) -> <<"stopped">>;
status(true, Start, _End, Now) when Now < Start -> <<"waiting">>;
status(true, _Start, End, Now) when Now >= End -> <<"stopped">>;
status(true, _Start, _End, _Now) -> <<"running">>.
%% if the module is not running, it will return stopped, user can download the trace file.
status(false, _Enable, _Start, _End, _Now) -> <<"stopped">>;
status(true, false, _Start, _End, _Now) -> <<"stopped">>;
status(true, true, Start, _End, Now) when Now < Start -> <<"waiting">>;
status(true, true, _Start, End, Now) when Now >= End -> <<"stopped">>;
status(true, true, _Start, _End, _Now) -> <<"running">>.