fix(telemetry): avoid hanging emqx_modules_app init with request

This commit is contained in:
Thales Macedo Garitezi 2022-04-25 11:21:38 -03:00
parent ada3b03f6e
commit 727bc712e6
No known key found for this signature in database
GPG Key ID: DD279F8152A9B6DD
2 changed files with 19 additions and 4 deletions

View File

@ -145,9 +145,13 @@ init(_Opts) ->
{NodeUUID, ClusterUUID} = ensure_uuids(), {NodeUUID, ClusterUUID} = ensure_uuids(),
{ok, State0#state{node_uuid = NodeUUID, cluster_uuid = ClusterUUID}}. {ok, State0#state{node_uuid = NodeUUID, cluster_uuid = ClusterUUID}}.
handle_call(enable, _From, State0) -> handle_call(enable, _From, State) ->
State = report_telemetry(State0), %% Wait a few moments before reporting the first telemetry, as the
{reply, ok, ensure_report_timer(State)}; %% 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}) -> handle_call(disable, _From, State = #state{timer = Timer}) ->
emqx_misc:cancel_timer(Timer), emqx_misc:cancel_timer(Timer),
{reply, ok, State#state{timer = undefined}}; {reply, ok, State#state{timer = undefined}};
@ -194,6 +198,9 @@ official_version(Version) ->
match =:= re:run(Version, Pt, [{capture, none}]). match =:= re:run(Version, Pt, [{capture, none}]).
ensure_report_timer(State = #state{report_interval = ReportInterval}) -> 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)}. State#state{timer = emqx_misc:start_timer(ReportInterval, time_to_report_telemetry_data)}.
os_info() -> os_info() ->

View File

@ -367,7 +367,15 @@ t_send_after_enable(_) ->
ok = snabbkaffe:start_trace(), ok = snabbkaffe:start_trace(),
try try
ok = emqx_telemetry:enable(), 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 receive
{request, post, _URL, _Headers, Body} -> {request, post, _URL, _Headers, Body} ->
{ok, Decoded} = emqx_json:safe_decode(Body, [return_maps]), {ok, Decoded} = emqx_json:safe_decode(Body, [return_maps]),