class Presto::Metrics::Client
Public Class Methods
new(opts={})
click to toggle source
# File lib/presto/metrics/client.rb, line 5 def initialize(opts={}) require 'httparty' require 'json' require 'set' require 'time' require 'uri' @host = opts[:host] || 'localhost' @port = opts[:port] || '8080' @endpoint = opts[:endpoint] || "http://#{@host}:#{@port}" @mbean_path = opts[:mbean_path] || '/v1/jmx/mbean' @query_path = opts[:query_path] || '/v1/query' @node_path = opts[:node_path] || '/v1/node' @caml_case = opts[:caml_case] || false @headers = opts[:headers] || {} end
Public Instance Methods
cluster_memory_manager_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 233 def cluster_memory_manager_metrics(target_attr=[]) get_metrics("presto.memory:name=ClusterMemoryManager", target_attr) end
cluster_memory_pool_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 227 def cluster_memory_pool_metrics(target_attr=[]) ['general', 'reserved'].each_with_object({}) {|type, hash| hash[type] = get_metrics("presto.memory:type=ClusterMemoryPool,name=#{type}", target_attr) } end
extract_path(json_obj, path, depth)
click to toggle source
# File lib/presto/metrics/client.rb, line 57 def extract_path(json_obj, path, depth) return nil if json_obj.nil? if depth >= path.length json_obj else if json_obj.kind_of?(Array) # Handle key, value pairs of GC information value = json_obj.find { |e| e.is_a?(Hash) && e['key'] == path[depth] } extract_path(value['value'], path, depth+1) else extract_path(json_obj[path[depth]], path, depth+1) end end end
extract_value(v)
click to toggle source
# File lib/presto/metrics/client.rb, line 150 def extract_value(v) if v.is_a?(Hash) if v.has_key?('key') && v.has_key?('value') {sanitize_key(v['key']) => extract_value(v['value']) } else v.each_with_object({}) {|(k1, v1), h2| h2[sanitize_key(k1)] = extract_value(v1) } end elsif v.is_a?(Array) && v.all?{|e| e.is_a?(Hash) } v.each_with_object({}){|e, h| h.merge!(extract_value(e)) } else v end end
gc_cms_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 183 def gc_cms_metrics(target_attr=[]) get_gc_metrics('java.lang:type=GarbageCollector,name=ConcurrentMarkSweep', target_attr) end
gc_g1_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 191 def gc_g1_metrics(target_attr=[]) ['G1 Old Generation', 'G1 Young Generation'].each_with_object({}){|gen, h| h[sanitize_key(gen)] = get_gc_metrics("java.lang:type=GarbageCollector,name=#{gen}", target_attr) } end
gc_parnew_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 187 def gc_parnew_metrics(target_attr=[]) get_gc_metrics('java.lang:type=GarbageCollector,name=ParNew', target_attr) end
get(path, default='{}')
click to toggle source
# File lib/presto/metrics/client.rb, line 82 def get(path, default='{}') parser = URI::Parser.new resp = HTTParty.get(parser.escape("#{@endpoint}#{path}"), headers: @headers) if resp.code == 200 resp.body else default end end
get_attribute(mbean, attr_name)
click to toggle source
# File lib/presto/metrics/client.rb, line 109 def get_attribute(mbean, attr_name) get_attributes(mbean).find { |obj| obj['name'] == attr_name } || {} end
get_attributes(mbean)
click to toggle source
# File lib/presto/metrics/client.rb, line 104 def get_attributes(mbean) json = get_mbean(mbean) json['attributes'] || [] end
get_gc_metrics(mbean, target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 166 def get_gc_metrics(mbean, target_attr=[]) h = {} get_attributes(mbean) .reject { |attr| attr['name'].nil? || attr['value'].nil? } .each { |attr| key = sanitize_key(attr['name']) v = attr['value'] h[key] = extract_value(v) } h end
get_mbean(mbean)
click to toggle source
# File lib/presto/metrics/client.rb, line 78 def get_mbean(mbean) JSON.parse(get_mbean_json(mbean)) end
get_mbean_json(mbean)
click to toggle source
# File lib/presto/metrics/client.rb, line 92 def get_mbean_json(mbean) get("#{@mbean_path}/#{mbean}") end
get_metrics(mbean, target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 130 def get_metrics(mbean, target_attr=[]) kv = Hash.new arr = target_attr.kind_of?(Array) ? target_attr : [target_attr] c_target_attr = map_to_canonical_name(arr).to_set get_attributes(mbean) .reject { |attr| attr['name'].nil? || attr['value'].nil? } .each { |attr| c_name = to_canonical_name(attr['name']) if c_target_attr.empty? || c_target_attr.include?(c_name) key = @caml_case ? attr['name'] : underscore(attr['name']) kv[key] = attr['value'] end } kv end
get_node_json()
click to toggle source
# File lib/presto/metrics/client.rb, line 100 def get_node_json get(@node_path) end
get_query_json(path='', default='[]')
click to toggle source
# File lib/presto/metrics/client.rb, line 96 def get_query_json(path='', default='[]') get("#{@query_path}/#{path}", default) end
memory_pool_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 221 def memory_pool_metrics(target_attr=[]) ['general', 'reserved'].each_with_object({}) {|type, hash| hash[type] = get_metrics("presto.memory:type=MemoryPool,name=#{type}", target_attr) } end
memory_usage_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 179 def memory_usage_metrics(target_attr=[]) get_metrics('java.lang:type=Memory', target_attr) end
node_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 238 def node_metrics(target_attr=[]) p = URI::Parser.new node_state = JSON.parse(get_node_json) node_state.map{|n| uri = n['uri'] || '' m = {} m['host'] = p.parse(uri).host n.each{|k, v| key = @caml_case ? k : underscore(k) m[key] = v } m } end
node_scheduler_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 209 def node_scheduler_metrics(target_attr=[]) get_metrics('presto.execution:name=NodeScheduler', target_attr) end
os_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 197 def os_metrics(target_attr=[]) get_metrics('java.lang:type=OperatingSystem', target_attr) end
path(path)
click to toggle source
# File lib/presto/metrics/client.rb, line 41 def path(path) c = path.split(/:/) target = c[0] mbean = @@MBEAN_ALIAS[target] || target json_obj = get_metrics(mbean) return json_obj if c.size <= 1 query_list = (c[1] || '').split(/,/) result = {} query_list.each { |q| path_elems = q.split('/') target_elem = extract_path(json_obj, path_elems, 0) result[q] = target_elem unless target_elem.nil? } result end
query()
click to toggle source
# File lib/presto/metrics/client.rb, line 74 def query Query.new(self) end
query_execution_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 205 def query_execution_metrics(target_attr=[]) get_metrics('presto.execution:name=QueryExecution', target_attr) end
query_manager_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 201 def query_manager_metrics(target_attr=[]) get_metrics('presto.execution:name=QueryManager', target_attr) end
sanitize_key(k)
click to toggle source
# File lib/presto/metrics/client.rb, line 146 def sanitize_key(k) @caml_case ? k : underscore(k) end
task_executor_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 213 def task_executor_metrics(target_attr=[]) get_metrics('presto.execution:name=TaskExecutor', target_attr) end
task_manager_metrics(target_attr=[])
click to toggle source
# File lib/presto/metrics/client.rb, line 217 def task_manager_metrics(target_attr=[]) get_metrics('presto.execution:name=TaskManager', target_attr) end
Private Instance Methods
map_to_canonical_name(name_arr)
click to toggle source
# File lib/presto/metrics/client.rb, line 113 def map_to_canonical_name(name_arr) name_arr.map { |name| to_canonical_name(name) } end
to_canonical_name(name)
click to toggle source
# File lib/presto/metrics/client.rb, line 117 def to_canonical_name(name) name.to_s.downcase.gsub(/_/, '') end
underscore(str)
click to toggle source
# File lib/presto/metrics/client.rb, line 121 def underscore(str) str.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). gsub(/([a-z\d])([A-Z])/, '\1_\2'). gsub(/ +/,'_'). tr('-', '_'). downcase end