fix: only return recommend setting when get prometheus api
This commit is contained in:
parent
107efda78f
commit
525a9e0adf
|
@ -24,6 +24,7 @@
|
|||
-export([add_handler/0, remove_handler/0]).
|
||||
-export([pre_config_update/3, post_config_update/5]).
|
||||
-export([regenerate_minirest_dispatch/0]).
|
||||
-export([delay_job/1]).
|
||||
|
||||
-behaviour(gen_server).
|
||||
|
||||
|
@ -68,7 +69,7 @@ handle_call(_Request, _From, State) ->
|
|||
handle_cast(_Request, State) ->
|
||||
{noreply, State, hibernate}.
|
||||
|
||||
handle_info(i18n_lang_changed, _State) ->
|
||||
handle_info(regenerate, _State) ->
|
||||
NewState = regenerate_minirest_dispatch(),
|
||||
{noreply, NewState, hibernate};
|
||||
handle_info({update_listeners, OldListeners, NewListeners}, _State) ->
|
||||
|
@ -146,7 +147,7 @@ remove_sensitive_data(Conf0) ->
|
|||
end.
|
||||
|
||||
post_config_update(_, {change_i18n_lang, _}, _NewConf, _OldConf, _AppEnvs) ->
|
||||
delay_job(i18n_lang_changed);
|
||||
delay_job(regenerate);
|
||||
post_config_update(_, _Req, NewConf, OldConf, _AppEnvs) ->
|
||||
OldHttp = get_listener(http, OldConf),
|
||||
OldHttps = get_listener(https, OldConf),
|
||||
|
|
|
@ -50,15 +50,15 @@ schema("/prometheus") ->
|
|||
description => ?DESC(get_prom_conf_info),
|
||||
tags => ?TAGS,
|
||||
responses =>
|
||||
#{200 => prometheus_setting_schema()}
|
||||
#{200 => prometheus_setting_response()}
|
||||
},
|
||||
put =>
|
||||
#{
|
||||
description => ?DESC(update_prom_conf_info),
|
||||
tags => ?TAGS,
|
||||
'requestBody' => prometheus_setting_schema(),
|
||||
'requestBody' => prometheus_setting_request(),
|
||||
responses =>
|
||||
#{200 => prometheus_setting_schema()}
|
||||
#{200 => prometheus_setting_response()}
|
||||
}
|
||||
};
|
||||
schema("/prometheus/stats") ->
|
||||
|
@ -84,7 +84,13 @@ security() ->
|
|||
%%--------------------------------------------------------------------
|
||||
|
||||
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}) ->
|
||||
case emqx_prometheus_config:update(Body) of
|
||||
{ok, NewConfig} ->
|
||||
|
@ -112,7 +118,7 @@ stats(get, #{headers := Headers}) ->
|
|||
%% Internal funcs
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
prometheus_setting_schema() ->
|
||||
prometheus_setting_request() ->
|
||||
[{prometheus, #{type := Setting}}] = emqx_prometheus_schema:roots(),
|
||||
emqx_dashboard_swagger:schema_with_examples(
|
||||
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() ->
|
||||
Summary = <<"legacy_deprecated_setting">>,
|
||||
{Summary, #{
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
-export([pre_config_update/3, post_config_update/5]).
|
||||
-export([update/1]).
|
||||
-export([conf/0, is_push_gateway_server_enabled/1]).
|
||||
-export([to_recommend_type/1]).
|
||||
|
||||
update(Config) ->
|
||||
case
|
||||
|
@ -60,7 +61,8 @@ pre_config_update(?PROMETHEUS, MergeConf, OriginConf) ->
|
|||
to_recommend_type(Conf) ->
|
||||
#{
|
||||
<<"push_gateway">> => to_push_gateway(Conf),
|
||||
<<"collectors">> => to_collectors(Conf)
|
||||
<<"collectors">> => to_collectors(Conf),
|
||||
<<"enable_basic_auth">> => false
|
||||
}.
|
||||
|
||||
to_push_gateway(Conf) ->
|
||||
|
@ -123,7 +125,7 @@ update_push_gateway(Prometheus) ->
|
|||
end.
|
||||
|
||||
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;
|
||||
update_auth(_, _) ->
|
||||
ok.
|
||||
|
|
|
@ -78,7 +78,7 @@ groups() ->
|
|||
].
|
||||
|
||||
suite() ->
|
||||
[{timetrap, {seconds, 30}}].
|
||||
[{timetrap, {seconds, 60}}].
|
||||
|
||||
common_tests() ->
|
||||
emqx_common_test_helpers:all(?MODULE).
|
||||
|
|
|
@ -80,29 +80,42 @@ set_special_configs(_App, _) ->
|
|||
%%--------------------------------------------------------------------
|
||||
%% Cases
|
||||
%%--------------------------------------------------------------------
|
||||
%% we return recommend config for prometheus even if prometheus is legacy.
|
||||
t_legacy_prometheus_api(_) ->
|
||||
Path = emqx_mgmt_api_test_util:api_path(["prometheus"]),
|
||||
Auth = emqx_mgmt_api_test_util:auth_header_(),
|
||||
{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]),
|
||||
%% Always return new config.
|
||||
?assertMatch(
|
||||
#{
|
||||
<<"push_gateway_server">> := _,
|
||||
<<"interval">> := _,
|
||||
<<"enable">> := _,
|
||||
<<"vm_statistics_collector">> := _,
|
||||
<<"vm_system_info_collector">> := _,
|
||||
<<"vm_memory_collector">> := _,
|
||||
<<"vm_msacc_collector">> := _,
|
||||
<<"headers">> := _
|
||||
<<"collectors">> :=
|
||||
#{
|
||||
<<"mnesia">> := <<"disabled">>,
|
||||
<<"vm_dist">> := <<"disabled">>,
|
||||
<<"vm_memory">> := <<"disabled">>,
|
||||
<<"vm_msacc">> := <<"disabled">>,
|
||||
<<"vm_statistics">> := <<"disabled">>,
|
||||
<<"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
|
||||
),
|
||||
#{<<"enable">> := Enable} = Conf,
|
||||
#{<<"push_gateway">> := #{<<"enable">> := Enable}} = Conf,
|
||||
?assertEqual(Enable, undefined =/= erlang:whereis(emqx_prometheus)),
|
||||
|
||||
NewConf = Conf#{
|
||||
NewConf = OldConf#{
|
||||
<<"interval">> => <<"2s">>,
|
||||
<<"vm_statistics_collector">> => <<"enabled">>,
|
||||
<<"headers">> => #{
|
||||
|
@ -113,7 +126,7 @@ t_legacy_prometheus_api(_) ->
|
|||
{ok, Response2} = emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, NewConf),
|
||||
|
||||
Conf2 = emqx_utils_json:decode(Response2, [return_maps]),
|
||||
?assertMatch(NewConf, Conf2),
|
||||
?assertEqual(NewConf, Conf2),
|
||||
|
||||
EnvCollectors = application:get_env(prometheus, collectors, []),
|
||||
PromCollectors = prometheus_registry:collectors(default),
|
||||
|
@ -153,11 +166,11 @@ t_legacy_prometheus_api(_) ->
|
|||
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),
|
||||
?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(
|
||||
{error, {"HTTP/1.1", 400, _}},
|
||||
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, ConfWithoutScheme)
|
||||
|
@ -267,8 +280,7 @@ t_stats_no_auth_api(_) ->
|
|||
%% undefined is legacy prometheus
|
||||
case emqx:get_config([prometheus, enable_basic_auth], undefined) of
|
||||
true ->
|
||||
{ok, _} = emqx:update_config([prometheus, enable_basic_auth], false),
|
||||
emqx_dashboard_listener:regenerate_minirest_dispatch();
|
||||
{ok, _} = emqx:update_config([prometheus, enable_basic_auth], false);
|
||||
_ ->
|
||||
ok
|
||||
end,
|
||||
|
@ -278,6 +290,7 @@ t_stats_no_auth_api(_) ->
|
|||
|
||||
t_stats_auth_api(_) ->
|
||||
{ok, _} = emqx:update_config([prometheus, enable_basic_auth], true),
|
||||
emqx_dashboard_listener:regenerate_minirest_dispatch(),
|
||||
Auth = emqx_mgmt_api_test_util:auth_header_(),
|
||||
JsonAuth = [{"accept", "application/json"}, Auth],
|
||||
request_stats(JsonAuth, Auth),
|
||||
|
|
Loading…
Reference in New Issue