Skip to content

Commit 079cb2a

Browse files
committed
Avoid lists when generating channel stats
[#152240490]
1 parent accd6aa commit 079cb2a

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

src/rabbit_channel.erl

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,21 @@
174174

175175
-define(INFO_KEYS, ?CREATION_EVENT_KEYS ++ ?STATISTICS_KEYS -- [pid]).
176176

177-
-define(INCR_STATS(Incs, Measure, State),
177+
-define(INCR_STATS(Type, Key, Inc, Measure, State),
178178
case rabbit_event:stats_level(State, #ch.stats_timer) of
179-
fine -> incr_stats(Incs, Measure);
180-
_ -> ok
179+
fine ->
180+
rabbit_core_metrics:channel_stats(Type, Measure, {self(), Key}, Inc),
181+
%% Keys in the process dictionary are used to clean up the core metrics
182+
put({Type, Key}, none);
183+
_ ->
184+
ok
185+
end).
186+
187+
-define(INCR_STATS(Type, Key, Inc, Measure),
188+
begin
189+
rabbit_core_metrics:channel_stats(Type, Measure, {self(), Key}, Inc),
190+
%% Keys in the process dictionary are used to clean up the core metrics
191+
put({Type, Key}, none)
181192
end).
182193

183194
%%----------------------------------------------------------------------------
@@ -1658,7 +1669,7 @@ basic_return(#basic_message{exchange_name = ExchangeName,
16581669
content = Content},
16591670
State = #ch{protocol = Protocol, writer_pid = WriterPid},
16601671
Reason) ->
1661-
?INCR_STATS([{exchange_stats, ExchangeName, 1}], return_unroutable, State),
1672+
?INCR_STATS(exchange_stats, ExchangeName, 1, return_unroutable, State),
16621673
{_Close, ReplyCode, ReplyText} = Protocol:lookup_amqp_exception(Reason),
16631674
ok = rabbit_writer:send_command(
16641675
WriterPid,
@@ -1695,14 +1706,14 @@ record_sent(ConsumerTag, AckRequired,
16951706
user = #user{username = Username},
16961707
conn_name = ConnName,
16971708
channel = ChannelNum}) ->
1698-
?INCR_STATS([{queue_stats, QName, 1}], case {ConsumerTag, AckRequired} of
1699-
{none, true} -> get;
1700-
{none, false} -> get_no_ack;
1701-
{_ , true} -> deliver;
1702-
{_ , false} -> deliver_no_ack
1703-
end, State),
1709+
?INCR_STATS(queue_stats, QName, 1, case {ConsumerTag, AckRequired} of
1710+
{none, true} -> get;
1711+
{none, false} -> get_no_ack;
1712+
{_ , true} -> deliver;
1713+
{_ , false} -> deliver_no_ack
1714+
end, State),
17041715
case Redelivered of
1705-
true -> ?INCR_STATS([{queue_stats, QName, 1}], redeliver, State);
1716+
true -> ?INCR_STATS(queue_stats, QName, 1, redeliver, State);
17061717
false -> ok
17071718
end,
17081719
rabbit_trace:tap_out(Msg, ConnName, ChannelNum, Username, TraceState),
@@ -1747,11 +1758,11 @@ ack(Acked, State = #ch{queue_names = QNames}) ->
17471758
foreach_per_queue(
17481759
fun (QPid, MsgIds) ->
17491760
ok = rabbit_amqqueue:ack(QPid, MsgIds, self()),
1750-
?INCR_STATS(case dict:find(QPid, QNames) of
1751-
{ok, QName} -> Count = length(MsgIds),
1752-
[{queue_stats, QName, Count}];
1753-
error -> []
1754-
end, ack, State)
1761+
case dict:find(QPid, QNames) of
1762+
{ok, QName} -> Count = length(MsgIds),
1763+
?INCR_STATS(queue_stats, QName, Count, ack, State);
1764+
error -> ok
1765+
end
17551766
end, Acked),
17561767
ok = notify_limiter(State#ch.limiter, Acked).
17571768

@@ -1816,7 +1827,7 @@ deliver_to_queues({#delivery{message = #basic_message{exchange_name = XName},
18161827
confirm = false,
18171828
mandatory = false},
18181829
[]}, State) -> %% optimisation
1819-
?INCR_STATS([{exchange_stats, XName, 1}], publish, State),
1830+
?INCR_STATS(exchange_stats, XName, 1, publish, State),
18201831
State;
18211832
deliver_to_queues({Delivery = #delivery{message = Message = #basic_message{
18221833
exchange_name = XName},
@@ -1853,11 +1864,15 @@ deliver_to_queues({Delivery = #delivery{message = Message = #basic_message{
18531864
Message, State1),
18541865
State3 = process_routing_confirm( Confirm, DeliveredQPids, MsgSeqNo,
18551866
XName, State2),
1856-
?INCR_STATS([{exchange_stats, XName, 1} |
1857-
[{queue_exchange_stats, {QName, XName}, 1} ||
1858-
QPid <- DeliveredQPids,
1859-
{ok, QName} <- [dict:find(QPid, QNames1)]]],
1860-
publish, State3),
1867+
case rabbit_event:stats_level(State3, #ch.stats_timer) of
1868+
fine ->
1869+
?INCR_STATS(exchange_stats, XName, 1, publish),
1870+
[?INCR_STATS(queue_exchange_stats, {QName, XName}, 1, publish) ||
1871+
QPid <- DeliveredQPids,
1872+
{ok, QName} <- [dict:find(QPid, QNames1)]];
1873+
_ ->
1874+
ok
1875+
end,
18611876
State3.
18621877

18631878
process_routing_mandatory(false, _, _MsgSeqNo, _Msg, State) ->
@@ -1900,7 +1915,7 @@ send_confirms(State = #ch{tx = none, confirmed = C}) ->
19001915
ok -> MsgSeqNos =
19011916
lists:foldl(
19021917
fun ({MsgSeqNo, XName}, MSNs) ->
1903-
?INCR_STATS([{exchange_stats, XName, 1}],
1918+
?INCR_STATS(exchange_stats, XName, 1,
19041919
confirm, State),
19051920
[MsgSeqNo | MSNs]
19061921
end, [], lists:append(C)),
@@ -1997,13 +2012,6 @@ i(Item, _) ->
19972012
name(#ch{conn_name = ConnName, channel = Channel}) ->
19982013
list_to_binary(rabbit_misc:format("~s (~p)", [ConnName, Channel])).
19992014

2000-
incr_stats(Incs, Measure) ->
2001-
[begin
2002-
rabbit_core_metrics:channel_stats(Type, Measure, {self(), Key}, Inc),
2003-
%% Keys in the process dictionary are used to clean up the core metrics
2004-
put({Type, Key}, none)
2005-
end || {Type, Key, Inc} <- Incs].
2006-
20072015
emit_stats(State) -> emit_stats(State, []).
20082016

20092017
emit_stats(State, Extra) ->

0 commit comments

Comments
 (0)