Просмотр исходного кода

fix(trace): download trace api not working

zhongwencool 4 лет назад
Родитель
Сommit
04313dc044

+ 3 - 0
apps/emqx_dashboard/src/emqx_dashboard_swagger.erl

@@ -312,6 +312,9 @@ responses(Responses, Module) ->
 
 response(Status, Bin, {Acc, RefsAcc, Module}) when is_binary(Bin) ->
     {Acc#{integer_to_binary(Status) => #{description => Bin}}, RefsAcc, Module};
+%% Support swagger raw object(file download).
+response(Status, #{content := _} = Content, {Acc, RefsAcc, Module}) ->
+    {Acc#{integer_to_binary(Status) => Content}, RefsAcc, Module};
 response(Status, ?REF(StructName), {Acc, RefsAcc, Module}) ->
     response(Status, ?R_REF(Module, StructName), {Acc, RefsAcc, Module});
 response(Status, ?R_REF(_Mod, _Name) = RRef, {Acc, RefsAcc, Module}) ->

+ 20 - 6
apps/emqx_management/src/emqx_mgmt_api_trace.erl

@@ -107,9 +107,14 @@ schema("/trace/:name/download") ->
         get => #{
             description => "Download trace log by name",
             parameters => [hoconsc:ref(name)],
-            %% todo zip file octet-stream
             responses => #{
-                200 => <<"TODO octet-stream">>
+                200 =>
+                #{description => "A trace zip file",
+                    content => #{
+                        'application/octet-stream' =>
+                             #{schema => #{type => "string", format => "binary"}}
+                    }
+                }
             }
         }
     };
@@ -126,7 +131,11 @@ schema("/trace/:name/log") ->
             ],
             %% todo response data
             responses => #{
-                200 => <<"TODO">>
+                200 =>
+                [
+                    {items, hoconsc:mk(binary(), #{example => "BinBinBin"})}
+                    | fields(bytes) ++ fields(position)
+                ]
             }
         }
     }.
@@ -209,6 +218,7 @@ fields(position) ->
             default => 0
         })}].
 
+
 -define(NAME_RE, "^[A-Za-z]+[A-Za-z0-9-_]*$").
 
 validate_name(Name) ->
@@ -296,7 +306,12 @@ download_trace_log(get, #{bindings := #{name := Name}}) ->
             ZipFileName = ZipDir ++ binary_to_list(Name) ++ ".zip",
             {ok, ZipFile} = zip:zip(ZipFileName, Zips, [{cwd, ZipDir}]),
             emqx_trace:delete_files_after_send(ZipFileName, Zips),
-            {200, ZipFile};
+            Headers = #{
+                <<"content-type">> => <<"application/octet-stream">>,
+                <<"content-disposition">> =>
+                iolist_to_binary("attachment; filename=" ++ filename:basename(ZipFile))
+            },
+            {200, Headers, {file, ZipFile}};
         {error, not_found} -> ?NOT_FOUND(Name)
     end.
 
@@ -324,11 +339,10 @@ cluster_call(Mod, Fun, Args, Timeout) ->
     BadNodes =/= [] andalso ?LOG(error, "rpc call failed on ~p ~p", [BadNodes, {Mod, Fun, Args}]),
     GoodRes.
 
-stream_log_file(get, #{bindings := #{name := Name}, query_string := Query} = T) ->
+stream_log_file(get, #{bindings := #{name := Name}, query_string := Query}) ->
     Node0 = maps:get(<<"node">>, Query, atom_to_binary(node())),
     Position = maps:get(<<"position">>, Query, 0),
     Bytes = maps:get(<<"bytes">>, Query, 1000),
-    logger:error("~p", [T]),
     case to_node(Node0) of
         {ok, Node} ->
             case rpc:call(Node, ?MODULE, read_trace_file, [Name, Position, Bytes]) of

+ 0 - 50
apps/emqx_modules/etc/emqx_modules.conf.rendered

@@ -1,50 +0,0 @@
-
-delayed {
-    enable = true
-    ## 0 is no limit
-    max_delayed_messages = 0
-}
-
-observer_cli {
-    enable = true
-}
-
-telemetry {
-    enable = true
-}
-
-event_message {
-    "$event/client_connected" = true
-    "$event/client_disconnected" = true
-    # "$event/client_subscribed": false
-    # "$event/client_unsubscribed": false
-    # "$event/message_delivered": false
-    # "$event/message_acked": false
-    # "$event/message_dropped": false
-}
-
-topic_metrics: [
-    #{topic: "test/1"}
-]
-
-rewrite: [
-    # {
-    #     action = publish
-    #     source_topic = "x/#"
-    #     re = "^x/y/(.+)$"
-    #     dest_topic = "z/y/$1"
-    # },
-    # {
-    #     action = subscribe
-    #     source_topic = "x1/#"
-    #     re = "^x1/y/(.+)$"
-    #     dest_topic = "z1/y/$1"
-    # },
-    # {
-    #     action = all
-    #     source_topic = "x2/#"
-    #     re = "^x2/y/(.+)$"
-    #     dest_topic = "z2/y/$1"
-    # }
-]
-

+ 1 - 1
rebar.config

@@ -55,7 +55,7 @@
     , {mria, {git, "https://github.com/emqx/mria", {tag, "0.1.5"}}}
     , {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.11.1"}}}
     , {gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.5.1"}}}
-    , {minirest, {git, "https://github.com/emqx/minirest", {tag, "1.2.7"}}}
+    , {minirest, {git, "https://github.com/emqx/minirest", {tag, "1.2.9"}}}
     , {ecpool, {git, "https://github.com/emqx/ecpool", {tag, "0.5.1"}}}
     , {replayq, "0.3.3"}
     , {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}}