diff --git a/logstash-core/lib/logstash/api/commands/base.rb b/logstash-core/lib/logstash/api/commands/base.rb index 873f4e7e1..d2bef44e6 100644 --- a/logstash-core/lib/logstash/api/commands/base.rb +++ b/logstash-core/lib/logstash/api/commands/base.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + module LogStash module Api module Commands diff --git a/logstash-core/lib/logstash/api/commands/default_metadata.rb b/logstash-core/lib/logstash/api/commands/default_metadata.rb index bae4f0811..2a5e03dbb 100644 --- a/logstash-core/lib/logstash/api/commands/default_metadata.rb +++ b/logstash-core/lib/logstash/api/commands/default_metadata.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + require "logstash/api/commands/base" module LogStash diff --git a/logstash-core/lib/logstash/api/commands/hot_threads_reporter.rb b/logstash-core/lib/logstash/api/commands/hot_threads_reporter.rb new file mode 100644 index 000000000..3b27a6e32 --- /dev/null +++ b/logstash-core/lib/logstash/api/commands/hot_threads_reporter.rb @@ -0,0 +1,61 @@ +# encoding: utf-8 + +class HotThreadsReport + HOT_THREADS_STACK_TRACES_SIZE_DEFAULT = 10.freeze + + def initialize(cmd, options) + @cmd = cmd + filter = { :stacktrace_size => options.fetch(:stacktrace_size, HOT_THREADS_STACK_TRACES_SIZE_DEFAULT) } + jr_dump = JRMonitor.threads.generate(filter) + @thread_dump = ::LogStash::Util::ThreadDump.new(options.merge(:dump => jr_dump)) + end + + def to_s + hash = to_hash + report = "#{I18n.t("logstash.web_api.hot_threads.title", :hostname => hash[:hostname], :time => hash[:time], :top_count => @thread_dump.top_count )} \n" + report << '=' * 80 + report << "\n" + hash[:threads].each do |thread| + thread_report = "" + thread_report = "#{I18n.t("logstash.web_api. + hot_threads.thread_title", :percent_of_cpu_time => thread[:percent_of_cpu_time], :thread_state => thread[:state], :thread_name => thread[:name])} \n" + thread_report = "#{thread[:percent_of_cpu_time]} % of of cpu usage by #{thread[:state]} thread named '#{thread[:name]}'\n" + thread_report << "#{thread[:path]}\n" if thread[:path] + thread[:traces].each do |trace| + thread_report << "\t#{trace}\n" + end + report << thread_report + report << '-' * 80 + report << "\n" + end + report + end + + def to_hash + hash = { :time => Time.now.iso8601, :busiest_threads => @thread_dump.top_count, :threads => [] } + @thread_dump.each do |thread_name, _hash| + thread_name, thread_path = _hash["thread.name"].split(": ") + thread = { :name => thread_name, + :percent_of_cpu_time => cpu_time_as_percent(_hash), + :state => _hash["thread.state"] + } + thread[:path] = thread_path if thread_path + traces = [] + _hash["thread.stacktrace"].each do |trace| + traces << trace + end + thread[:traces] = traces unless traces.empty? + hash[:threads] << thread + end + hash + end + + def cpu_time_as_percent(hash) + (((cpu_time(hash) / @cmd.uptime * 1.0)*10000).to_i)/100.0 + end + + def cpu_time(hash) + hash["cpu.time"] / 1000000.0 + end + +end diff --git a/logstash-core/lib/logstash/api/commands/node.rb b/logstash-core/lib/logstash/api/commands/node.rb index 6ffc42746..d35fac990 100644 --- a/logstash-core/lib/logstash/api/commands/node.rb +++ b/logstash-core/lib/logstash/api/commands/node.rb @@ -1,10 +1,12 @@ +# encoding: utf-8 require "logstash/api/commands/base" +require_relative "hot_threads_reporter" module LogStash module Api module Commands class Node < Commands::Base - + def all(selected_fields=[]) payload = { :pipeline => pipeline, @@ -14,7 +16,7 @@ module LogStash payload.select! { |k,v| selected_fields.include?(k) } unless selected_fields.empty? payload end - + def pipeline extract_metrics( [:stats, :pipelines, :main, :config], @@ -54,65 +56,6 @@ module LogStash HotThreadsReport.new(self, options) end - class HotThreadsReport - HOT_THREADS_STACK_TRACES_SIZE_DEFAULT = 10.freeze - - def initialize(cmd, options) - @cmd = cmd - filter = { :stacktrace_size => options.fetch(:stacktrace_size, HOT_THREADS_STACK_TRACES_SIZE_DEFAULT) } - jr_dump = JRMonitor.threads.generate(filter) - @thread_dump = ::LogStash::Util::ThreadDump.new(options.merge(:dump => jr_dump)) - end - - def to_s - hash = to_hash[:hot_threads] - report = "#{I18n.t("logstash.web_api.hot_threads.title", :hostname => hash[:hostname], :time => hash[:time], :top_count => @thread_dump.top_count )} \n" - report << '=' * 80 - report << "\n" - hash[:threads].each do |thread| - thread_report = "" - thread_report = "#{I18n.t("logstash.web_api. - hot_threads.thread_title", :percent_of_cpu_time => thread[:percent_of_cpu_time], :thread_state => thread[:state], :thread_name => thread[:name])} \n" - thread_report = "#{thread[:percent_of_cpu_time]} % of of cpu usage by #{thread[:state]} thread named '#{thread[:name]}'\n" - thread_report << "#{thread[:path]}\n" if thread[:path] - thread[:traces].each do |trace| - thread_report << "\t#{trace}\n" - end - report << thread_report - report << '-' * 80 - report << "\n" - end - report - end - - def to_hash - hash = { :time => Time.now.iso8601, :busiest_threads => @thread_dump.top_count, :threads => [] } - @thread_dump.each do |thread_name, _hash| - thread_name, thread_path = _hash["thread.name"].split(": ") - thread = { :name => thread_name, - :percent_of_cpu_time => cpu_time_as_percent(_hash), - :state => _hash["thread.state"] - } - thread[:path] = thread_path if thread_path - traces = [] - _hash["thread.stacktrace"].each do |trace| - traces << trace - end - thread[:traces] = traces unless traces.empty? - hash[:threads] << thread - end - { :hot_threads => hash } - end - - def cpu_time_as_percent(hash) - (((cpu_time(hash) / @cmd.uptime * 1.0)*10000).to_i)/100.0 - end - - def cpu_time(hash) - hash["cpu.time"] / 1000000.0 - end - - end end end end diff --git a/logstash-core/lib/logstash/api/commands/stats.rb b/logstash-core/lib/logstash/api/commands/stats.rb index c3a800571..d89770090 100644 --- a/logstash-core/lib/logstash/api/commands/stats.rb +++ b/logstash-core/lib/logstash/api/commands/stats.rb @@ -1,5 +1,7 @@ +# encoding: utf-8 require "logstash/api/commands/base" require 'logstash/util/thread_dump' +require_relative "hot_threads_reporter" module LogStash module Api @@ -61,65 +63,6 @@ module LogStash HotThreadsReport.new(self, options) end - class HotThreadsReport - HOT_THREADS_STACK_TRACES_SIZE_DEFAULT = 10.freeze - - def initialize(cmd, options) - @cmd = cmd - filter = { :stacktrace_size => options.fetch(:stacktrace_size, HOT_THREADS_STACK_TRACES_SIZE_DEFAULT) } - jr_dump = JRMonitor.threads.generate(filter) - @thread_dump = ::LogStash::Util::ThreadDump.new(options.merge(:dump => jr_dump)) - end - - def to_s - hash = to_hash - report = "#{I18n.t("logstash.web_api.hot_threads.title", :hostname => hash[:hostname], :time => hash[:time], :top_count => @thread_dump.top_count )} \n" - report << '=' * 80 - report << "\n" - hash[:threads].each do |thread| - thread_report = "" - thread_report = "#{I18n.t("logstash.web_api. - hot_threads.thread_title", :percent_of_cpu_time => thread[:percent_of_cpu_time], :thread_state => thread[:state], :thread_name => thread[:name])} \n" - thread_report = "#{thread[:percent_of_cpu_time]} % of of cpu usage by #{thread[:state]} thread named '#{thread[:name]}'\n" - thread_report << "#{thread[:path]}\n" if thread[:path] - thread[:traces].each do |trace| - thread_report << "\t#{trace}\n" - end - report << thread_report - report << '-' * 80 - report << "\n" - end - report - end - - def to_hash - hash = { :hostname => @cmd.hostname, :time => Time.now.iso8601, :busiest_threads => @thread_dump.top_count, :threads => [] } - @thread_dump.each do |thread_name, _hash| - thread_name, thread_path = _hash["thread.name"].split(": ") - thread = { :name => thread_name, - :percent_of_cpu_time => cpu_time_as_percent(_hash), - :state => _hash["thread.state"] - } - thread[:path] = thread_path if thread_path - traces = [] - _hash["thread.stacktrace"].each do |trace| - traces << trace - end - thread[:traces] = traces unless traces.empty? - hash[:threads] << thread - end - hash - end - - def cpu_time_as_percent(hash) - (((cpu_time(hash) / @cmd.uptime * 1.0)*10000).to_i)/100.0 - end - - def cpu_time(hash) - hash["cpu.time"] / 1000000.0 - end - end # class HotThreadsReport - module PluginsStats module_function diff --git a/logstash-core/lib/logstash/api/modules/base.rb b/logstash-core/lib/logstash/api/modules/base.rb index b47ca016d..5146dd96d 100644 --- a/logstash-core/lib/logstash/api/modules/base.rb +++ b/logstash-core/lib/logstash/api/modules/base.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 require "logstash/api/app_helpers" require "logstash/api/command_factory"