Skip to content

Allow multiple protocols in protocol listener health check (backport #13871) #13874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions deps/rabbitmq_management/priv/www/api/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1202,10 +1202,12 @@ <h2>Reference</h2>
<td></td>
<td></td>
<td></td>
<td class="path">/api/health/checks/protocol-listener/<i>protocol</i></td>
<td class="path">/api/health/checks/protocol-listener/<i>protocols</i></td>
<td>
Responds a 200 OK if there is an active listener for the given protocol,
otherwise responds with a 503 Service Unavailable. Valid protocol names are: amqp091, amqp10, mqtt, stomp, web-mqtt, web-stomp.
Responds a 200 OK if all given protocols have active listeners,
otherwise responds with a 503 Service Unavailable. Multiple protocols
may be provided by separating the names with commas. Valid protocol
names are: amqp091, amqp10, mqtt, stomp, web-mqtt, web-stomp.
</td>
</tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion deps/rabbitmq_management/src/rabbit_mgmt_dispatcher.erl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ dispatcher() ->
{"/health/checks/metadata-store/initialized/with-data", rabbit_mgmt_wm_health_check_metadata_store_initialized_with_data, []},
{"/health/checks/certificate-expiration/:within/:unit", rabbit_mgmt_wm_health_check_certificate_expiration, []},
{"/health/checks/port-listener/:port", rabbit_mgmt_wm_health_check_port_listener, []},
{"/health/checks/protocol-listener/:protocol", rabbit_mgmt_wm_health_check_protocol_listener, []},
{"/health/checks/protocol-listener/:protocols", rabbit_mgmt_wm_health_check_protocol_listener, []},
{"/health/checks/virtual-hosts", rabbit_mgmt_wm_health_check_virtual_hosts, []},
{"/health/checks/quorum-queues-without-elected-leaders/all-vhosts/", rabbit_mgmt_wm_health_check_quorum_queues_without_elected_leaders_across_all_vhosts, []},
{"/health/checks/quorum-queues-without-elected-leaders/vhost/:vhost/", rabbit_mgmt_wm_health_check_quorum_queues_without_elected_leaders, []},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,46 @@ content_types_provided(ReqData, Context) ->
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.

resource_exists(ReqData, Context) ->
{case protocol(ReqData) of
{case protocols(ReqData) of
none -> false;
_ -> true
end, ReqData, Context}.

to_json(ReqData, Context) ->
Protocol = normalize_protocol(protocol(ReqData)),
Listeners = rabbit_networking:active_listeners(),
Local = [L || #listener{node = N} = L <- Listeners, N == node()],
ProtoListeners = [L || #listener{protocol = P} = L <- Local, atom_to_list(P) == Protocol],
case ProtoListeners of
Protocols = string:split(protocols(ReqData), ",", all),
RequestedProtocols = sets:from_list(
[normalize_protocol(P) || P <- Protocols],
[{version, 2}]),
Listeners = rabbit_networking:node_listeners(node()),
ActiveProtocols = sets:from_list(
[atom_to_list(P) || #listener{protocol = P} <- Listeners],
[{version, 2}]),
MissingProtocols = sets:to_list(sets:subtract(RequestedProtocols, ActiveProtocols)),
case MissingProtocols of
[] ->
Msg = <<"No active listener">>,
failure(Msg, Protocol, [P || #listener{protocol = P} <- Local], ReqData, Context);
Body = #{status => ok,
protocols => [list_to_binary(P) || P <- sets:to_list(ActiveProtocols)]},
rabbit_mgmt_util:reply(Body, ReqData, Context);
_ ->
Body = #{status => ok,
protocol => list_to_binary(Protocol)},
rabbit_mgmt_util:reply(Body, ReqData, Context)
Msg = <<"No active listener">>,
failure(Msg, MissingProtocols, sets:to_list(ActiveProtocols), ReqData, Context)
end.

failure(Message, Missing, Protocols, ReqData, Context) ->
Body = #{
status => failed,
reason => Message,
missing => list_to_binary(Missing),
protocols => Protocols
missing => [list_to_binary(P) || P <- Missing],
protocols => [list_to_binary(P) || P <- Protocols]
},
{Response, ReqData1, Context1} = rabbit_mgmt_util:reply(Body, ReqData, Context),
{stop, cowboy_req:reply(503, #{}, Response, ReqData1), Context1}.

is_authorized(ReqData, Context) ->
rabbit_mgmt_util:is_authorized(ReqData, Context).

protocol(ReqData) ->
rabbit_mgmt_util:id(protocol, ReqData).
protocols(ReqData) ->
rabbit_mgmt_util:id(protocols, ReqData).

normalize_protocol(Protocol) ->
case string:lowercase(binary_to_list(Protocol)) of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ protocol_listener_test(Config) ->
Body0 = http_get_failed(Config, "/health/checks/protocol-listener/mqtt"),
?assertEqual(<<"failed">>, maps:get(<<"status">>, Body0)),
?assertEqual(true, maps:is_key(<<"reason">>, Body0)),
?assertEqual(<<"mqtt">>, maps:get(<<"missing">>, Body0)),
?assertEqual([<<"mqtt">>], maps:get(<<"missing">>, Body0)),
?assert(lists:member(<<"http">>, maps:get(<<"protocols">>, Body0))),
?assert(lists:member(<<"clustering">>, maps:get(<<"protocols">>, Body0))),
?assert(lists:member(<<"amqp">>, maps:get(<<"protocols">>, Body0))),
Expand All @@ -394,6 +394,14 @@ protocol_listener_test(Config) ->
http_get_failed(Config, "/health/checks/protocol-listener/stomp"),
http_get_failed(Config, "/health/checks/protocol-listener/stomp1.0"),

%% Multiple protocols may be supplied. The health check only returns OK if
%% all requested protocols are available.
Body1 = http_get_failed(Config, "/health/checks/protocol-listener/amqp,mqtt"),
?assertEqual(<<"failed">>, maps:get(<<"status">>, Body1)),
?assertEqual(true, maps:is_key(<<"reason">>, Body1)),
?assert(lists:member(<<"mqtt">>, maps:get(<<"missing">>, Body1))),
?assert(lists:member(<<"amqp">>, maps:get(<<"protocols">>, Body1))),

passed.

port_listener_test(Config) ->
Expand Down