Skip to content

Commit 601d4f2

Browse files
New HTTP API health check endpoints
for the check introduced in #13487. Note that encoding a regular expression pattern with percent encoding is a pain (e.g. '.*' = '.%2a'), so these endpoints fall back to a default pattern value that matches all queues.
1 parent 14d53f8 commit 601d4f2

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

deps/rabbitmq_management/src/rabbit_mgmt_dispatcher.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ dispatcher() ->
202202
{"/health/checks/port-listener/:port", rabbit_mgmt_wm_health_check_port_listener, []},
203203
{"/health/checks/protocol-listener/:protocol", rabbit_mgmt_wm_health_check_protocol_listener, []},
204204
{"/health/checks/virtual-hosts", rabbit_mgmt_wm_health_check_virtual_hosts, []},
205+
{"/health/checks/quorum-queues-without-elected-leaders/all-vhosts/", rabbit_mgmt_wm_health_check_quorum_queues_without_elected_leaders_across_all_vhosts, []},
206+
{"/health/checks/quorum-queues-without-elected-leaders/vhost/:vhost/", rabbit_mgmt_wm_health_check_quorum_queues_without_elected_leaders, []},
207+
{"/health/checks/quorum-queues-without-elected-leaders/all-vhosts/pattern/:pattern", rabbit_mgmt_wm_health_check_quorum_queues_without_elected_leaders_across_all_vhosts, []},
208+
{"/health/checks/quorum-queues-without-elected-leaders/vhost/:vhost/pattern/:pattern", rabbit_mgmt_wm_health_check_quorum_queues_without_elected_leaders, []},
205209
{"/health/checks/node-is-quorum-critical", rabbit_mgmt_wm_health_check_node_is_quorum_critical, []},
206210
{"/reset", rabbit_mgmt_wm_reset, []},
207211
{"/reset/:node", rabbit_mgmt_wm_reset, []},
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
%%
7+
8+
%% An HTTP API counterpart of 'rabbitmq-diagnostics check_for_quorum_queues_without_an_elected_leader'
9+
-module(rabbit_mgmt_wm_health_check_quorum_queues_without_elected_leaders).
10+
11+
-export([init/2, to_json/2, content_types_provided/2, is_authorized/2]).
12+
-export([resource_exists/2]).
13+
-export([variances/2]).
14+
15+
-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
16+
17+
-define(DEFAULT_PATTERN, <<".*">>).
18+
19+
%%--------------------------------------------------------------------
20+
21+
init(Req, _State) ->
22+
{cowboy_rest, rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE), #context{}}.
23+
24+
variances(Req, Context) ->
25+
{[<<"accept-encoding">>, <<"origin">>], Req, Context}.
26+
27+
content_types_provided(ReqData, Context) ->
28+
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.
29+
30+
resource_exists(ReqData, Context) ->
31+
Result = case {vhost(ReqData), pattern(ReqData)} of
32+
{none, _} -> false;
33+
{_, none} -> false;
34+
_ -> true
35+
end,
36+
{Result, ReqData, Context}.
37+
38+
to_json(ReqData, Context) ->
39+
case rabbit_quorum_queue:leader_health_check(pattern(ReqData), vhost(ReqData)) of
40+
[] ->
41+
rabbit_mgmt_util:reply(#{status => ok}, ReqData, Context);
42+
Qs when length(Qs) > 0 ->
43+
Msg = <<"Detected quorum queues without an elected leader">>,
44+
failure(Msg, Qs, ReqData, Context)
45+
end.
46+
47+
failure(Message, Qs, ReqData, Context) ->
48+
Body = #{status => failed,
49+
reason => Message,
50+
queues => Qs},
51+
{Response, ReqData1, Context1} = rabbit_mgmt_util:reply(Body, ReqData, Context),
52+
{stop, cowboy_req:reply(503, #{}, Response, ReqData1), Context1}.
53+
54+
is_authorized(ReqData, Context) ->
55+
rabbit_mgmt_util:is_authorized(ReqData, Context).
56+
57+
%%
58+
%% Implementation
59+
%%
60+
61+
vhost(ReqData) ->
62+
rabbit_mgmt_util:id(vhost, ReqData).
63+
64+
pattern(ReqData) ->
65+
case rabbit_mgmt_util:id(pattern, ReqData) of
66+
none -> ?DEFAULT_PATTERN;
67+
Other -> Other
68+
end.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
%%
7+
8+
%% An HTTP API counterpart of 'rabbitmq-diagnostics check_for_quorum_queues_without_an_elected_leader --across-all-vhosts'
9+
-module(rabbit_mgmt_wm_health_check_quorum_queues_without_elected_leaders_across_all_vhosts).
10+
11+
-export([init/2, to_json/2, content_types_provided/2, is_authorized/2]).
12+
-export([resource_exists/2]).
13+
-export([variances/2]).
14+
15+
-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
16+
17+
-define(ACROSS_ALL_VHOSTS, across_all_vhosts).
18+
-define(DEFAULT_PATTERN, <<".*">>).
19+
20+
%%--------------------------------------------------------------------
21+
22+
init(Req, _State) ->
23+
{cowboy_rest, rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE), #context{}}.
24+
25+
variances(Req, Context) ->
26+
{[<<"accept-encoding">>, <<"origin">>], Req, Context}.
27+
28+
content_types_provided(ReqData, Context) ->
29+
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.
30+
31+
resource_exists(ReqData, Context) ->
32+
{true, ReqData, Context}.
33+
34+
to_json(ReqData, Context) ->
35+
case rabbit_quorum_queue:leader_health_check(pattern(ReqData), ?ACROSS_ALL_VHOSTS) of
36+
[] ->
37+
rabbit_mgmt_util:reply(#{status => ok}, ReqData, Context);
38+
Qs when length(Qs) > 0 ->
39+
Msg = <<"Detected quorum queues without an elected leader">>,
40+
failure(Msg, Qs, ReqData, Context)
41+
end.
42+
43+
failure(Message, Qs, ReqData, Context) ->
44+
Body = #{status => failed,
45+
reason => Message,
46+
queues => Qs},
47+
{Response, ReqData1, Context1} = rabbit_mgmt_util:reply(Body, ReqData, Context),
48+
{stop, cowboy_req:reply(503, #{}, Response, ReqData1), Context1}.
49+
50+
is_authorized(ReqData, Context) ->
51+
rabbit_mgmt_util:is_authorized(ReqData, Context).
52+
53+
%%
54+
%% Implementation
55+
%%
56+
57+
pattern(ReqData) ->
58+
case rabbit_mgmt_util:id(pattern, ReqData) of
59+
none -> ?DEFAULT_PATTERN;
60+
Other -> Other
61+
end.

0 commit comments

Comments
 (0)