zhongwencool 4 лет назад
Родитель
Сommit
38438415b8

+ 3 - 3
apps/emqx/src/emqx_trace/emqx_trace.erl

@@ -205,14 +205,14 @@ init([]) ->
     {ok, #{timer => TRef, monitors => #{}, primary_log_level => OriginLogLevel}}.
 
 handle_call(Req, _From, State) ->
-    ?LOG(error, "Unexpected call: ~p", [Req]),
+    ?SLOG(error, #{unexpected_call => Req}),
     {reply, ok, State}.
 
 handle_cast({delete_tag, Pid, Files}, State = #{monitors := Monitors}) ->
     erlang:monitor(process, Pid),
     {noreply, State#{monitors => Monitors#{Pid => Files}}};
 handle_cast(Msg, State) ->
-    ?LOG(error, "Unexpected cast: ~p", [Msg]),
+    ?SLOG(error, #{unexpected_cast => Msg}),
     {noreply, State}.
 
 handle_info({'DOWN', _Ref, process, Pid, _Reason}, State = #{monitors := Monitors}) ->
@@ -234,7 +234,7 @@ handle_info({mnesia_table_event, _Events}, State = #{timer := TRef}) ->
     handle_info({timeout, TRef, update_trace}, State);
 
 handle_info(Info, State) ->
-    ?LOG(error, "Unexpected info: ~p", [Info]),
+    ?SLOG(error, #{unexpected_info => Info}),
     {noreply, State}.
 
 terminate(_Reason, #{timer := TRef, primary_log_level := OriginLogLevel}) ->

+ 22 - 4
apps/emqx/src/emqx_trace/emqx_trace_api.erl

@@ -107,10 +107,23 @@ group_trace_file(ZipDir, TraceLog, TraceFiles) ->
         case Res of
             {ok, Node, Bin} ->
                 ZipName = ZipDir ++ Node ++ "-" ++ TraceLog,
-                ok = file:write_file(ZipName, Bin),
-                [Node ++ "-" ++ TraceLog | Acc];
+                case file:write_file(ZipName, Bin) of
+                ok -> [Node ++ "-" ++ TraceLog | Acc];
+                Error ->
+                    ?SLOG(error, #{
+                        msg => "write_file_failed",
+                        error => Error,
+                        zip_name => ZipName,
+                        byte_size => byte_size(Bin)}),
+                    Acc
+                end;
             {error, Node, Reason} ->
-                ?LOG(error, "download trace log error:~p", [{Node, TraceLog, Reason}]),
+                ?SLOG(error, #{
+                    msg => "download_trace_log_failed",
+                    node => Node,
+                    trace_log => TraceLog,
+                    reason => Reason
+                }),
                 Acc
         end
                 end, [], TraceFiles).
@@ -121,7 +134,12 @@ collect_trace_file(TraceLog) ->
 cluster_call(Mod, Fun, Args, Timeout) ->
     Nodes = mria_mnesia:running_nodes(),
     {GoodRes, BadNodes} = rpc:multicall(Nodes, Mod, Fun, Args, Timeout),
-    BadNodes =/= [] andalso ?LOG(error, "rpc call failed on ~p ~p", [BadNodes, {Mod, Fun, Args}]),
+    BadNodes =/= [] andalso
+        ?SLOG(error, #{
+            msg => "rpc_call_failed",
+            bad_nodes => BadNodes,
+            mfa => {Mod, Fun, Args }
+        }),
     GoodRes.
 
 stream_log_file(#{name := Name}, Params) ->

+ 7 - 8
apps/emqx/test/emqx_trace_SUITE.erl

@@ -40,12 +40,12 @@ end_per_suite(_Config) ->
     emqx_common_test_helpers:stop_apps([]).
 
 init_per_testcase(_, Config) ->
-    load(),
     ok = emqx_trace:clear(),
+    reload(),
+    ct:pal("load:~p~n", [erlang:whereis(emqx_trace)]),
     Config.
 
 end_per_testcase(_) ->
-    unload(),
     ok.
 
 t_base_create_delete(_Config) ->
@@ -299,12 +299,13 @@ t_trace_file(_Config) ->
     ok.
 
 t_download_log(_Config) ->
-    ClientId = <<"client-test">>,
+    ClientId = <<"client-test-download">>,
     Now = erlang:system_time(second),
     Start = to_rfc3339(Now),
     Name = <<"test_client_id">>,
     ok = emqx_trace:create([{<<"name">>, Name},
         {<<"type">>, clientid}, {<<"clientid">>, ClientId}, {<<"start_at">>, Start}]),
+    ct:sleep(50),
     {ok, Client} = emqtt:start_link([{clean_start, true}, {clientid, ClientId}]),
     {ok, _} = emqtt:connect(Client),
     [begin _ = emqtt:ping(Client) end ||_ <- lists:seq(1, 5)],
@@ -341,8 +342,6 @@ t_find_closed_time(_Config) ->
 to_rfc3339(Second) ->
     list_to_binary(calendar:system_time_to_rfc3339(Second)).
 
-load() ->
-    emqx_trace:start_link().
-
-unload() ->
-    gen_server:stop(emqx_trace).
+reload() ->
+    catch ok = gen_server:stop(emqx_trace),
+    {ok, _Pid} = emqx_trace:start_link().