fix: improve rendering of action_template_rendered trace

This commit is contained in:
Kjell Winblad 2024-04-18 17:00:47 +02:00
parent 7be18730e8
commit 285bfa9367
5 changed files with 52 additions and 16 deletions

View File

@ -88,8 +88,14 @@ unsubscribe(Topic, SubOpts) ->
?TRACE("UNSUBSCRIBE", "unsubscribe", #{topic => Topic, sub_opts => SubOpts}).
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
#{stop_action_after_render := true} ->
%% We throw an unrecoverable error to stop action before the

View File

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

View File

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

View File

@ -263,6 +263,12 @@ t_http_test_json_formatter(_Config) ->
topic => Topic,
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, _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")),
@ -320,6 +326,14 @@ t_http_test_json_formatter(_Config) ->
<<"meta">> := #{
<<"rule_ids">> := [<<"a">>, <<"b">>, <<"c">>]
}
},
#{
<<"meta">> := #{
<<"action_info">> := #{
<<"type">> := <<"http">>,
<<"name">> := <<"emqx_bridge_http_test_lib">>
}
}
}
| _
],

View File

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