Skip to content

Commit a4afc2a

Browse files
Merge pull request #13872 from rabbitmq/md/health-check-is-in-service
Add an 'is-in-service' health check wrapping `rabbit:is_serving/0`
2 parents 98ba31a + 07fe630 commit a4afc2a

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

deps/rabbitmq_management/priv/www/api/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,19 @@ <h2>Reference</h2>
12391239
</p>
12401240
</td>
12411241
</tr>
1242+
<tr>
1243+
<td>X</td>
1244+
<td></td>
1245+
<td></td>
1246+
<td></td>
1247+
<td class="path">/api/health/checks/is-in-service</td>
1248+
<td>
1249+
Responds a 200 OK if the target node is booted, running, and ready to
1250+
serve clients, otherwise responds with a 503 Service Unavailable. If the
1251+
target node is being drained for maintenance then this check returns 503
1252+
Service Unavailable.
1253+
</td>
1254+
</tr>
12421255
<tr>
12431256
<td>X</td>
12441257
<td></td>

deps/rabbitmq_management/src/rabbit_mgmt_dispatcher.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ dispatcher() ->
207207
{"/health/checks/quorum-queues-without-elected-leaders/all-vhosts/pattern/:pattern", rabbit_mgmt_wm_health_check_quorum_queues_without_elected_leaders_across_all_vhosts, []},
208208
{"/health/checks/quorum-queues-without-elected-leaders/vhost/:vhost/pattern/:pattern", rabbit_mgmt_wm_health_check_quorum_queues_without_elected_leaders, []},
209209
{"/health/checks/node-is-quorum-critical", rabbit_mgmt_wm_health_check_node_is_quorum_critical, []},
210+
{"/health/checks/is-in-service", rabbit_mgmt_wm_health_check_is_in_service, []},
210211
{"/reset", rabbit_mgmt_wm_reset, []},
211212
{"/reset/:node", rabbit_mgmt_wm_reset, []},
212213
{"/rebalance/queues", rabbit_mgmt_wm_rebalance_queues, [{queues, all}]},
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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) 2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
%%
7+
8+
-module(rabbit_mgmt_wm_health_check_is_in_service).
9+
10+
-export([init/2]).
11+
-export([to_json/2, content_types_provided/2]).
12+
-export([variances/2]).
13+
14+
-include("rabbit_mgmt.hrl").
15+
-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
16+
17+
init(Req, _State) ->
18+
Req1 = rabbit_mgmt_headers:set_no_cache_headers(
19+
rabbit_mgmt_headers:set_common_permission_headers(
20+
Req, ?MODULE), ?MODULE),
21+
{cowboy_rest, Req1, #context{}}.
22+
23+
variances(Req, Context) ->
24+
{[<<"accept-encoding">>, <<"origin">>], Req, Context}.
25+
26+
content_types_provided(ReqData, Context) ->
27+
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.
28+
29+
to_json(ReqData, Context) ->
30+
case rabbit:is_serving() of
31+
true ->
32+
rabbit_mgmt_util:reply(#{status => ok}, ReqData, Context);
33+
false ->
34+
Msg = "this rabbit node is not currently available to serve",
35+
failure(Msg, ReqData, Context)
36+
end.
37+
38+
failure(Message, ReqData, Context) ->
39+
Body = #{
40+
status => failed,
41+
reason => rabbit_data_coercion:to_binary(Message)
42+
},
43+
{Response, ReqData1, Context1} = rabbit_mgmt_util:reply(Body, ReqData, Context),
44+
{stop, cowboy_req:reply(?HEALTH_CHECK_FAILURE_STATUS, #{}, Response, ReqData1), Context1}.

deps/rabbitmq_management/test/rabbit_mgmt_http_health_checks_SUITE.erl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ all_tests() -> [
5050
metadata_store_initialized_with_data_test,
5151
protocol_listener_test,
5252
port_listener_test,
53-
certificate_expiration_test
53+
certificate_expiration_test,
54+
is_in_service_test
5455
].
5556

5657
%% -------------------------------------------------------------------
@@ -457,6 +458,18 @@ certificate_expiration_test(Config) ->
457458

458459
passed.
459460

461+
is_in_service_test(Config) ->
462+
Path = "/health/checks/is-in-service",
463+
Check0 = http_get(Config, Path, ?OK),
464+
?assertEqual(<<"ok">>, maps:get(status, Check0)),
465+
466+
true = rabbit_ct_broker_helpers:mark_as_being_drained(Config, 0),
467+
Body0 = http_get_failed(Config, Path),
468+
?assertEqual(<<"failed">>, maps:get(<<"status">>, Body0)),
469+
true = rabbit_ct_broker_helpers:unmark_as_being_drained(Config, 0),
470+
471+
passed.
472+
460473
http_get_failed(Config, Path) ->
461474
{ok, {{_, Code, _}, _, ResBody}} = req(Config, get, Path, [auth_header("guest", "guest")]),
462475
?assertEqual(Code, ?HEALTH_CHECK_FAILURE_STATUS),

0 commit comments

Comments
 (0)