Merge pull request #7857 from DDDHuang/monitor_10s

fix: monitor calculate rate time delta is 0
This commit is contained in:
zhongwencool 2022-05-06 10:27:06 +08:00 committed by GitHub
commit 2d58121ed0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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);
cal_rate(
#emqx_monit{data = NowData, time = NowTime},
#emqx_monit{data = LastData, time = LastTime}
#emqx_monit{data = LastData, time = LastTime} = Last
) ->
TimeDelta = NowTime - LastTime,
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.
case NowTime - LastTime of
0 ->
%% make sure: not divide by zero
timer:sleep(5),
NewSamplers = sample(erlang:system_time(millisecond)),
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}) ->
NewValue = maps:get(Key, Now),