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

View File

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