refactor(prometheus): generic api response funcs
This commit is contained in:
parent
e0feb580b6
commit
bf2e4d134a
|
@ -19,6 +19,7 @@
|
||||||
-behaviour(minirest_api).
|
-behaviour(minirest_api).
|
||||||
|
|
||||||
-include_lib("hocon/include/hoconsc.hrl").
|
-include_lib("hocon/include/hoconsc.hrl").
|
||||||
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
api_spec/0,
|
api_spec/0,
|
||||||
|
@ -115,37 +116,40 @@ setting(put, #{body := Body}) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
stats(get, #{headers := Headers}) ->
|
stats(get, #{headers := Headers}) ->
|
||||||
Type =
|
collect(emqx_prometheus, Headers).
|
||||||
case maps:get(<<"accept">>, Headers, <<"text/plain">>) of
|
|
||||||
<<"application/json">> -> <<"json">>;
|
|
||||||
_ -> <<"prometheus">>
|
|
||||||
end,
|
|
||||||
Data = emqx_prometheus:collect(Type),
|
|
||||||
case Type of
|
|
||||||
<<"json">> ->
|
|
||||||
{200, Data};
|
|
||||||
<<"prometheus">> ->
|
|
||||||
{200, #{<<"content-type">> => <<"text/plain">>}, Data}
|
|
||||||
end.
|
|
||||||
|
|
||||||
auth(get, #{headers := Headers}) ->
|
auth(get, #{headers := Headers}) ->
|
||||||
Type =
|
collect(emqx_prometheus_auth, Headers).
|
||||||
case maps:get(<<"accept">>, Headers, <<"text/plain">>) of
|
|
||||||
<<"application/json">> -> <<"json">>;
|
|
||||||
_ -> <<"prometheus">>
|
|
||||||
end,
|
|
||||||
Data = emqx_prometheus_auth:collect(Type),
|
|
||||||
case Type of
|
|
||||||
<<"json">> ->
|
|
||||||
{200, Data};
|
|
||||||
<<"prometheus">> ->
|
|
||||||
{200, #{<<"content-type">> => <<"text/plain">>}, Data}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal funcs
|
%% Internal funcs
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
|
collect(Module, Headers) ->
|
||||||
|
Type = response_type(Headers),
|
||||||
|
Data =
|
||||||
|
case erlang:function_exported(Module, collect, 1) of
|
||||||
|
true ->
|
||||||
|
erlang:apply(Module, collect, [Type]);
|
||||||
|
false ->
|
||||||
|
?SLOG(error, #{
|
||||||
|
msg => "prometheus callback module not found, empty data responded",
|
||||||
|
module_name => Module
|
||||||
|
}),
|
||||||
|
<<>>
|
||||||
|
end,
|
||||||
|
gen_response(Type, Data).
|
||||||
|
|
||||||
|
response_type(#{<<"accept">> := <<"application/json">>}) ->
|
||||||
|
<<"json">>;
|
||||||
|
response_type(_) ->
|
||||||
|
<<"prometheus">>.
|
||||||
|
|
||||||
|
gen_response(<<"json">>, Data) ->
|
||||||
|
{200, Data};
|
||||||
|
gen_response(<<"prometheus">>, Data) ->
|
||||||
|
{200, #{<<"content-type">> => <<"text/plain">>}, Data}.
|
||||||
|
|
||||||
prometheus_setting_request() ->
|
prometheus_setting_request() ->
|
||||||
[{prometheus, #{type := Setting}}] = emqx_prometheus_schema:roots(),
|
[{prometheus, #{type := Setting}}] = emqx_prometheus_schema:roots(),
|
||||||
emqx_dashboard_swagger:schema_with_examples(
|
emqx_dashboard_swagger:schema_with_examples(
|
||||||
|
|
Loading…
Reference in New Issue