refactor(prometheus): refactor prometheus swagger schema

This commit is contained in:
Turtle 2021-08-21 14:15:30 +08:00 committed by turtleDeng
parent 91f787533d
commit d4d4ba9ea4
2 changed files with 21 additions and 46 deletions

View File

@ -179,14 +179,12 @@ gen_schema(Conf) when is_binary(Conf); is_atom(Conf) ->
gen_schema(Conf) when is_number(Conf) -> gen_schema(Conf) when is_number(Conf) ->
with_default_value(#{type => number}, Conf); with_default_value(#{type => number}, Conf);
gen_schema(Conf) when is_list(Conf) -> gen_schema(Conf) when is_list(Conf) ->
#{type => array, items => case Conf of
[] -> #{}; %% don't know the type
_ ->
case io_lib:printable_unicode_list(Conf) of case io_lib:printable_unicode_list(Conf) of
true -> gen_schema(unicode:characters_to_binary(Conf)); true ->
false -> gen_schema(hd(Conf)) gen_schema(unicode:characters_to_binary(Conf));
end false ->
end}; #{type => array, items => gen_schema(hd(Conf))}
end;
gen_schema(Conf) when is_map(Conf) -> gen_schema(Conf) when is_map(Conf) ->
#{type => object, properties => #{type => object, properties =>
maps:map(fun(_K, V) -> gen_schema(V) end, Conf)}; maps:map(fun(_K, V) -> gen_schema(V) end, Conf)};

View File

@ -20,8 +20,7 @@
-include("emqx_prometheus.hrl"). -include("emqx_prometheus.hrl").
-import(emqx_mgmt_util, [ response_schema/1 -import(emqx_mgmt_util, [ response_schema/2
, response_schema/2
, request_body_schema/1 , request_body_schema/1
]). ]).
@ -35,38 +34,21 @@ api_spec() ->
{[prometheus_api()], schemas()}. {[prometheus_api()], schemas()}.
schemas() -> schemas() ->
[#{prometheus => #{ [#{prometheus => emqx_mgmt_api_configs:gen_schema(emqx:get_raw_config([prometheus]))}].
type => object,
properties => #{
push_gateway_server => #{
type => string,
description => <<"prometheus PushGateway Server">>,
example => get_raw(<<"push_gateway_server">>, <<"http://127.0.0.1:9091">>)},
interval => #{
type => string,
description => <<"Interval">>,
example => get_raw(<<"interval">>, <<"15s">>)},
enable => #{
type => boolean,
description => <<"Prometheus status">>,
example => get_raw(<<"enable">>, false)}
}
}}].
prometheus_api() -> prometheus_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => <<"Get Prometheus info">>, description => <<"Get Prometheus info">>,
responses => #{ responses => #{
<<"200">> => response_schema(prometheus) <<"200">> => response_schema(<<>>, prometheus)
} }
}, },
put => #{ put => #{
description => <<"Update Prometheus">>, description => <<"Update Prometheus">>,
'requestBody' => request_body_schema(prometheus), 'requestBody' => request_body_schema(prometheus),
responses => #{ responses => #{
<<"200">> => <<"200">> =>response_schema(<<>>, prometheus),
response_schema(<<"Update Prometheus successfully">>),
<<"400">> => <<"400">> =>
response_schema(<<"Bad Request">>, #{ response_schema(<<"Bad Request">>, #{
type => object, type => object,
@ -106,15 +88,21 @@ prometheus_api() ->
% {"/prometheus/stats", Metadata, stats}. % {"/prometheus/stats", Metadata, stats}.
prometheus(get, _Request) -> prometheus(get, _Request) ->
Response = emqx:get_raw_config([<<"prometheus">>], #{}), {200, emqx:get_raw_config([<<"prometheus">>], #{})};
{200, Response};
prometheus(put, Request) -> prometheus(put, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
Params = emqx_json:decode(Body, [return_maps]), Params = emqx_json:decode(Body, [return_maps]),
Enable = maps:get(<<"enable">>, Params), {ok, Config} = emqx:update_config([prometheus], Params),
{ok, _} = emqx:update_config([prometheus], Params), case maps:get(<<"enable">>, Params) of
enable_prometheus(Enable). true ->
_ = emqx_prometheus_sup:stop_child(?APP),
emqx_prometheus_sup:start_child(?APP, maps:get(config, Config));
false ->
_ = emqx_prometheus_sup:stop_child(?APP),
ok
end,
{200, emqx:get_raw_config([<<"prometheus">>], #{})}.
% stats(_Bindings, Params) -> % stats(_Bindings, Params) ->
% Type = proplists:get_value(<<"format_type">>, Params, <<"json">>), % Type = proplists:get_value(<<"format_type">>, Params, <<"json">>),
@ -125,14 +113,3 @@ prometheus(put, Request) ->
% <<"prometheus">> -> % <<"prometheus">> ->
% {ok, #{<<"content-type">> => <<"text/plain">>}, Data} % {ok, #{<<"content-type">> => <<"text/plain">>}, Data}
% end. % end.
enable_prometheus(true) ->
ok = emqx_prometheus_sup:stop_child(?APP),
emqx_prometheus_sup:start_child(?APP, emqx:get_config([prometheus], #{})),
{200};
enable_prometheus(false) ->
_ = emqx_prometheus_sup:stop_child(?APP),
{200}.
get_raw(Key, Def) ->
emqx:get_raw_config([<<"prometheus">>] ++ [Key], Def).