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
|
, stop_trace/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-type(trace_who() :: {clientid | topic, binary() | list()}).
|
-type(trace_who() :: {clientid | topic, binary()}).
|
||||||
|
|
||||||
-define(TRACER, ?MODULE).
|
-define(TRACER, ?MODULE).
|
||||||
-define(FORMAT, {emqx_logger_formatter,
|
-define(FORMAT, {emqx_logger_formatter,
|
||||||
|
@ -135,9 +135,9 @@ filter_traces(#{id := Id, level := Level, dst := Dst}, Acc) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
handler_id(?TOPIC_TRACE(Topic)) ->
|
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)) ->
|
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}) ->
|
filter_by_meta_key(#{meta:=Meta}=LogEvent, {MetaKey, MetaValue}) ->
|
||||||
case maps:find(MetaKey, Meta) of
|
case maps:find(MetaKey, Meta) of
|
||||||
|
@ -150,6 +150,16 @@ filter_by_meta_key(#{meta:=Meta}=LogEvent, {MetaKey, MetaValue}) ->
|
||||||
_ -> ignore
|
_ -> ignore
|
||||||
end.
|
end.
|
||||||
|
|
||||||
str(Bin) when is_binary(Bin) -> binary_to_list(Bin);
|
handler_name(Bin) ->
|
||||||
str(Atom) when is_atom(Atom) -> atom_to_list(Atom);
|
case byte_size(Bin) of
|
||||||
str(Str) when is_list(Str) -> Str.
|
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"),
|
{error, _} = emqx_tracer:start_trace({clientid, <<"client">>}, debug, "tmp/client.log"),
|
||||||
emqx_logger:set_log_level(debug),
|
emqx_logger:set_log_level(debug),
|
||||||
ok = emqx_tracer:start_trace({clientid, <<"client">>}, debug, "tmp/client.log"),
|
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, <<"client2">>}, all, "tmp/client2.log"),
|
||||||
ok = emqx_tracer:start_trace({clientid, client3}, all, "tmp/client3.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, {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, "."),
|
{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"),
|
ok = emqx_tracer:start_trace({topic, <<"a/#">>}, all, "tmp/topic_trace.log"),
|
||||||
|
|
Loading…
Reference in New Issue