Class: Presto::Metrics::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/presto/metrics/client.rb

Constant Summary collapse

@@MBEAN_ALIAS =
{
  'memory' => 'java.lang:type=Memory',
  'gc_cms' => 'java.lang:type=GarbageCollector,name=ConcurrentMarkSweep',
  'gc_parnew' => 'java.lang:type=GarbageCollector,name=ParNew',
  'os' => 'java.lang:type=OperatingSystem',
  'query_manager' => 'com.facebook.presto.execution:name=QueryManager',
  'query_execution' => 'com.facebook.presto.execution:name=QueryExecution',
  'node_scheduler' => 'com.facebook.presto.execution:name=NodeScheduler',
  'task_executor' => 'com.facebook.presto.execution:name=TaskExecutor',
  'task_manager' => 'com.facebook.presto.execution:name=TaskManager'
}

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/presto/metrics/client.rb', line 6

def initialize(opts={})
    require 'httparty'
    require 'json'
    require 'set'
    require 'time'

    @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'
  @caml_case = opts[:caml_case] || false
end

Instance Method Details

#extract_path(json_obj, path, depth) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/presto/metrics/client.rb', line 50

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

#gc_cms_metrics(target_attr = []) ⇒ Object



138
139
140
# File 'lib/presto/metrics/client.rb', line 138

def gc_cms_metrics(target_attr=[])
  get_metrics('java.lang:type=GarbageCollector,name=ConcurrentMarkSweep', target_attr)
end

#gc_parnew_metrics(target_attr = []) ⇒ Object



142
143
144
# File 'lib/presto/metrics/client.rb', line 142

def gc_parnew_metrics(target_attr=[])
  get_metrics('java.lang:type=GarbageCollector,name=ParNew', target_attr)
end

#get(path, default = '{}') ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/presto/metrics/client.rb', line 75

def get(path, default='{}')
  resp = HTTParty.get("#{@endpoint}#{path}")
  if resp.code == 200
    resp.body        
  else
    default
  end
end

#get_attribute(mbean, attr_name) ⇒ Object



97
98
99
# File 'lib/presto/metrics/client.rb', line 97

def get_attribute(mbean, attr_name) 
      get_attributes(mbean).find {|obj| obj['name'] == attr_name } || {}
end

#get_attributes(mbean) ⇒ Object



92
93
94
95
# File 'lib/presto/metrics/client.rb', line 92

def get_attributes(mbean) 
      json = get_mbean(mbean)
      json['attributes'] || []
end

#get_mbean(mbean) ⇒ Object



71
72
73
# File 'lib/presto/metrics/client.rb', line 71

def get_mbean(mbean)
      JSON.parse(get_mbean_json(mbean))
end

#get_mbean_json(mbean) ⇒ Object



84
85
86
# File 'lib/presto/metrics/client.rb', line 84

def get_mbean_json(mbean) 
  get("#{@mbean_path}/#{mbean}")
end

#get_metrics(mbean, target_attr = []) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/presto/metrics/client.rb', line 117

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_query_json(path = '', default = '[]') ⇒ Object



88
89
90
# File 'lib/presto/metrics/client.rb', line 88

def get_query_json(path='', default='[]')
      get("#{@query_path}/#{path}",default)
end

#memory_usage_metrics(target_attr = []) ⇒ Object



134
135
136
# File 'lib/presto/metrics/client.rb', line 134

def memory_usage_metrics(target_attr=[])
  get_metrics('java.lang:type=Memory', target_attr)
end

#node_scheduler_metrics(target_attr = []) ⇒ Object



158
159
160
# File 'lib/presto/metrics/client.rb', line 158

def node_scheduler_metrics(target_attr=[])
  get_metrics('com.facebook.presto.execution:name=NodeScheduler', target_attr)
end

#os_metrics(target_attr = []) ⇒ Object



146
147
148
# File 'lib/presto/metrics/client.rb', line 146

def os_metrics(target_attr=[])
  get_metrics('java.lang:type=OperatingSystem', target_attr)
end

#path(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/presto/metrics/client.rb', line 33

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

#queryObject



67
68
69
# File 'lib/presto/metrics/client.rb', line 67

def query 
  Query.new(self)
end

#query_execution_metrics(target_attr = []) ⇒ Object



154
155
156
# File 'lib/presto/metrics/client.rb', line 154

def query_execution_metrics(target_attr=[])
  get_metrics('com.facebook.presto.execution:name=QueryExecution', target_attr)
end

#query_manager_metrics(target_attr = []) ⇒ Object



150
151
152
# File 'lib/presto/metrics/client.rb', line 150

def query_manager_metrics(target_attr=[])
  get_metrics('com.facebook.presto.execution:name=QueryManager', target_attr)
end

#task_executor_metrics(target_attr = []) ⇒ Object



162
163
164
# File 'lib/presto/metrics/client.rb', line 162

def task_executor_metrics(target_attr=[])
  get_metrics('com.facebook.presto.execution:name=TaskExecutor', target_attr)
end

#task_manager_metrics(target_attr = []) ⇒ Object



166
167
168
# File 'lib/presto/metrics/client.rb', line 166

def task_manager_metrics(target_attr=[])
  get_metrics('com.facebook.presto.execution:name=TaskManager', target_attr)
end