fix(tracer): create short handler-id if clientid is too long (#4164)
* fix(tracer): create short handler-id if clientid is too long No need to support lists and atoms for when tracing clients/topics.
This commit is contained in:
parent
56a75d0d47
commit
eec64e440d
|
@ -28,7 +28,7 @@
|
|||
, stop_trace/1
|
||||
]).
|
||||
|
||||
-type(trace_who() :: {clientid | topic, binary() | list()}).
|
||||
-type(trace_who() :: {clientid | topic, binary()}).
|
||||
|
||||
-define(TRACER, ?MODULE).
|
||||
-define(FORMAT, {emqx_logger_formatter,
|
||||
|
@ -135,9 +135,9 @@ filter_traces(#{id := Id, level := Level, dst := Dst}, Acc) ->
|
|||
end.
|
||||
|
||||
handler_id(?TOPIC_TRACE(Topic)) ->
|
||||
list_to_atom(?TOPIC_TRACE_ID(str(Topic)));
|
||||
list_to_atom(?TOPIC_TRACE_ID(handler_name(Topic)));
|
||||
handler_id(?CLIENT_TRACE(ClientId)) ->
|
||||
list_to_atom(?CLIENT_TRACE_ID(str(ClientId))).
|
||||
list_to_atom(?CLIENT_TRACE_ID(handler_name(ClientId))).
|
||||
|
||||
filter_by_meta_key(#{meta:=Meta}=LogEvent, {MetaKey, MetaValue}) ->
|
||||
case maps:find(MetaKey, Meta) of
|
||||
|
@ -150,6 +150,16 @@ filter_by_meta_key(#{meta:=Meta}=LogEvent, {MetaKey, MetaValue}) ->
|
|||
_ -> ignore
|
||||
end.
|
||||
|
||||
str(Bin) when is_binary(Bin) -> binary_to_list(Bin);
|
||||
str(Atom) when is_atom(Atom) -> atom_to_list(Atom);
|
||||
str(Str) when is_list(Str) -> Str.
|
||||
handler_name(Bin) ->
|
||||
case byte_size(Bin) of
|
||||
Size when Size =< 200 -> binary_to_list(Bin);
|
||||
_ -> hashstr(Bin)
|
||||
end.
|
||||
|
||||
hashstr(Bin) ->
|
||||
hexstr(crypto:hash(sha, Bin)).
|
||||
|
||||
hexstr(Bin) ->
|
||||
lists:flatten(
|
||||
[io_lib:format("~2.16.0B", [Int])
|
||||
|| Int <- binary_to_list(Bin)]).
|
||||
|
|
|
@ -47,8 +47,8 @@ t_start_traces(_Config) ->
|
|||
{error, _} = emqx_tracer:start_trace({clientid, <<"client">>}, debug, "tmp/client.log"),
|
||||
emqx_logger:set_log_level(debug),
|
||||
ok = emqx_tracer:start_trace({clientid, <<"client">>}, debug, "tmp/client.log"),
|
||||
ok = emqx_tracer:start_trace({clientid, "client2"}, all, "tmp/client2.log"),
|
||||
ok = emqx_tracer:start_trace({clientid, client3}, all, "tmp/client3.log"),
|
||||
ok = emqx_tracer:start_trace({clientid, <<"client2">>}, all, "tmp/client2.log"),
|
||||
ok = emqx_tracer:start_trace({clientid, <<"client3">>}, all, "tmp/client3.log"),
|
||||
{error, {invalid_log_level, bad_level}} = emqx_tracer:start_trace({clientid, <<"client4">>}, bad_level, "tmp/client4.log"),
|
||||
{error, {handler_not_added, {file_error,".",eisdir}}} = emqx_tracer:start_trace({clientid, <<"client5">>}, debug, "."),
|
||||
ok = emqx_tracer:start_trace({topic, <<"a/#">>}, all, "tmp/topic_trace.log"),
|
||||
|
|
Loading…
Reference in New Issue