Make batch committing of metrics more elegant

This commit is contained in:
周子博 2018-11-30 17:08:25 +08:00 committed by Feng Lee
parent f315994eb9
commit ad8b547519
1 changed files with 9 additions and 13 deletions

View File

@ -171,25 +171,21 @@ trans(dec, gauge, Metric, Val) ->
hold(gauge, Metric, -Val). hold(gauge, Metric, -Val).
hold(Type, Metric, Val) when Type =:= counter orelse Type =:= gauge -> hold(Type, Metric, Val) when Type =:= counter orelse Type =:= gauge ->
NewMetrics = case get(metrics) of put('$metrics', case get('$metrics') of
undefined -> undefined ->
#{Metric => {Type, Val}}; #{{Type, Metric} => Val};
Metrics -> Metrics ->
{Type, Count} = maps:get(Metric, Metrics, {Type, 0}), maps:update_with({Type, Metric}, fun(Cnt) -> Cnt + Val end, Val, Metrics)
Metrics#{Metric => {Type, Count + Val}} end).
end,
put(metrics, NewMetrics).
commit() -> commit() ->
case get(metrics) of case get('$metrics') of
undefined -> undefined -> ok;
ok;
Metrics -> Metrics ->
maps:fold(fun(Metric, {Type, Val}, Acc) -> maps:fold(fun({Type, Metric}, Val, _Acc) ->
update_counter(key(Type, Metric), {2, Val}), update_counter(key(Type, Metric), {2, Val})
Acc
end, 0, Metrics), end, 0, Metrics),
put(metrics, #{}) erase('$metrics')
end. end.
%% @doc Metric key %% @doc Metric key