Skip to content

Commit 7142de4

Browse files
committed
Return policy fields and delivery_limit when stats are disabled
When `management_agent.disable_metrics_collector` and `management.disable_stats` are both set to `true`, the HTTP API returns minimal queue information that excludes policy-related fields and `delivery_limit`. This causes `policy`, `operator_policy`, `effective_policy_definition`, and `delivery_limit` to be `null` in API responses, even though these are configuration metadata rather than statistics. This change adds these four fields to the `format/2` function in both `rabbit_classic_queue` and `rabbit_quorum_queue` modules. The fields now appear in the type-specific formatting that runs regardless of metrics collection status. The three policy fields use existing `i/2` function implementations that call `rabbit_policy` module functions. For `delivery_limit`, this change adds an `i(delivery_limit, Q)` function to `rabbit_quorum_queue` that extracts the delivery limit from the queue's `x-delivery-limit` argument, returning `unlimited` when not set. Fixes #15182
1 parent a8901bc commit 7142de4

File tree

3 files changed

+123
-55
lines changed

3 files changed

+123
-55
lines changed

deps/rabbit/src/rabbit_classic_queue.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,10 @@ format(Q, _Ctx) when ?is_amqqueue(Q) ->
280280
end,
281281
[{type, rabbit_queue_type:short_alias_of(?MODULE)},
282282
{state, State},
283-
{node, node(amqqueue:get_pid(Q))}].
283+
{node, node(amqqueue:get_pid(Q))},
284+
{policy, i(policy, Q)},
285+
{operator_policy, i(operator_policy, Q)},
286+
{effective_policy_definition, i(effective_policy_definition, Q)}].
284287

285288
-spec init(amqqueue:amqqueue()) -> {ok, state()}.
286289
init(Q) when ?amqqueue_is_classic(Q) ->

deps/rabbit/src/rabbit_quorum_queue.erl

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -343,21 +343,7 @@ gather_policy_config(Q, IsQueueDeclaration) ->
343343
OverflowBin = args_policy_lookup(<<"overflow">>, fun policy_has_precedence/2, Q),
344344
Overflow = overflow(OverflowBin, drop_head, QName),
345345
MaxBytes = args_policy_lookup(<<"max-length-bytes">>, fun min/2, Q),
346-
DeliveryLimit = case args_policy_lookup(<<"delivery-limit">>,
347-
fun resolve_delivery_limit/2, Q) of
348-
undefined ->
349-
case IsQueueDeclaration of
350-
true ->
351-
?LOG_INFO(
352-
"~ts: delivery_limit not set, defaulting to ~b",
353-
[rabbit_misc:rs(QName), ?DEFAULT_DELIVERY_LIMIT]);
354-
false ->
355-
ok
356-
end,
357-
?DEFAULT_DELIVERY_LIMIT;
358-
DL ->
359-
DL
360-
end,
346+
DeliveryLimit = get_delivery_limit(Q, IsQueueDeclaration),
361347
Expires = args_policy_lookup(<<"expires">>, fun min/2, Q),
362348
MsgTTL = args_policy_lookup(<<"message-ttl">>, fun min/2, Q),
363349
DeadLetterHandler = dead_letter_handler(Q, Overflow),
@@ -382,12 +368,6 @@ ra_machine_config(Q) when ?is_amqqueue(Q) ->
382368
created => erlang:system_time(millisecond)
383369
}.
384370

385-
resolve_delivery_limit(PolVal, ArgVal)
386-
when PolVal < 0 orelse ArgVal < 0 ->
387-
max(PolVal, ArgVal);
388-
resolve_delivery_limit(PolVal, ArgVal) ->
389-
min(PolVal, ArgVal).
390-
391371
policy_has_precedence(Policy, _QueueArg) ->
392372
Policy.
393373

@@ -1753,6 +1733,29 @@ i_totals(Q) when ?is_amqqueue(Q) ->
17531733
{messages, 0}]
17541734
end.
17551735

1736+
resolve_delivery_limit(PolVal, ArgVal)
1737+
when PolVal < 0 orelse ArgVal < 0 ->
1738+
max(PolVal, ArgVal);
1739+
resolve_delivery_limit(PolVal, ArgVal) ->
1740+
min(PolVal, ArgVal).
1741+
1742+
get_delivery_limit(Q) ->
1743+
get_delivery_limit(Q, false).
1744+
1745+
get_delivery_limit(Q, ShouldLog) ->
1746+
PolicyValue = args_policy_lookup(<<"delivery-limit">>, fun resolve_delivery_limit/2, Q),
1747+
get_delivery_limit({handle_policy_value, PolicyValue}, Q, ShouldLog).
1748+
1749+
get_delivery_limit({handle_policy_value, undefined}, Q, true) ->
1750+
QName = amqqueue:get_name(Q),
1751+
?LOG_INFO("~ts: delivery_limit not set, defaulting to ~b",
1752+
[rabbit_misc:rs(QName), ?DEFAULT_DELIVERY_LIMIT]),
1753+
?DEFAULT_DELIVERY_LIMIT;
1754+
get_delivery_limit({handle_policy_value, undefined}, _Q, false) ->
1755+
?DEFAULT_DELIVERY_LIMIT;
1756+
get_delivery_limit({handle_policy_value, Limit}, _Q, _ShouldLog) when is_integer(Limit) ->
1757+
Limit.
1758+
17561759
i(name, Q) when ?is_amqqueue(Q) -> amqqueue:get_name(Q);
17571760
i(durable, Q) when ?is_amqqueue(Q) -> amqqueue:is_durable(Q);
17581761
i(auto_delete, Q) when ?is_amqqueue(Q) -> amqqueue:is_auto_delete(Q);
@@ -1887,6 +1890,8 @@ i(message_bytes_dlx, Q) when ?is_amqqueue(Q) ->
18871890
{timeout, _} ->
18881891
0
18891892
end;
1893+
i(delivery_limit, Q) when ?is_amqqueue(Q) ->
1894+
get_delivery_limit(Q);
18901895
i(_K, _Q) -> ''.
18911896

18921897
open_files(Name) ->
@@ -1992,7 +1997,11 @@ format(Q, Ctx) when ?is_amqqueue(Q) ->
19921997
{node, LeaderNode},
19931998
{members, Nodes},
19941999
{leader, LeaderNode},
1995-
{online, Online}].
2000+
{online, Online},
2001+
{policy, i(policy, Q)},
2002+
{operator_policy, i(operator_policy, Q)},
2003+
{effective_policy_definition, i(effective_policy_definition, Q)},
2004+
{delivery_limit, i(delivery_limit, Q)}].
19962005

19972006
-spec quorum_messages(rabbit_amqqueue:name()) -> non_neg_integer().
19982007

deps/rabbitmq_management/test/rabbit_mgmt_only_http_SUITE.erl

Lines changed: 88 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@ all() ->
3838
[
3939
{group, all_tests_with_prefix},
4040
{group, all_tests_without_prefix},
41-
{group, stats_disabled_on_request}
41+
{group, stats_disabled_on_request},
42+
{group, stats_disabled_via_config}
4243
].
4344

4445
groups() ->
4546
[
4647
{all_tests_with_prefix, [], some_tests() ++ all_tests()},
4748
{all_tests_without_prefix, [], some_tests()},
4849
{stats_disabled_on_request, [], [disable_with_disable_stats_parameter_test]},
50+
{stats_disabled_via_config, [], [
51+
classic_queue_with_stats_disabled_test,
52+
quorum_queue_with_stats_disabled_test
53+
]},
4954
{invalid_config, [], [invalid_config_test]}
5055
].
5156

@@ -130,6 +135,17 @@ init_per_group(all_tests_with_prefix = Group, Config0) ->
130135
Config1 = rabbit_ct_helpers:merge_app_env(Config0, PathConfig),
131136
Config2 = finish_init(Group, Config1),
132137
start_broker(Config2);
138+
init_per_group(stats_disabled_via_config = Group, Config0) ->
139+
Config1 = rabbit_ct_helpers:merge_app_env(Config0,
140+
{rabbitmq_management_agent, [
141+
{disable_metrics_collector, true}
142+
]}),
143+
Config2 = rabbit_ct_helpers:merge_app_env(Config1,
144+
{rabbitmq_management, [
145+
{disable_management_stats, true}
146+
]}),
147+
Config3 = finish_init(Group, Config2, false),
148+
start_broker(Config3);
133149
init_per_group(Group, Config0) ->
134150
Config1 = finish_init(Group, Config0),
135151
start_broker(Config1).
@@ -1442,6 +1458,77 @@ disable_with_disable_stats_parameter_test(Config) ->
14421458

14431459
passed.
14441460

1461+
classic_queue_with_stats_disabled_test(Config) ->
1462+
QArgs = #{arguments => #{'x-max-length' => 100}},
1463+
PolicyArgs = #{pattern => <<".*">>,
1464+
definition => #{'max-length' => 1024},
1465+
priority => 0,
1466+
'apply-to' => <<"queues">>},
1467+
OpPolicyArgs = #{pattern => <<".*">>,
1468+
definition => #{'max-length' => 2048},
1469+
priority => 0,
1470+
'apply-to' => <<"queues">>},
1471+
1472+
http_put(Config, "/queues/%2F/test-classic-queue", QArgs, {group, '2xx'}),
1473+
http_put(Config, "/policies/%2F/test-policy", PolicyArgs, {group, '2xx'}),
1474+
http_put(Config, "/operator-policies/%2F/test-op-policy", OpPolicyArgs, {group, '2xx'}),
1475+
1476+
await_condition(fun() ->
1477+
Queue = http_get(Config, "/queues/%2F/test-classic-queue", ?OK),
1478+
maps:get(policy, Queue, undefined) =:= <<"test-policy">>
1479+
end),
1480+
1481+
Queue = http_get(Config, "/queues/%2F/test-classic-queue", ?OK),
1482+
1483+
?assertEqual(<<"test-policy">>, maps:get(policy, Queue)),
1484+
?assertEqual(<<"test-op-policy">>, maps:get(operator_policy, Queue)),
1485+
?assert(maps:is_key(effective_policy_definition, Queue)),
1486+
EffectiveDef = maps:get(effective_policy_definition, Queue),
1487+
?assertEqual(1024, maps:get('max-length', EffectiveDef)),
1488+
1489+
http_delete(Config, "/queues/%2F/test-classic-queue", {group, '2xx'}),
1490+
http_delete(Config, "/policies/%2F/test-policy", {group, '2xx'}),
1491+
http_delete(Config, "/operator-policies/%2F/test-op-policy", {group, '2xx'}),
1492+
1493+
passed.
1494+
1495+
quorum_queue_with_stats_disabled_test(Config) ->
1496+
QArgs = #{durable => true,
1497+
arguments => #{'x-queue-type' => 'quorum',
1498+
'x-delivery-limit' => 40}},
1499+
PolicyArgs = #{pattern => <<".*">>,
1500+
definition => #{'queue-leader-locator' => <<"balanced">>},
1501+
priority => 0,
1502+
'apply-to' => <<"queues">>},
1503+
OpPolicyArgs = #{pattern => <<".*">>,
1504+
definition => #{'max-length' => 1024},
1505+
priority => 0,
1506+
'apply-to' => <<"queues">>},
1507+
1508+
http_put(Config, "/queues/%2F/test-quorum-queue", QArgs, {group, '2xx'}),
1509+
http_put(Config, "/policies/%2F/test-policy", PolicyArgs, {group, '2xx'}),
1510+
http_put(Config, "/operator-policies/%2F/test-op-policy", OpPolicyArgs, {group, '2xx'}),
1511+
1512+
await_condition(fun() ->
1513+
Queue = http_get(Config, "/queues/%2F/test-quorum-queue", ?OK),
1514+
maps:get(policy, Queue, undefined) =:= <<"test-policy">>
1515+
end),
1516+
1517+
Queue = http_get(Config, "/queues/%2F/test-quorum-queue", ?OK),
1518+
1519+
?assertEqual(<<"test-policy">>, maps:get(policy, Queue)),
1520+
?assertEqual(<<"test-op-policy">>, maps:get(operator_policy, Queue)),
1521+
?assert(maps:is_key(effective_policy_definition, Queue)),
1522+
EffectiveDef = maps:get(effective_policy_definition, Queue),
1523+
?assertEqual(1024, maps:get('max-length', EffectiveDef)),
1524+
?assertEqual(40, maps:get(delivery_limit, Queue)),
1525+
1526+
http_delete(Config, "/queues/%2F/test-quorum-queue", {group, '2xx'}),
1527+
http_delete(Config, "/policies/%2F/test-policy", {group, '2xx'}),
1528+
http_delete(Config, "/operator-policies/%2F/test-op-policy", {group, '2xx'}),
1529+
1530+
passed.
1531+
14451532
sorting_test(Config) ->
14461533
QArgs = #{},
14471534
PermArgs = [{configure, <<".*">>}, {write, <<".*">>}, {read, <<".*">>}],
@@ -1600,37 +1687,6 @@ local_port(Conn) ->
16001687
{ok, Port} = inet:port(Sock),
16011688
Port.
16021689

1603-
spawn_invalid(_Config, 0) ->
1604-
ok;
1605-
spawn_invalid(Config, N) ->
1606-
Self = self(),
1607-
spawn(fun() ->
1608-
timer:sleep(rand:uniform(250)),
1609-
{ok, Sock} = gen_tcp:connect("localhost", amqp_port(Config), [list]),
1610-
ok = gen_tcp:send(Sock, "Some Data"),
1611-
receive_msg(Self)
1612-
end),
1613-
spawn_invalid(Config, N-1).
1614-
1615-
receive_msg(Self) ->
1616-
receive
1617-
{tcp, _, [$A, $M, $Q, $P | _]} ->
1618-
Self ! done
1619-
after
1620-
60000 ->
1621-
Self ! no_reply
1622-
end.
1623-
1624-
wait_for_answers(0) ->
1625-
ok;
1626-
wait_for_answers(N) ->
1627-
receive
1628-
done ->
1629-
wait_for_answers(N-1);
1630-
no_reply ->
1631-
throw(no_reply)
1632-
end.
1633-
16341690
publish(Ch) ->
16351691
amqp_channel:call(Ch, #'basic.publish'{exchange = <<"">>,
16361692
routing_key = <<"myqueue">>},

0 commit comments

Comments
 (0)