Skip to content
This repository was archived by the owner on Nov 18, 2020. It is now read-only.

Commit c549841

Browse files
Merge pull request #282 from rabbitmq/msacc-command
New diagnostics command: runtime_thread_stats
2 parents e878828 + 515c175 commit c549841

8 files changed

+161
-4
lines changed

lib/rabbitmq/cli/diagnostics/commands/discover_peers_command.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ defmodule RabbitMQ.CLI.Diagnostics.Commands.DiscoverPeersCommand do
2525
def validate([_|_], _) do
2626
{:validation_failure, :too_many_args}
2727
end
28+
2829
def validate(_, _), do: :ok
30+
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
2931

3032
def run([], %{node: node_name, timeout: timeout}) do
3133
:rabbit_misc.rpc_call(node_name, :rabbit_peer_discovery, :discover_cluster_nodes, [], timeout)
@@ -48,6 +50,4 @@ defmodule RabbitMQ.CLI.Diagnostics.Commands.DiscoverPeersCommand do
4850
def usage, do: "discover_peers"
4951

5052
def banner(_,_), do: "Discovering peers nodes ..."
51-
52-
5353
end

lib/rabbitmq/cli/diagnostics/commands/maybe_stuck_command.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ defmodule RabbitMQ.CLI.Diagnostics.Commands.MaybeStuckCommand do
2727
{:validation_failure, :too_many_args}
2828
end
2929
def validate(_, _), do: :ok
30+
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
3031

3132
def run([], %{node: node_name, timeout: timeout}) do
3233
:rabbit_misc.rpc_call(node_name, :rabbit_diagnostics, :maybe_stuck, [], timeout)

lib/rabbitmq/cli/diagnostics/commands/memory_breakdown_command.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ defmodule RabbitMQ.CLI.Diagnostics.Commands.MemoryBreakdownCommand do
3737
{:validation_failure, "unit '#{unit}' is not supported. Please use one of: bytes, mb, gb"}
3838
end
3939
end
40+
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
4041

4142
def run([], %{node: node_name, timeout: timeout}) do
4243
:rabbit_misc.rpc_call(node_name, :rabbit_vm, :memory, [], timeout)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## The contents of this file are subject to the Mozilla Public License
2+
## Version 1.1 (the "License"); you may not use this file except in
3+
## compliance with the License. You may obtain a copy of the License
4+
## at http://www.mozilla.org/MPL/
5+
##
6+
## Software distributed under the License is distributed on an "AS IS"
7+
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
8+
## the License for the specific language governing rights and
9+
## limitations under the License.
10+
##
11+
## The Original Code is RabbitMQ.
12+
##
13+
## The Initial Developer of the Original Code is GoPivotal, Inc.
14+
## Copyright (c) 2007-2018 Pivotal Software, Inc. All rights reserved.
15+
16+
17+
defmodule RabbitMQ.CLI.Diagnostics.Commands.RuntimeThreadStatsCommand do
18+
@behaviour RabbitMQ.CLI.CommandBehaviour
19+
20+
def switches(), do: [sample_interval: :integer]
21+
def aliases(), do: [i: :sample_interval]
22+
23+
def merge_defaults(args, opts) do
24+
{args, Map.merge(%{sample_interval: 5}, opts)}
25+
end
26+
27+
def validate(args, _) when length(args) > 0 do
28+
{:validation_failure, :too_many_args}
29+
end
30+
def validate(_, _), do: :ok
31+
32+
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
33+
34+
def run([], %{node: node_name, timeout: timeout, sample_interval: interval}) do
35+
case :rabbit_misc.rpc_call(node_name, :rabbit_runtime, :msacc_stats, [interval * 1000], timeout) do
36+
{:ok, stats} -> stats;
37+
other -> other
38+
end
39+
end
40+
41+
def output(result, %{formatter: :json}) when is_list(result) do
42+
{:error, "JSON formatter is not supported by this command"}
43+
end
44+
45+
def output(result, %{formatter: :csv}) when is_list(result) do
46+
{:error, "CSV formatter is not supported by this command"}
47+
end
48+
49+
def output(result, _options) when is_list(result) do
50+
{:ok, result}
51+
end
52+
53+
def banner([], %{node: node_name, sample_interval: interval}) do
54+
"Will collect runtime thread stats on #{node_name} for #{interval} seconds..."
55+
end
56+
57+
def usage, do: "runtime_thread_stats [--sample-interval <interval>]"
58+
59+
def formatter(), do: RabbitMQ.CLI.Formatters.Msacc
60+
end

lib/rabbitmq/cli/formatters/msacc.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## The contents of this file are subject to the Mozilla Public License
2+
## Version 1.1 (the "License"); you may not use this file except in
3+
## compliance with the License. You may obtain a copy of the License
4+
## at http://www.mozilla.org/MPL/
5+
##
6+
## Software distributed under the License is distributed on an "AS IS"
7+
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
8+
## the License for the specific language governing rights and
9+
## limitations under the License.
10+
##
11+
## The Original Code is RabbitMQ.
12+
##
13+
## The Initial Developer of the Original Code is GoPivotal, Inc.
14+
## Copyright (c) 2007-2018 Pivotal Software, Inc. All rights reserved.
15+
16+
defmodule RabbitMQ.CLI.Formatters.Msacc do
17+
@behaviour RabbitMQ.CLI.FormatterBehaviour
18+
19+
def format_output(output, _) do
20+
{:ok, io} = StringIO.open("")
21+
:msacc.print(io, output, %{})
22+
StringIO.flush(io)
23+
end
24+
25+
def format_stream(stream, options) do
26+
[format_output(Enum.to_list(stream), options)]
27+
end
28+
end

test/diagnostics/cipher_suites_command_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ defmodule CipherSuitesCommandTest do
2323
setup_all do
2424
RabbitMQ.CLI.Core.Distribution.start()
2525

26-
2726
:ok
2827
end
2928

test/diagnostics/discover_peers_command_test.exs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ defmodule DiscoverPeersCommandTest do
2727
{:ok, opts: %{node: get_rabbit_hostname(), timeout: context[:test_timeout]}}
2828
end
2929

30+
test "validate: providing no arguments passes validation", context do
31+
assert @command.validate([], context[:opts]) == :ok
32+
end
33+
34+
test "validate: providing any arguments fails validation", context do
35+
assert @command.validate(["a"], context[:opts]) ==
36+
{:validation_failure, :too_many_args}
37+
end
38+
3039
@tag test_timeout: 15000
31-
test "run: return a list of nodes when the backend isn't configured", context do
40+
test "run: returns a list of nodes when the backend isn't configured", context do
3241
assert match?({:ok, {[], _}}, @command.run([], context[:opts]))
3342
end
3443
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## The contents of this file are subject to the Mozilla Public License
2+
## Version 1.1 (the "License"); you may not use this file except in
3+
## compliance with the License. You may obtain a copy of the License
4+
## at http://www.mozilla.org/MPL/
5+
##
6+
## Software distributed under the License is distributed on an "AS IS"
7+
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
8+
## the License for the specific language governing rights and
9+
## limitations under the License.
10+
##
11+
## The Original Code is RabbitMQ.
12+
##
13+
## The Initial Developer of the Original Code is GoPivotal, Inc.
14+
## Copyright (c) 2007-2018 Pivotal Software, Inc. All rights reserved.
15+
16+
17+
defmodule RuntimeThreadStatsCommandTest do
18+
use ExUnit.Case
19+
import TestHelper
20+
21+
@command RabbitMQ.CLI.Diagnostics.Commands.RuntimeThreadStatsCommand
22+
23+
setup_all do
24+
RabbitMQ.CLI.Core.Distribution.start()
25+
26+
:ok
27+
end
28+
29+
setup context do
30+
{:ok, opts: %{
31+
node: get_rabbit_hostname(),
32+
timeout: context[:test_timeout] || 10000,
33+
sample_interval: 1
34+
}}
35+
end
36+
37+
38+
test "validate: providing no arguments passes validation", context do
39+
assert @command.validate([], context[:opts]) == :ok
40+
end
41+
42+
test "validate: providing any arguments fails validation", context do
43+
assert @command.validate(["a"], context[:opts]) ==
44+
{:validation_failure, :too_many_args}
45+
end
46+
47+
@tag test_timeout: 2000
48+
test "run: targeting an unreachable node throws a badrpc", context do
49+
assert @command.run([], Map.merge(context[:opts], %{node: :jake@thedog})) == {:badrpc, :nodedown}
50+
end
51+
52+
@tag test_timeout: 6000
53+
test "run: returns msacc-formatted output", context do
54+
res = @command.run([], context[:opts])
55+
# the output is long and its values are environment-specific,
56+
# so we simply assert that it is non-empty. MK.
57+
assert length(res) > 0
58+
end
59+
end

0 commit comments

Comments
 (0)