Skip to content

Commit 5771ffc

Browse files
committed
introduce a cli list_channel_interceptors_command to list all active
channel interceptors with their configured meta information
1 parent a5ef59f commit 5771ffc

4 files changed

Lines changed: 137 additions & 2 deletions

File tree

deps/rabbit/src/rabbit_channel_interceptor.erl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
-include_lib("rabbit_common/include/rabbit.hrl").
1111

12-
-export([init/1, intercept_in/3]).
12+
-export([init/1, intercept_in/3, list/0]).
1313

1414
-behaviour(rabbit_registry_class).
1515

@@ -37,6 +37,10 @@ added_to_rabbit_registry(_Type, _ModuleName) ->
3737
removed_from_rabbit_registry(_Type) ->
3838
rabbit_channel:refresh_interceptors().
3939

40+
list() ->
41+
Mods = [M || {_, M} <- rabbit_registry:lookup_all(channel_interceptor)],
42+
[[{name, Mod}, {applies_to, Mod:applies_to()}, {priority, priority(Mod)}] || Mod <- Mods].
43+
4044
init(Ch) ->
4145
Mods = [M || {_, M} <- rabbit_registry:lookup_all(channel_interceptor)],
4246
Sorted = lists:sort(fun(A, B) -> priority(A) =< priority(B) end, Mods),

deps/rabbit/test/dummy_interceptor.erl renamed to deps/rabbit_common/src/dummy_interceptor.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-include_lib("rabbit_common/include/rabbit_framing.hrl").
77

88

9-
-compile(export_all).
9+
-export([init/1, description/0, intercept/3, applies_to/0]).
1010

1111
init(_Ch) ->
1212
undefined.
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) 2007-2026 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
7+
defmodule RabbitMQ.CLI.Ctl.Commands.ListChannelInterceptorsCommand do
8+
@behaviour RabbitMQ.CLI.CommandBehaviour
9+
10+
def scopes(), do: [:ctl, :diagnostics]
11+
12+
use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout
13+
use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments
14+
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
15+
16+
def merge_defaults(args, opts) do
17+
{args, Map.merge(%{table_headers: true}, opts)}
18+
end
19+
20+
def run([], %{node: node_name, timeout: timeout}) do
21+
case :rabbit_misc.rpc_call(node_name, :rabbit_channel_interceptor, :list, [], timeout) do
22+
{:badrpc, _} = err ->
23+
err
24+
25+
interceptors ->
26+
Enum.map(interceptors, fn info ->
27+
Keyword.update!(info, :applies_to, &Enum.join(&1, ", "))
28+
end)
29+
end
30+
end
31+
32+
use RabbitMQ.CLI.DefaultOutput
33+
34+
def formatter(), do: RabbitMQ.CLI.Formatters.Table
35+
36+
def usage, do: "list_channel_interceptors [--no-table-headers]"
37+
38+
def help_section(), do: :observability_and_health_checks
39+
40+
def description(),
41+
do: "Lists registered channel interceptors with their name, intercepted AMQP operations, and priority"
42+
43+
def banner(_, _), do: "Listing channel interceptors ..."
44+
end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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-2026 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
7+
defmodule ListChannelInterceptorsCommandTest do
8+
use ExUnit.Case, async: false
9+
import TestHelper
10+
11+
@command RabbitMQ.CLI.Ctl.Commands.ListChannelInterceptorsCommand
12+
13+
setup_all do
14+
RabbitMQ.CLI.Core.Distribution.start()
15+
16+
:ok
17+
end
18+
19+
setup context do
20+
{
21+
:ok,
22+
opts: %{
23+
node: get_rabbit_hostname(),
24+
timeout: context[:test_timeout] || :infinity
25+
}
26+
}
27+
end
28+
29+
test "merge_defaults: adds table_headers true to opts", context do
30+
{args, opts} = @command.merge_defaults([], context[:opts])
31+
assert args == []
32+
assert opts[:table_headers] == true
33+
end
34+
35+
test "validate: accepts no positional arguments", context do
36+
assert @command.validate([], context[:opts]) == :ok
37+
end
38+
39+
test "validate: rejects any positional arguments", context do
40+
assert @command.validate(["extra"], context[:opts]) ==
41+
{:validation_failure, :too_many_args}
42+
end
43+
44+
test "run: on a bad RabbitMQ node, returns a badrpc" do
45+
opts = %{node: :jake@thedog, timeout: 200}
46+
assert match?({:badrpc, _}, @command.run([], opts))
47+
end
48+
49+
@tag test_timeout: :infinity
50+
test "run: with no interceptors registered, returns an empty list", context do
51+
result = @command.run([], context[:opts])
52+
assert result == []
53+
end
54+
55+
@tag test_timeout: :infinity
56+
test "run: registered interceptors are listed with applies_to joined as a string", context do
57+
node = get_rabbit_hostname()
58+
59+
:ok =
60+
:rabbit_misc.rpc_call(
61+
node,
62+
:rabbit_registry,
63+
:register,
64+
[:channel_interceptor, <<"test interceptor">>, :dummy_interceptor]
65+
)
66+
67+
try do
68+
result = @command.run([], context[:opts])
69+
assert is_list(result)
70+
interceptor = Enum.find(result, fn info -> info[:name] == :dummy_interceptor end)
71+
assert interceptor != nil
72+
assert is_binary(interceptor[:applies_to])
73+
assert is_integer(interceptor[:priority])
74+
after
75+
:rabbit_misc.rpc_call(
76+
node,
77+
:rabbit_registry,
78+
:unregister,
79+
[:channel_interceptor, <<"test interceptor">>]
80+
)
81+
end
82+
end
83+
84+
test "banner: returns the expected string", context do
85+
assert @command.banner([], context[:opts]) == "Listing channel interceptors ..."
86+
end
87+
end

0 commit comments

Comments
 (0)