Skip to content

Commit 0e4f126

Browse files
committed
rabbit_alarm: Add a helper to format resource alarm sources
1 parent 19be906 commit 0e4f126

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

deps/rabbit/src/rabbit_alarm.erl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
-export([start_link/0, start/0, stop/0, register/2, set_alarm/1,
2626
clear_alarm/1, get_alarms/0, get_alarms/1, get_local_alarms/0, get_local_alarms/1, on_node_up/1, on_node_down/1,
27+
format_resource_alarm_source/1,
2728
format_as_map/1, format_as_maps/1, is_local/1]).
2829

2930
-export([init/1, handle_call/2, handle_event/2, handle_info/2,
@@ -124,25 +125,23 @@ is_local({file_descriptor_limit, _}) -> true;
124125
is_local({{resource_limit, _Resource, Node}, _}) when Node =:= node() -> true;
125126
is_local({{resource_limit, _Resource, Node}, _}) when Node =/= node() -> false.
126127

128+
-spec format_resource_alarm_source(resource_alarm_source()) -> iodata().
129+
format_resource_alarm_source(disk) ->
130+
?DISK_SPACE_RESOURCE;
131+
format_resource_alarm_source(memory) ->
132+
?MEMORY_RESOURCE;
133+
format_resource_alarm_source(Unknown) ->
134+
io_lib:format("~w", [Unknown]).
135+
127136
-spec format_as_map(alarm()) -> #{binary() => term()}.
128137
format_as_map(file_descriptor_limit) ->
129138
#{
130139
<<"resource">> => ?FILE_DESCRIPTOR_RESOURCE,
131140
<<"node">> => node()
132141
};
133-
format_as_map({resource_limit, disk, Node}) ->
134-
#{
135-
<<"resource">> => ?DISK_SPACE_RESOURCE,
136-
<<"node">> => Node
137-
};
138-
format_as_map({resource_limit, memory, Node}) ->
139-
#{
140-
<<"resource">> => ?MEMORY_RESOURCE,
141-
<<"node">> => Node
142-
};
143142
format_as_map({resource_limit, Limit, Node}) ->
144143
#{
145-
<<"resource">> => rabbit_data_coercion:to_binary(Limit),
144+
<<"resource">> => iolist_to_binary(format_resource_alarm_source(Limit)),
146145
<<"node">> => Node
147146
}.
148147

@@ -237,7 +236,7 @@ handle_event({node_down, Node}, #alarms{alarmed_nodes = AN} = State) ->
237236
AlarmsForDeadNode = maps:get(Node, AN, []),
238237
{ok, lists:foldr(fun(Source, AccState) ->
239238
?LOG_WARNING("~ts resource limit alarm cleared for dead node ~tp",
240-
[Source, Node]),
239+
[format_resource_alarm_source(Source), Node]),
241240
maybe_alert(fun map_unappend/3, Node, Source, false, AccState)
242241
end, State, AlarmsForDeadNode)};
243242

@@ -283,7 +282,8 @@ maybe_alert(UpdateFun, Node, Source, WasAlertAdded,
283282
end, AN1),
284283
case StillHasAlerts of
285284
true -> ok;
286-
false -> ?LOG_WARNING("~ts resource limit alarm cleared across the cluster", [Source])
285+
false -> ?LOG_WARNING("~ts resource limit alarm cleared across the cluster",
286+
[format_resource_alarm_source(Source)])
287287
end,
288288
Alert = {WasAlertAdded, StillHasAlerts, Node},
289289
case node() of
@@ -326,7 +326,7 @@ handle_set_resource_alarm(Source, Node, State) ->
326326
"**********************************************************~n"
327327
"*** Publishers will be blocked until this alarm clears ***~n"
328328
"**********************************************************~n",
329-
[Source, Node]),
329+
[format_resource_alarm_source(Source), Node]),
330330
{ok, maybe_alert(fun map_append/3, Node, Source, true, State)}.
331331

332332
handle_set_alarm({file_descriptor_limit, []}, State) ->
@@ -342,7 +342,7 @@ handle_set_alarm(Alarm, State) ->
342342

343343
handle_clear_resource_alarm(Source, Node, State) ->
344344
?LOG_WARNING("~ts resource limit alarm cleared on node ~tp",
345-
[Source, Node]),
345+
[format_resource_alarm_source(Source), Node]),
346346
{ok, maybe_alert(fun map_unappend/3, Node, Source, false, State)}.
347347

348348
handle_clear_alarm(file_descriptor_limit, State) ->

deps/rabbit/src/rabbit_reader.erl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,14 +1727,11 @@ send_error_on_channel0_and_close(Channel, Protocol, Reason, State) ->
17271727
blocked_by_message(#throttle{blocked_by = Reasons}) ->
17281728
%% we don't want to report internal flow as a reason here since
17291729
%% it is entirely transient
1730-
Reasons1 = sets:del_element(flow, Reasons),
1731-
RStr = string:join([format_blocked_by(R) || R <- sets:to_list(Reasons1)], " & "),
1730+
RStr = lists:join([rabbit_alarm:format_resource_alarm_source(R) ||
1731+
{resource, R} <- sets:to_list(Reasons)],
1732+
" & "),
17321733
list_to_binary(rabbit_misc:format("low on ~ts", [RStr])).
17331734

1734-
format_blocked_by({resource, memory}) -> "memory";
1735-
format_blocked_by({resource, disk}) -> "disk";
1736-
format_blocked_by({resource, disc}) -> "disk".
1737-
17381735
update_last_blocked_at(Throttle) ->
17391736
Throttle#throttle{last_blocked_at = erlang:monotonic_time()}.
17401737

0 commit comments

Comments
 (0)