fix: monitor calculate rate time delta is 0

This commit is contained in:
DDDHuang 2022-04-29 21:05:59 +08:00
parent 8ff552c9cf
commit 2504531b45
1 changed files with 18 additions and 7 deletions

View File

@ -276,14 +276,25 @@ cal_rate(_Now, undefined) ->
lists:foldl(fun(Key, Acc) -> Acc#{Key => 0} end, #{}, AllSamples); lists:foldl(fun(Key, Acc) -> Acc#{Key => 0} end, #{}, AllSamples);
cal_rate( cal_rate(
#emqx_monit{data = NowData, time = NowTime}, #emqx_monit{data = NowData, time = NowTime},
#emqx_monit{data = LastData, time = LastTime} #emqx_monit{data = LastData, time = LastTime} = Last
) -> ) ->
TimeDelta = NowTime - LastTime, case NowTime - LastTime of
Filter = fun(Key, _) -> lists:member(Key, ?GAUGE_SAMPLER_LIST) end, 0 ->
Gauge = maps:filter(Filter, NowData), %% make sure: not divide by zero
{_, _, _, Rate} = timer:sleep(5),
lists:foldl(fun cal_rate_/2, {NowData, LastData, TimeDelta, Gauge}, ?DELTA_SAMPLER_LIST), NewSamplers = sample(erlang:system_time(millisecond)),
Rate. cal_rate(NewSamplers, Last);
TimeDelta ->
Filter = fun(Key, _) -> lists:member(Key, ?GAUGE_SAMPLER_LIST) end,
Gauge = maps:filter(Filter, NowData),
{_, _, _, Rate} =
lists:foldl(
fun cal_rate_/2,
{NowData, LastData, TimeDelta, Gauge},
?DELTA_SAMPLER_LIST
),
Rate
end.
cal_rate_(Key, {Now, Last, TDelta, Res}) -> cal_rate_(Key, {Now, Last, TDelta, Res}) ->
NewValue = maps:get(Key, Now), NewValue = maps:get(Key, Now),