feat(prometheus): add get prometheus_format stats api

This commit is contained in:
Turtle 2021-08-24 20:13:20 +08:00 committed by turtleDeng
parent bc325e55fc
commit a1ae4457df
2 changed files with 31 additions and 48 deletions

View File

@ -125,13 +125,13 @@ collect(<<"json">>) ->
Metrics = emqx_metrics:all(), Metrics = emqx_metrics:all(),
Stats = emqx_stats:getstats(), Stats = emqx_stats:getstats(),
VMData = emqx_vm_data(), VMData = emqx_vm_data(),
[{stats, [collect_stats(Name, Stats) || Name <- emqx_stats()]}, #{stats => maps:from_list([collect_stats(Name, Stats) || Name <- emqx_stats()]),
{metrics, [collect_stats(Name, VMData) || Name <- emqx_vm()]}, metrics => maps:from_list([collect_stats(Name, VMData) || Name <- emqx_vm()]),
{packets, [collect_stats(Name, Metrics) || Name <- emqx_metrics_packets()]}, packets => maps:from_list([collect_stats(Name, Metrics) || Name <- emqx_metrics_packets()]),
{messages, [collect_stats(Name, Metrics) || Name <- emqx_metrics_messages()]}, messages => maps:from_list([collect_stats(Name, Metrics) || Name <- emqx_metrics_messages()]),
{delivery, [collect_stats(Name, Metrics) || Name <- emqx_metrics_delivery()]}, delivery => maps:from_list([collect_stats(Name, Metrics) || Name <- emqx_metrics_delivery()]),
{client, [collect_stats(Name, Metrics) || Name <- emqx_metrics_client()]}, client => maps:from_list([collect_stats(Name, Metrics) || Name <- emqx_metrics_client()]),
{session, [collect_stats(Name, Metrics) || Name <- emqx_metrics_session()]}]; session => maps:from_list([collect_stats(Name, Metrics) || Name <- emqx_metrics_session()])};
collect(<<"prometheus">>) -> collect(<<"prometheus">>) ->
prometheus_text_format:format(). prometheus_text_format:format().

View File

@ -20,17 +20,16 @@
-include("emqx_prometheus.hrl"). -include("emqx_prometheus.hrl").
-import(emqx_mgmt_util, [ schema/1 -import(emqx_mgmt_util, [ schema/1]).
, bad_request/0]).
-export([api_spec/0]). -export([api_spec/0]).
-export([ prometheus/2 -export([ prometheus/2
% , stats/2 , stats/2
]). ]).
api_spec() -> api_spec() ->
{[prometheus_api()], schemas()}. {[prometheus_api(), prometheus_data_api()], schemas()}.
schemas() -> schemas() ->
[#{prometheus => emqx_mgmt_api_configs:gen_schema(emqx:get_raw_config([prometheus]))}]. [#{prometheus => emqx_mgmt_api_configs:gen_schema(emqx:get_raw_config([prometheus]))}].
@ -44,38 +43,24 @@ prometheus_api() ->
put => #{ put => #{
description => <<"Update Prometheus">>, description => <<"Update Prometheus">>,
'requestBody' => schema(prometheus), 'requestBody' => schema(prometheus),
responses => #{ responses => #{<<"200">> => schema(prometheus)}
<<"200">> => schema(prometheus),
<<"400">> => bad_request()
}
} }
}, },
{"/prometheus", Metadata, prometheus}. {"/prometheus", Metadata, prometheus}.
% prometheus_data_api() -> prometheus_data_api() ->
% Metadata = #{ Metadata = #{
% get => #{ get => #{
% description => <<"Get Prometheus Data">>, description => <<"Get Prometheus Data">>,
% parameters => [#{ parameters => [#{
% name => format_type, name => format_type,
% in => path, in => path,
% schema => #{type => string} schema => #{type => string}
% }], }],
% responses => #{ responses => #{<<"200">> => schema(#{type => object})}
% <<"200">> => }
% response_schema(<<"Update Prometheus successfully">>), },
% <<"400">> => {"/prometheus/stats", Metadata, stats}.
% response_schema(<<"Bad Request">>, #{
% type => object,
% properties => #{
% message => #{type => string},
% code => #{type => string}
% }
% })
% }
% }
% },
% {"/prometheus/stats", Metadata, stats}.
prometheus(get, _Params) -> prometheus(get, _Params) ->
{200, emqx:get_raw_config([<<"prometheus">>], #{})}; {200, emqx:get_raw_config([<<"prometheus">>], #{})};
@ -92,12 +77,10 @@ prometheus(put, #{body := Body}) ->
end, end,
{200, emqx:get_raw_config([<<"prometheus">>], #{})}. {200, emqx:get_raw_config([<<"prometheus">>], #{})}.
% stats(_Bindings, Params) -> stats(get, #{query_string := Qs}) ->
% Type = proplists:get_value(<<"format_type">>, Params, <<"json">>), Type = maps:get(<<"format_type">>, Qs, <<"json">>),
% Data = emqx_prometheus:collect(Type), Data = emqx_prometheus:collect(Type),
% case Type of case Type of
% <<"json">> -> <<"json">> -> {200, Data};
% {ok, Data}; <<"prometheus">> -> {200, #{<<"content-type">> => <<"text/plain">>}, Data}
% <<"prometheus">> -> end.
% {ok, #{<<"content-type">> => <<"text/plain">>}, Data}
% end.