add the magic header # encoding: utf-8 to a few files, plus refactoring the hot threads code to be in single location

Fixes #5630
This commit is contained in:
Pere Urbon-Bayes 2016-07-13 13:47:45 +02:00
parent 0a59cde937
commit e812821fd2
6 changed files with 72 additions and 120 deletions

View file

@ -1,3 +1,5 @@
# encoding: utf-8
module LogStash
module Api
module Commands

View file

@ -1,3 +1,5 @@
# encoding: utf-8
require "logstash/api/commands/base"
module LogStash

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,3 +1,4 @@
# encoding: utf-8
require "logstash/api/app_helpers"
require "logstash/api/command_factory"