fix: only return recommend setting when get prometheus api

This commit is contained in:
zhongwencool 2023-12-11 14:49:32 +08:00
parent 107efda78f
commit 525a9e0adf
5 changed files with 55 additions and 25 deletions

View File

@ -24,6 +24,7 @@
-export([add_handler/0, remove_handler/0]). -export([add_handler/0, remove_handler/0]).
-export([pre_config_update/3, post_config_update/5]). -export([pre_config_update/3, post_config_update/5]).
-export([regenerate_minirest_dispatch/0]). -export([regenerate_minirest_dispatch/0]).
-export([delay_job/1]).
-behaviour(gen_server). -behaviour(gen_server).
@ -68,7 +69,7 @@ handle_call(_Request, _From, State) ->
handle_cast(_Request, State) -> handle_cast(_Request, State) ->
{noreply, State, hibernate}. {noreply, State, hibernate}.
handle_info(i18n_lang_changed, _State) -> handle_info(regenerate, _State) ->
NewState = regenerate_minirest_dispatch(), NewState = regenerate_minirest_dispatch(),
{noreply, NewState, hibernate}; {noreply, NewState, hibernate};
handle_info({update_listeners, OldListeners, NewListeners}, _State) -> handle_info({update_listeners, OldListeners, NewListeners}, _State) ->
@ -146,7 +147,7 @@ remove_sensitive_data(Conf0) ->
end. end.
post_config_update(_, {change_i18n_lang, _}, _NewConf, _OldConf, _AppEnvs) -> post_config_update(_, {change_i18n_lang, _}, _NewConf, _OldConf, _AppEnvs) ->
delay_job(i18n_lang_changed); delay_job(regenerate);
post_config_update(_, _Req, NewConf, OldConf, _AppEnvs) -> post_config_update(_, _Req, NewConf, OldConf, _AppEnvs) ->
OldHttp = get_listener(http, OldConf), OldHttp = get_listener(http, OldConf),
OldHttps = get_listener(https, OldConf), OldHttps = get_listener(https, OldConf),

View File

@ -50,15 +50,15 @@ schema("/prometheus") ->
description => ?DESC(get_prom_conf_info), description => ?DESC(get_prom_conf_info),
tags => ?TAGS, tags => ?TAGS,
responses => responses =>
#{200 => prometheus_setting_schema()} #{200 => prometheus_setting_response()}
}, },
put => put =>
#{ #{
description => ?DESC(update_prom_conf_info), description => ?DESC(update_prom_conf_info),
tags => ?TAGS, tags => ?TAGS,
'requestBody' => prometheus_setting_schema(), 'requestBody' => prometheus_setting_request(),
responses => responses =>
#{200 => prometheus_setting_schema()} #{200 => prometheus_setting_response()}
} }
}; };
schema("/prometheus/stats") -> schema("/prometheus/stats") ->
@ -84,7 +84,13 @@ security() ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
setting(get, _Params) -> setting(get, _Params) ->
{200, emqx:get_raw_config([<<"prometheus">>], #{})}; Raw = emqx:get_raw_config([<<"prometheus">>], #{}),
Conf =
case emqx_prometheus_schema:is_recommend_type(Raw) of
true -> Raw;
false -> emqx_prometheus_config:to_recommend_type(Raw)
end,
{200, Conf};
setting(put, #{body := Body}) -> setting(put, #{body := Body}) ->
case emqx_prometheus_config:update(Body) of case emqx_prometheus_config:update(Body) of
{ok, NewConfig} -> {ok, NewConfig} ->
@ -112,7 +118,7 @@ stats(get, #{headers := Headers}) ->
%% Internal funcs %% Internal funcs
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
prometheus_setting_schema() -> 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(
Setting, Setting,
@ -122,6 +128,14 @@ prometheus_setting_schema() ->
] ]
). ).
%% Always return recommend setting
prometheus_setting_response() ->
{_, #{value := Example}} = recommend_setting_example(),
emqx_dashboard_swagger:schema_with_example(
?R_REF(emqx_prometheus_schema, recommend_setting),
Example
).
legacy_setting_example() -> legacy_setting_example() ->
Summary = <<"legacy_deprecated_setting">>, Summary = <<"legacy_deprecated_setting">>,
{Summary, #{ {Summary, #{

View File

@ -23,6 +23,7 @@
-export([pre_config_update/3, post_config_update/5]). -export([pre_config_update/3, post_config_update/5]).
-export([update/1]). -export([update/1]).
-export([conf/0, is_push_gateway_server_enabled/1]). -export([conf/0, is_push_gateway_server_enabled/1]).
-export([to_recommend_type/1]).
update(Config) -> update(Config) ->
case case
@ -60,7 +61,8 @@ pre_config_update(?PROMETHEUS, MergeConf, OriginConf) ->
to_recommend_type(Conf) -> to_recommend_type(Conf) ->
#{ #{
<<"push_gateway">> => to_push_gateway(Conf), <<"push_gateway">> => to_push_gateway(Conf),
<<"collectors">> => to_collectors(Conf) <<"collectors">> => to_collectors(Conf),
<<"enable_basic_auth">> => false
}. }.
to_push_gateway(Conf) -> to_push_gateway(Conf) ->
@ -123,7 +125,7 @@ update_push_gateway(Prometheus) ->
end. end.
update_auth(#{enable_basic_auth := New}, #{enable_basic_auth := Old}) when New =/= Old -> update_auth(#{enable_basic_auth := New}, #{enable_basic_auth := Old}) when New =/= Old ->
emqx_dashboard_listener:regenerate_minirest_dispatch(), emqx_dashboard_listener:delay_job(regenerate),
ok; ok;
update_auth(_, _) -> update_auth(_, _) ->
ok. ok.

View File

@ -78,7 +78,7 @@ groups() ->
]. ].
suite() -> suite() ->
[{timetrap, {seconds, 30}}]. [{timetrap, {seconds, 60}}].
common_tests() -> common_tests() ->
emqx_common_test_helpers:all(?MODULE). emqx_common_test_helpers:all(?MODULE).

View File

@ -80,29 +80,42 @@ set_special_configs(_App, _) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Cases %% Cases
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% we return recommend config for prometheus even if prometheus is legacy.
t_legacy_prometheus_api(_) -> t_legacy_prometheus_api(_) ->
Path = emqx_mgmt_api_test_util:api_path(["prometheus"]), Path = emqx_mgmt_api_test_util:api_path(["prometheus"]),
Auth = emqx_mgmt_api_test_util:auth_header_(), Auth = emqx_mgmt_api_test_util:auth_header_(),
{ok, Response} = emqx_mgmt_api_test_util:request_api(get, Path, "", Auth), {ok, Response} = emqx_mgmt_api_test_util:request_api(get, Path, "", Auth),
OldConf = emqx:get_raw_config([prometheus]),
Conf = emqx_utils_json:decode(Response, [return_maps]), Conf = emqx_utils_json:decode(Response, [return_maps]),
%% Always return new config.
?assertMatch( ?assertMatch(
#{ #{
<<"push_gateway_server">> := _, <<"collectors">> :=
<<"interval">> := _, #{
<<"enable">> := _, <<"mnesia">> := <<"disabled">>,
<<"vm_statistics_collector">> := _, <<"vm_dist">> := <<"disabled">>,
<<"vm_system_info_collector">> := _, <<"vm_memory">> := <<"disabled">>,
<<"vm_memory_collector">> := _, <<"vm_msacc">> := <<"disabled">>,
<<"vm_msacc_collector">> := _, <<"vm_statistics">> := <<"disabled">>,
<<"headers">> := _ <<"vm_system_info">> := <<"disabled">>
},
<<"enable_basic_auth">> := false,
<<"push_gateway">> :=
#{
<<"enable">> := true,
<<"headers">> := #{<<"Authorization">> := <<"some-authz-tokens">>},
<<"interval">> := <<"1s">>,
<<"job_name">> := <<"${name}~${host}">>,
<<"url">> := <<"http://127.0.0.1:9091">>
}
}, },
Conf Conf
), ),
#{<<"enable">> := Enable} = Conf, #{<<"push_gateway">> := #{<<"enable">> := Enable}} = Conf,
?assertEqual(Enable, undefined =/= erlang:whereis(emqx_prometheus)), ?assertEqual(Enable, undefined =/= erlang:whereis(emqx_prometheus)),
NewConf = Conf#{ NewConf = OldConf#{
<<"interval">> => <<"2s">>, <<"interval">> => <<"2s">>,
<<"vm_statistics_collector">> => <<"enabled">>, <<"vm_statistics_collector">> => <<"enabled">>,
<<"headers">> => #{ <<"headers">> => #{
@ -113,7 +126,7 @@ t_legacy_prometheus_api(_) ->
{ok, Response2} = emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, NewConf), {ok, Response2} = emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, NewConf),
Conf2 = emqx_utils_json:decode(Response2, [return_maps]), Conf2 = emqx_utils_json:decode(Response2, [return_maps]),
?assertMatch(NewConf, Conf2), ?assertEqual(NewConf, Conf2),
EnvCollectors = application:get_env(prometheus, collectors, []), EnvCollectors = application:get_env(prometheus, collectors, []),
PromCollectors = prometheus_registry:collectors(default), PromCollectors = prometheus_registry:collectors(default),
@ -153,11 +166,11 @@ t_legacy_prometheus_api(_) ->
emqx_config:get([prometheus]) emqx_config:get([prometheus])
), ),
NewConf1 = Conf#{<<"enable">> => (not Enable)}, NewConf1 = OldConf#{<<"enable">> => (not Enable)},
{ok, _Response3} = emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, NewConf1), {ok, _Response3} = emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, NewConf1),
?assertEqual((not Enable), undefined =/= erlang:whereis(emqx_prometheus)), ?assertEqual((not Enable), undefined =/= erlang:whereis(emqx_prometheus)),
ConfWithoutScheme = Conf#{<<"push_gateway_server">> => "127.0.0.1:8081"}, ConfWithoutScheme = OldConf#{<<"push_gateway_server">> => "127.0.0.1:8081"},
?assertMatch( ?assertMatch(
{error, {"HTTP/1.1", 400, _}}, {error, {"HTTP/1.1", 400, _}},
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, ConfWithoutScheme) emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, ConfWithoutScheme)
@ -267,8 +280,7 @@ t_stats_no_auth_api(_) ->
%% undefined is legacy prometheus %% undefined is legacy prometheus
case emqx:get_config([prometheus, enable_basic_auth], undefined) of case emqx:get_config([prometheus, enable_basic_auth], undefined) of
true -> true ->
{ok, _} = emqx:update_config([prometheus, enable_basic_auth], false), {ok, _} = emqx:update_config([prometheus, enable_basic_auth], false);
emqx_dashboard_listener:regenerate_minirest_dispatch();
_ -> _ ->
ok ok
end, end,
@ -278,6 +290,7 @@ t_stats_no_auth_api(_) ->
t_stats_auth_api(_) -> t_stats_auth_api(_) ->
{ok, _} = emqx:update_config([prometheus, enable_basic_auth], true), {ok, _} = emqx:update_config([prometheus, enable_basic_auth], true),
emqx_dashboard_listener:regenerate_minirest_dispatch(),
Auth = emqx_mgmt_api_test_util:auth_header_(), Auth = emqx_mgmt_api_test_util:auth_header_(),
JsonAuth = [{"accept", "application/json"}, Auth], JsonAuth = [{"accept", "application/json"}, Auth],
request_stats(JsonAuth, Auth), request_stats(JsonAuth, Auth),