Просмотр исходного кода

perf(audit): only truncate the http body

JianBo He 1 год назад
Родитель
Сommit
bb78a911a6
2 измененных файлов с 17 добавлено и 7 удалено
  1. 9 4
      apps/emqx_audit/src/emqx_audit.erl
  2. 8 3
      apps/emqx_audit/test/emqx_audit_api_SUITE.erl

+ 9 - 4
apps/emqx_audit/src/emqx_audit.erl

@@ -40,8 +40,8 @@
 to_audit(#{from := cli, cmd := Cmd, args := Args, duration_ms := DurationMs}) ->
     #?AUDIT{
         operation_id = <<"">>,
-        operation_type = truncate_large_term(Cmd),
-        args = truncate_large_term(Args),
+        operation_type = atom_to_binary(Cmd),
+        args = Args,
         operation_result = <<"">>,
         failure = <<"">>,
         duration_ms = DurationMs,
@@ -67,7 +67,7 @@ to_audit(#{from := erlang_console, function := F, args := Args}) ->
         http_method = <<"">>,
         http_request = <<"">>,
         duration_ms = 0,
-        args = truncate_large_term({F, Args})
+        args = iolist_to_binary(io_lib:format("~p: ~ts", [F, Args]))
     };
 to_audit(#{from := From} = Log) when is_atom(From) ->
     #{
@@ -95,7 +95,7 @@ to_audit(#{from := From} = Log) when is_atom(From) ->
         %% request detail
         http_status_code = StatusCode,
         http_method = Method,
-        http_request = truncate_large_term(Request),
+        http_request = truncate_http_body(Request),
         duration_ms = DurationMs,
         args = <<"">>
     }.
@@ -246,5 +246,10 @@ log_to_file(Level, Meta, #{module := Module} = Handler) ->
             end
     end.
 
+truncate_http_body(Req = #{body := Body}) ->
+    Req#{body => truncate_large_term(Body)};
+truncate_http_body(Req) ->
+    Req.
+
 truncate_large_term(Req) ->
     unicode:characters_to_binary(io_lib:format("~0p", [Req], [{chars_limit, ?CHARS_LIMIT_IN_DB}])).

+ 8 - 3
apps/emqx_audit/test/emqx_audit_api_SUITE.erl

@@ -94,7 +94,12 @@ t_http_api(_) ->
                     <<"operation_id">> := <<"/configs/global_zone">>,
                     <<"source_ip">> := <<"127.0.0.1">>,
                     <<"source">> := _,
-                    <<"http_request">> := _,
+                    <<"http_request">> := #{
+                        <<"method">> := <<"put">>,
+                        <<"body">> := _,
+                        <<"bindings">> := _,
+                        <<"headers">> := #{<<"authorization">> := <<"******">>}
+                    },
                     <<"http_status_code">> := 200,
                     <<"operation_result">> := <<"success">>,
                     <<"operation_type">> := <<"configs">>
@@ -161,7 +166,7 @@ t_cli(_Config) ->
                 <<"operation_id">> := <<"">>,
                 <<"source_ip">> := <<"">>,
                 <<"operation_type">> := <<"conf">>,
-                <<"args">> := <<"[<<\"show\">>,<<\"log\">>]">>,
+                <<"args">> := [<<"show">>, <<"log">>],
                 <<"node">> := _,
                 <<"source">> := <<"">>,
                 <<"http_request">> := <<"">>
@@ -179,7 +184,7 @@ t_cli(_Config) ->
     {ok, Res1} = emqx_mgmt_api_test_util:request_api(get, AuditPath, "from=cli", AuthHeader),
     #{<<"data">> := Data1} = emqx_utils_json:decode(Res1, [return_maps]),
     ?assertMatch(
-        [ShowLogEntry, #{<<"operation_type">> := <<"emqx">>, <<"args">> := <<"[<<\"start\">>]">>}],
+        [ShowLogEntry, #{<<"operation_type">> := <<"emqx">>, <<"args">> := [<<"start">>]}],
         Data1
     ),
     {ok, Res2} = emqx_mgmt_api_test_util:request_api(