fix: dashboard_monitor crash

This commit is contained in:
Zhongwen Deng 2022-03-25 10:52:11 +08:00
parent 16f943459e
commit 55f805bac0
4 changed files with 29 additions and 4 deletions

View File

@ -328,14 +328,14 @@ all() ->
].
%% @doc Get metric value
-spec val(metric_name()) -> maybe(non_neg_integer()).
-spec val(metric_name()) -> non_neg_integer().
val(Name) ->
case ets:lookup(?TAB, Name) of
[#metric{idx = Idx}] ->
CRef = persistent_term:get(?MODULE),
counters:get(CRef, Idx);
[] ->
undefined
0
end.
%% @doc Increase counter

View File

@ -164,11 +164,11 @@ getstats() ->
end.
%% @doc Get stats by name.
-spec getstat(atom()) -> maybe(non_neg_integer()).
-spec getstat(atom()) -> non_neg_integer().
getstat(Name) ->
case ets:lookup(?TAB, Name) of
[{Name, Val}] -> Val;
[] -> undefined
[] -> 0
end.
%% @doc Set stats

View File

@ -242,6 +242,9 @@ format(Data) ->
format(TimeStamp, Data, All) ->
[Data#{time_stamp => TimeStamp} | All].
cal_rate(_Now, undefined) ->
AllSamples = ?GAUGE_SAMPLER_LIST ++ maps:keys(?DELTA_SAMPLER_RATE_MAP),
{ok, lists:foldl(fun(Key, Acc) -> Acc#{Key => 0} end, #{}, AllSamples)};
cal_rate( #emqx_monit{data = NowData, time = NowTime}
, #emqx_monit{data = LastData, time = LastTime}) ->
TimeDelta = NowTime - LastTime,

View File

@ -102,6 +102,15 @@ t_monitor_current_api(_) ->
|| Key <- maps:values(?DELTA_SAMPLER_RATE_MAP) ++ ?GAUGE_SAMPLER_LIST],
ok.
t_monitor_reset(_) ->
restart_monitor(),
{ok, Rate} = request(["monitor_current"]),
[?assert(maps:is_key(atom_to_binary(Key, utf8), Rate))
|| Key <- maps:values(?DELTA_SAMPLER_RATE_MAP) ++ ?GAUGE_SAMPLER_LIST],
{ok, Samplers} = request(["monitor"], "latest=1"),
?assertEqual(1, erlang:length(Samplers)),
ok.
t_monitor_api_error(_) ->
{error, {400, #{<<"code">> := <<"BAD_RPC">>}}} =
request(["monitor", "nodes", 'emqx@127.0.0.2']),
@ -147,3 +156,16 @@ do_request_api(Method, Request)->
auth_header_() ->
Basic = binary_to_list(base64:encode(<<"admin:public">>)),
{"Authorization", "Basic " ++ Basic}.
restart_monitor() ->
erlang:exit(erlang:whereis(emqx_dashboard_monitor), killed),
?assertEqual(ok, wait_new_monitor(10)).
wait_new_monitor(Count) when Count =< 0 -> timeout;
wait_new_monitor(Count) ->
case is_pid(erlang:whereis(emqx_dashboard_monitor)) of
true -> ok;
false ->
timer:sleep(100),
wait_new_monitor(Count - 1)
end.