Make batch committing of metrics more elegant
This commit is contained in:
parent
f315994eb9
commit
ad8b547519
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue