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-2021 VMware, Inc. or its affiliates.  All rights reserved.
6
7defmodule RabbitMQ.CLI.Diagnostics.Commands.RuntimeThreadStatsCommand do
8  alias RabbitMQ.CLI.Core.DocGuide
9
10  @behaviour RabbitMQ.CLI.CommandBehaviour
11
12  def switches(), do: [sample_interval: :integer]
13  def aliases(), do: [i: :sample_interval]
14
15  def merge_defaults(args, opts) do
16    {args, Map.merge(%{sample_interval: 5}, opts)}
17  end
18
19  use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments
20  use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
21
22  def run([], %{node: node_name, timeout: timeout, sample_interval: interval}) do
23    case :rabbit_misc.rpc_call(
24           node_name,
25           :rabbit_runtime,
26           :msacc_stats,
27           [interval * 1000],
28           timeout
29         ) do
30      {:ok, stats} -> stats
31      other -> other
32    end
33  end
34
35  def output(result, %{formatter: "json"}) when is_list(result) do
36    {:error, "JSON formatter is not supported by this command"}
37  end
38
39  def output(result, %{formatter: "csv"}) when is_list(result) do
40    {:error, "CSV formatter is not supported by this command"}
41  end
42
43  def output(result, _options) when is_list(result) do
44    {:ok, result}
45  end
46
47  def help_section(), do: :observability_and_health_checks
48
49  def description(), do: "Provides a breakdown of runtime thread activity stats on the target node"
50
51  def usage, do: "runtime_thread_stats [--sample-interval <interval>]"
52
53  def usage_additional() do
54    [
55      ["--sample-interval <seconds>", "sampling interval to use in seconds"]
56    ]
57  end
58
59  def usage_doc_guides() do
60    [
61      DocGuide.runtime_tuning()
62    ]
63  end
64
65  def banner([], %{node: node_name, sample_interval: interval}) do
66    "Will collect runtime thread stats on #{node_name} for #{interval} seconds..."
67  end
68
69  def formatter(), do: RabbitMQ.CLI.Formatters.Msacc
70end
71