Procházet zdrojové kódy

fix: improve rendering of action_template_rendered trace

Kjell Winblad před 1 rokem
rodič
revize
285bfa9367

+ 8 - 2
apps/emqx/src/emqx_trace/emqx_trace.erl

@@ -88,8 +88,14 @@ unsubscribe(Topic, SubOpts) ->
     ?TRACE("UNSUBSCRIBE", "unsubscribe", #{topic => Topic, sub_opts => SubOpts}).
     ?TRACE("UNSUBSCRIBE", "unsubscribe", #{topic => Topic, sub_opts => SubOpts}).
 
 
 rendered_action_template(ActionID, RenderResult) ->
 rendered_action_template(ActionID, RenderResult) ->
-    Msg = lists:flatten(io_lib:format("action_template_rendered(~ts)", [ActionID])),
-    TraceResult = ?TRACE("QUERY_RENDER", Msg, RenderResult),
+    TraceResult = ?TRACE(
+        "QUERY_RENDER",
+        "action_template_rendered",
+        #{
+            result => RenderResult,
+            action_id => ActionID
+        }
+    ),
     case logger:get_process_metadata() of
     case logger:get_process_metadata() of
         #{stop_action_after_render := true} ->
         #{stop_action_after_render := true} ->
             %% We throw an unrecoverable error to stop action before the
             %% We throw an unrecoverable error to stop action before the

+ 14 - 0
apps/emqx/src/emqx_trace/emqx_trace_json_formatter.erl

@@ -84,6 +84,13 @@ prepare_key_value(client_ids = K, V, _PEncode) ->
                 V
                 V
         end,
         end,
     {K, NewV};
     {K, NewV};
+prepare_key_value(action_id = K, V, _PEncode) ->
+    try
+        {action_info, format_action_info(V)}
+    catch
+        _:_ ->
+            {K, V}
+    end;
 prepare_key_value(K, V, PEncode) when is_map(V) ->
 prepare_key_value(K, V, PEncode) when is_map(V) ->
     {K, prepare_log_map(V, PEncode)};
     {K, prepare_log_map(V, PEncode)};
 prepare_key_value(K, V, _PEncode) ->
 prepare_key_value(K, V, _PEncode) ->
@@ -114,3 +121,10 @@ format_map_set_to_list(Map) ->
      || {K, V} <- maps:to_list(Map)
      || {K, V} <- maps:to_list(Map)
     ],
     ],
     lists:sort(Items).
     lists:sort(Items).
+
+format_action_info(V) ->
+    [<<"action">>, Type, Name | _] = binary:split(V, <<":">>, [global]),
+    #{
+        type => Type,
+        name => Name
+    }.

+ 14 - 13
apps/emqx_bridge_http/src/emqx_bridge_http_connector.erl

@@ -315,7 +315,7 @@ on_query(InstId, {send_message, Msg}, State) ->
             ClientId = maps:get(clientid, Msg, undefined),
             ClientId = maps:get(clientid, Msg, undefined),
             on_query(
             on_query(
                 InstId,
                 InstId,
-                {ClientId, Method, {Path, Headers, Body}, Timeout, Retry},
+                {undefined, ClientId, Method, {Path, Headers, Body}, Timeout, Retry},
                 State
                 State
             )
             )
     end;
     end;
@@ -345,19 +345,19 @@ on_query(
             ClientId = clientid(Msg),
             ClientId = clientid(Msg),
             on_query(
             on_query(
                 InstId,
                 InstId,
-                {ClientId, Method, {Path, Headers, Body}, Timeout, Retry},
+                {ActionId, ClientId, Method, {Path, Headers, Body}, Timeout, Retry},
                 State
                 State
             )
             )
     end;
     end;
 on_query(InstId, {Method, Request}, State) ->
 on_query(InstId, {Method, Request}, State) ->
     %% TODO: Get retry from State
     %% TODO: Get retry from State
-    on_query(InstId, {undefined, Method, Request, 5000, _Retry = 2}, State);
+    on_query(InstId, {undefined, undefined, Method, Request, 5000, _Retry = 2}, State);
 on_query(InstId, {Method, Request, Timeout}, State) ->
 on_query(InstId, {Method, Request, Timeout}, State) ->
     %% TODO: Get retry from State
     %% TODO: Get retry from State
-    on_query(InstId, {undefined, Method, Request, Timeout, _Retry = 2}, State);
+    on_query(InstId, {undefined, undefined, Method, Request, Timeout, _Retry = 2}, State);
 on_query(
 on_query(
     InstId,
     InstId,
-    {KeyOrNum, Method, Request, Timeout, Retry},
+    {ActionId, KeyOrNum, Method, Request, Timeout, Retry},
     #{base_path := BasePath} = State
     #{base_path := BasePath} = State
 ) ->
 ) ->
     ?TRACE(
     ?TRACE(
@@ -367,11 +367,12 @@ on_query(
             request => redact_request(Request),
             request => redact_request(Request),
             note => ?READACT_REQUEST_NOTE,
             note => ?READACT_REQUEST_NOTE,
             connector => InstId,
             connector => InstId,
+            action_id => ActionId,
             state => redact(State)
             state => redact(State)
         }
         }
     ),
     ),
     NRequest = formalize_request(Method, BasePath, Request),
     NRequest = formalize_request(Method, BasePath, Request),
-    trace_rendered_action_template(InstId, Method, NRequest, Timeout),
+    trace_rendered_action_template(ActionId, Method, NRequest, Timeout),
     Worker = resolve_pool_worker(State, KeyOrNum),
     Worker = resolve_pool_worker(State, KeyOrNum),
     Result0 = ehttpc:request(
     Result0 = ehttpc:request(
         Worker,
         Worker,
@@ -428,7 +429,7 @@ on_query_async(InstId, {send_message, Msg}, ReplyFunAndArgs, State) ->
             ClientId = maps:get(clientid, Msg, undefined),
             ClientId = maps:get(clientid, Msg, undefined),
             on_query_async(
             on_query_async(
                 InstId,
                 InstId,
-                {ClientId, Method, {Path, Headers, Body}, Timeout},
+                {undefined, ClientId, Method, {Path, Headers, Body}, Timeout},
                 ReplyFunAndArgs,
                 ReplyFunAndArgs,
                 State
                 State
             )
             )
@@ -458,14 +459,14 @@ on_query_async(
             ClientId = clientid(Msg),
             ClientId = clientid(Msg),
             on_query_async(
             on_query_async(
                 InstId,
                 InstId,
-                {ClientId, Method, {Path, Headers, Body}, Timeout},
+                {ActionId, ClientId, Method, {Path, Headers, Body}, Timeout},
                 ReplyFunAndArgs,
                 ReplyFunAndArgs,
                 State
                 State
             )
             )
     end;
     end;
 on_query_async(
 on_query_async(
     InstId,
     InstId,
-    {KeyOrNum, Method, Request, Timeout},
+    {ActionId, KeyOrNum, Method, Request, Timeout},
     ReplyFunAndArgs,
     ReplyFunAndArgs,
     #{base_path := BasePath} = State
     #{base_path := BasePath} = State
 ) ->
 ) ->
@@ -481,7 +482,7 @@ on_query_async(
         }
         }
     ),
     ),
     NRequest = formalize_request(Method, BasePath, Request),
     NRequest = formalize_request(Method, BasePath, Request),
-    trace_rendered_action_template(InstId, Method, NRequest, Timeout),
+    trace_rendered_action_template(ActionId, Method, NRequest, Timeout),
     MaxAttempts = maps:get(max_attempts, State, 3),
     MaxAttempts = maps:get(max_attempts, State, 3),
     Context = #{
     Context = #{
         attempt => 1,
         attempt => 1,
@@ -501,11 +502,11 @@ on_query_async(
     ),
     ),
     {ok, Worker}.
     {ok, Worker}.
 
 
-trace_rendered_action_template(InstId, Method, NRequest, Timeout) ->
+trace_rendered_action_template(ActionId, Method, NRequest, Timeout) ->
     case NRequest of
     case NRequest of
         {Path, Headers} ->
         {Path, Headers} ->
             emqx_trace:rendered_action_template(
             emqx_trace:rendered_action_template(
-                InstId,
+                ActionId,
                 #{
                 #{
                     path => Path,
                     path => Path,
                     method => Method,
                     method => Method,
@@ -515,7 +516,7 @@ trace_rendered_action_template(InstId, Method, NRequest, Timeout) ->
             );
             );
         {Path, Headers, Body} ->
         {Path, Headers, Body} ->
             emqx_trace:rendered_action_template(
             emqx_trace:rendered_action_template(
-                InstId,
+                ActionId,
                 #{
                 #{
                     path => Path,
                     path => Path,
                     method => Method,
                     method => Method,

+ 14 - 0
apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl

@@ -263,6 +263,12 @@ t_http_test_json_formatter(_Config) ->
         topic => Topic,
         topic => Topic,
         rule_ids => maps:from_keys([<<"a">>, <<"b">>, <<"c">>], true)
         rule_ids => maps:from_keys([<<"a">>, <<"b">>, <<"c">>], true)
     }),
     }),
+    %% action_id should be rendered as action_info
+    ?TRACE("CUSTOM", "my_log_msg", #{
+        topic => Topic,
+        action_id =>
+            <<"action:http:emqx_bridge_http_test_lib:connector:http:emqx_bridge_http_test_lib">>
+    }),
     ok = emqx_trace_handler_SUITE:filesync(Name, topic),
     ok = emqx_trace_handler_SUITE:filesync(Name, topic),
     {ok, _Detail2} = request_api(get, api_path("trace/" ++ binary_to_list(Name) ++ "/log_detail")),
     {ok, _Detail2} = request_api(get, api_path("trace/" ++ binary_to_list(Name) ++ "/log_detail")),
     {ok, Bin} = request_api(get, api_path("trace/" ++ binary_to_list(Name) ++ "/download")),
     {ok, Bin} = request_api(get, api_path("trace/" ++ binary_to_list(Name) ++ "/download")),
@@ -320,6 +326,14 @@ t_http_test_json_formatter(_Config) ->
                 <<"meta">> := #{
                 <<"meta">> := #{
                     <<"rule_ids">> := [<<"a">>, <<"b">>, <<"c">>]
                     <<"rule_ids">> := [<<"a">>, <<"b">>, <<"c">>]
                 }
                 }
+            },
+            #{
+                <<"meta">> := #{
+                    <<"action_info">> := #{
+                        <<"type">> := <<"http">>,
+                        <<"name">> := <<"emqx_bridge_http_test_lib">>
+                    }
+                }
             }
             }
             | _
             | _
         ],
         ],

+ 2 - 1
apps/emqx_rule_engine/test/emqx_rule_engine_api_rule_apply_SUITE.erl

@@ -170,7 +170,8 @@ create_trace(TraceName, TraceType, TraceValue) ->
         type => TraceType,
         type => TraceType,
         TraceType => TraceValue,
         TraceType => TraceValue,
         start_at => Start,
         start_at => Start,
-        end_at => End
+        end_at => End,
+        formatter => json
     },
     },
     {ok, _} = emqx_trace:create(Trace).
     {ok, _} = emqx_trace:create(Trace).