From 3716ee60fba0aa6015fb60e25b842d75869a7855 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Mon, 25 Apr 2022 11:22:59 -0300 Subject: [PATCH 1/3] style: format other applications --- apps/emqx_authn/test/emqx_authn_api_SUITE.erl | 52 ++++++++++++++----- .../test/emqx_authz_api_sources_SUITE.erl | 52 ++++++++++++++----- 2 files changed, 76 insertions(+), 28 deletions(-) diff --git a/apps/emqx_authn/test/emqx_authn_api_SUITE.erl b/apps/emqx_authn/test/emqx_authn_api_SUITE.erl index d7342185a..21b53bb1f 100644 --- a/apps/emqx_authn/test/emqx_authn_api_SUITE.erl +++ b/apps/emqx_authn/test/emqx_authn_api_SUITE.erl @@ -120,21 +120,45 @@ t_listener_authenticator_import_users(_) -> test_authenticator_import_users(["listeners", ?TCP_DEFAULT]). t_aggregate_metrics(_) -> - Metrics = #{ 'emqx@node1.emqx.io' => #{metrics => - #{failed => 0,matched => 1,rate => 0.0, - rate_last5m => 0.0,rate_max => 0.1, - success => 1} - }, - 'emqx@node2.emqx.io' => #{metrics => - #{failed => 0,matched => 1,rate => 0.0, - rate_last5m => 0.0,rate_max => 0.1, - success => 1} - } - }, + Metrics = #{ + 'emqx@node1.emqx.io' => #{ + metrics => + #{ + failed => 0, + matched => 1, + rate => 0.0, + rate_last5m => 0.0, + rate_max => 0.1, + success => 1 + } + }, + 'emqx@node2.emqx.io' => #{ + metrics => + #{ + failed => 0, + matched => 1, + rate => 0.0, + rate_last5m => 0.0, + rate_max => 0.1, + success => 1 + } + } + }, Res = emqx_authn_api:aggregate_metrics(maps:values(Metrics)), - ?assertEqual(#{metrics => - #{failed => 0,matched => 2,rate => 0.0,rate_last5m => 0.0, - rate_max => 0.2,success => 2}}, Res). + ?assertEqual( + #{ + metrics => + #{ + failed => 0, + matched => 2, + rate => 0.0, + rate_last5m => 0.0, + rate_max => 0.2, + success => 2 + } + }, + Res + ). test_authenticators(PathPrefix) -> ValidConfig = emqx_authn_test_lib:http_example(), diff --git a/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl b/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl index a41f98d27..308e67d53 100644 --- a/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl @@ -449,21 +449,45 @@ t_move_source(_) -> ok. t_aggregate_metrics(_) -> - Metrics = #{ 'emqx@node1.emqx.io' => #{metrics => - #{failed => 0,matched => 1,rate => 0.0, - rate_last5m => 0.0,rate_max => 0.1, - success => 1} - }, - 'emqx@node2.emqx.io' => #{metrics => - #{failed => 0,matched => 1,rate => 0.0, - rate_last5m => 0.0,rate_max => 0.1, - success => 1} - } - }, + Metrics = #{ + 'emqx@node1.emqx.io' => #{ + metrics => + #{ + failed => 0, + matched => 1, + rate => 0.0, + rate_last5m => 0.0, + rate_max => 0.1, + success => 1 + } + }, + 'emqx@node2.emqx.io' => #{ + metrics => + #{ + failed => 0, + matched => 1, + rate => 0.0, + rate_last5m => 0.0, + rate_max => 0.1, + success => 1 + } + } + }, Res = emqx_authn_api:aggregate_metrics(maps:values(Metrics)), - ?assertEqual(#{metrics => - #{failed => 0,matched => 2,rate => 0.0,rate_last5m => 0.0, - rate_max => 0.2,success => 2}}, Res). + ?assertEqual( + #{ + metrics => + #{ + failed => 0, + matched => 2, + rate => 0.0, + rate_last5m => 0.0, + rate_max => 0.2, + success => 2 + } + }, + Res + ). get_sources(Result) -> maps:get(<<"sources">>, jsx:decode(Result), []). From ada3b03f6ea2d75ea3b1d0d0781f51cb855473a0 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Mon, 25 Apr 2022 11:15:59 -0300 Subject: [PATCH 2/3] fix(telemetry): do not crash if bridge app is not ready yet --- apps/emqx_bridge/src/emqx_bridge.erl | 41 ++++++++++++++++------------ 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/apps/emqx_bridge/src/emqx_bridge.erl b/apps/emqx_bridge/src/emqx_bridge.erl index 4727a1dc8..b2939f9a9 100644 --- a/apps/emqx_bridge/src/emqx_bridge.erl +++ b/apps/emqx_bridge/src/emqx_bridge.erl @@ -431,23 +431,30 @@ if_only_to_toggle_enable(OldConf, Conf) -> } } when BridgeType :: atom(). get_basic_usage_info() -> - lists:foldl( - fun(#{resource_data := #{config := #{enable := false}}}, Acc) -> - Acc; - (#{type := BridgeType}, Acc) -> - NumBridges = maps:get(num_bridges, Acc), - CountByType0 = maps:get(count_by_type, Acc), - CountByType = maps:update_with( - binary_to_atom(BridgeType, utf8), - fun(X) -> X + 1 end, - 1, - CountByType0), - Acc#{ num_bridges => NumBridges + 1 - , count_by_type => CountByType - } - end, - #{num_bridges => 0, count_by_type => #{}}, - list()). + InitialAcc = #{num_bridges => 0, count_by_type => #{}}, + try + lists:foldl( + fun(#{resource_data := #{config := #{enable := false}}}, Acc) -> + Acc; + (#{type := BridgeType}, Acc) -> + NumBridges = maps:get(num_bridges, Acc), + CountByType0 = maps:get(count_by_type, Acc), + CountByType = maps:update_with( + binary_to_atom(BridgeType, utf8), + fun(X) -> X + 1 end, + 1, + CountByType0), + Acc#{ num_bridges => NumBridges + 1 + , count_by_type => CountByType + } + end, + InitialAcc, + list()) + catch + %% for instance, when the bridge app is not ready yet. + _:_ -> + InitialAcc + end. bin(Bin) when is_binary(Bin) -> Bin; bin(Str) when is_list(Str) -> list_to_binary(Str); From 727bc712e6eabc69f6e9960986cc8674615fef68 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Mon, 25 Apr 2022 11:21:38 -0300 Subject: [PATCH 3/3] fix(telemetry): avoid hanging emqx_modules_app init with request --- apps/emqx_modules/src/emqx_telemetry.erl | 13 ++++++++++--- apps/emqx_modules/test/emqx_telemetry_SUITE.erl | 10 +++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/apps/emqx_modules/src/emqx_telemetry.erl b/apps/emqx_modules/src/emqx_telemetry.erl index 8a86819ee..1791e39ad 100644 --- a/apps/emqx_modules/src/emqx_telemetry.erl +++ b/apps/emqx_modules/src/emqx_telemetry.erl @@ -145,9 +145,13 @@ init(_Opts) -> {NodeUUID, ClusterUUID} = ensure_uuids(), {ok, State0#state{node_uuid = NodeUUID, cluster_uuid = ClusterUUID}}. -handle_call(enable, _From, State0) -> - State = report_telemetry(State0), - {reply, ok, ensure_report_timer(State)}; +handle_call(enable, _From, State) -> + %% Wait a few moments before reporting the first telemetry, as the + %% apps might still be starting up. Also, this avoids hanging + %% `emqx_modules_app' initialization in case the POST request + %% takes a lot of time. + FirstReportTimeoutMS = timer:seconds(10), + {reply, ok, ensure_report_timer(FirstReportTimeoutMS, State)}; handle_call(disable, _From, State = #state{timer = Timer}) -> emqx_misc:cancel_timer(Timer), {reply, ok, State#state{timer = undefined}}; @@ -194,6 +198,9 @@ official_version(Version) -> match =:= re:run(Version, Pt, [{capture, none}]). ensure_report_timer(State = #state{report_interval = ReportInterval}) -> + ensure_report_timer(ReportInterval, State). + +ensure_report_timer(ReportInterval, State) -> State#state{timer = emqx_misc:start_timer(ReportInterval, time_to_report_telemetry_data)}. os_info() -> diff --git a/apps/emqx_modules/test/emqx_telemetry_SUITE.erl b/apps/emqx_modules/test/emqx_telemetry_SUITE.erl index d7acc9218..75ced7cfd 100644 --- a/apps/emqx_modules/test/emqx_telemetry_SUITE.erl +++ b/apps/emqx_modules/test/emqx_telemetry_SUITE.erl @@ -367,7 +367,15 @@ t_send_after_enable(_) -> ok = snabbkaffe:start_trace(), try ok = emqx_telemetry:enable(), - ?assertMatch({ok, _}, ?block_until(#{?snk_kind := telemetry_data_reported}, 2000, 100)), + Timeout = 12_000, + ?assertMatch( + {ok, _}, + ?wait_async_action( + ok = emqx_telemetry:enable(), + #{?snk_kind := telemetry_data_reported}, + Timeout + ) + ), receive {request, post, _URL, _Headers, Body} -> {ok, Decoded} = emqx_json:safe_decode(Body, [return_maps]),