fix(trace): download trace api not working

This commit is contained in:
zhongwencool 2021-12-29 19:03:54 +08:00
parent 121d906992
commit 04313dc044
4 changed files with 24 additions and 57 deletions

View File

@ -312,6 +312,9 @@ responses(Responses, Module) ->
response(Status, Bin, {Acc, RefsAcc, Module}) when is_binary(Bin) -> response(Status, Bin, {Acc, RefsAcc, Module}) when is_binary(Bin) ->
{Acc#{integer_to_binary(Status) => #{description => Bin}}, RefsAcc, Module}; {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, ?REF(StructName), {Acc, RefsAcc, Module}) ->
response(Status, ?R_REF(Module, StructName), {Acc, RefsAcc, Module}); response(Status, ?R_REF(Module, StructName), {Acc, RefsAcc, Module});
response(Status, ?R_REF(_Mod, _Name) = RRef, {Acc, RefsAcc, Module}) -> response(Status, ?R_REF(_Mod, _Name) = RRef, {Acc, RefsAcc, Module}) ->

View File

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

View File

@ -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"
# }
]

View File

@ -55,7 +55,7 @@
, {mria, {git, "https://github.com/emqx/mria", {tag, "0.1.5"}}} , {mria, {git, "https://github.com/emqx/mria", {tag, "0.1.5"}}}
, {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.11.1"}}} , {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.11.1"}}}
, {gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.5.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"}}} , {ecpool, {git, "https://github.com/emqx/ecpool", {tag, "0.5.1"}}}
, {replayq, "0.3.3"} , {replayq, "0.3.3"}
, {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}} , {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}}