chore(HTTP): prometheus or json format by accept header

This commit is contained in:
zhongwencool 2021-12-02 10:30:50 +08:00
parent 25c4f4aa4e
commit f6c61189c4
1 changed files with 13 additions and 3 deletions

View File

@ -52,7 +52,13 @@ prometheus_data_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => <<"Get Prometheus Data">>, description => <<"Get Prometheus Data">>,
responses => #{<<"200">> => schema(#{type => object})} responses => #{<<"200">> =>
#{content =>
#{
'application/json' => #{schema => #{type => object}},
'text/plain' => #{schema => #{type => string}}
}}
}
} }
}, },
{"/prometheus/stats", Metadata, stats}. {"/prometheus/stats", Metadata, stats}.
@ -72,8 +78,12 @@ prometheus(put, #{body := Body}) ->
end, end,
{200, emqx:get_raw_config([<<"prometheus">>], #{})}. {200, emqx:get_raw_config([<<"prometheus">>], #{})}.
stats(get, #{query_string := Qs}) -> stats(get, #{headers := Headers}) ->
Type = maps:get(<<"format_type">>, Qs, <<"json">>), Type =
case maps:get(<<"accept">>, Headers, <<"text/plain">>) of
<<"application/json">> -> <<"json">>;
_ -> <<"prometheus">>
end,
Data = emqx_prometheus:collect(Type), Data = emqx_prometheus:collect(Type),
case Type of case Type of
<<"json">> -> {200, Data}; <<"json">> -> {200, Data};