Ver código fonte

fix(json trace format): format client_ids and rule_ids as lists

Kjell Winblad 1 ano atrás
pai
commit
aa388adba9

+ 33 - 2
apps/emqx/src/emqx_trace/emqx_trace_json_formatter.erl

@@ -53,7 +53,7 @@ prepare_key_value(payload = K, V, PEncode) ->
         try
         try
             format_payload(V, PEncode)
             format_payload(V, PEncode)
         catch
         catch
-            _:_:_ ->
+            _:_ ->
                 V
                 V
         end,
         end,
     {K, NewV};
     {K, NewV};
@@ -62,7 +62,25 @@ prepare_key_value(packet = K, V, PEncode) ->
         try
         try
             format_packet(V, PEncode)
             format_packet(V, PEncode)
         catch
         catch
-            _:_:_ ->
+            _:_ ->
+                V
+        end,
+    {K, NewV};
+prepare_key_value(rule_ids = K, V, _PEncode) ->
+    NewV =
+        try
+            format_map_set_to_list(V)
+        catch
+            _:_ ->
+                V
+        end,
+    {K, NewV};
+prepare_key_value(client_ids = K, V, _PEncode) ->
+    NewV =
+        try
+            format_map_set_to_list(V)
+        catch
+            _:_ ->
                 V
                 V
         end,
         end,
     {K, NewV};
     {K, NewV};
@@ -83,3 +101,16 @@ format_payload(Payload, text) when ?MAX_PAYLOAD_FORMAT_LIMIT(Payload) ->
 format_payload(Payload, hex) when ?MAX_PAYLOAD_FORMAT_LIMIT(Payload) -> binary:encode_hex(Payload);
 format_payload(Payload, hex) when ?MAX_PAYLOAD_FORMAT_LIMIT(Payload) -> binary:encode_hex(Payload);
 format_payload(<<Part:?TRUNCATED_PAYLOAD_SIZE/binary, _/binary>> = Payload, Type) ->
 format_payload(<<Part:?TRUNCATED_PAYLOAD_SIZE/binary, _/binary>> = Payload, Type) ->
     emqx_packet:format_truncated_payload(Part, byte_size(Payload), Type).
     emqx_packet:format_truncated_payload(Part, byte_size(Payload), Type).
+
+format_map_set_to_list(Map) ->
+    Items = [
+        begin
+            %% Assert that it is really a map set
+            true = V,
+            %% Assert that the keys have the expected type
+            true = is_binary(K),
+            K
+        end
+     || {K, V} <- maps:to_list(Map)
+    ],
+    lists:sort(Items).

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

@@ -254,6 +254,15 @@ t_http_test_json_formatter(_Config) ->
             {<<"key2">>, <<"value2">>}
             {<<"key2">>, <<"value2">>}
         ]
         ]
     }),
     }),
+    %% We do special formatting for client_ids and rule_ids
+    ?TRACE("CUSTOM", "my_log_msg", #{
+        topic => Topic,
+        client_ids => maps:from_keys([<<"a">>, <<"b">>, <<"c">>], true)
+    }),
+    ?TRACE("CUSTOM", "my_log_msg", #{
+        topic => Topic,
+        rule_ids => maps:from_keys([<<"a">>, <<"b">>, <<"c">>], true)
+    }),
     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")),
@@ -301,6 +310,16 @@ t_http_test_json_formatter(_Config) ->
                         <<"key2">> := <<"value2">>
                         <<"key2">> := <<"value2">>
                     }
                     }
                 }
                 }
+            },
+            #{
+                <<"meta">> := #{
+                    <<"client_ids">> := [<<"a">>, <<"b">>, <<"c">>]
+                }
+            },
+            #{
+                <<"meta">> := #{
+                    <<"rule_ids">> := [<<"a">>, <<"b">>, <<"c">>]
+                }
             }
             }
             | _
             | _
         ],
         ],