Skip to content

Instantly share code, notes, and snippets.

@justinstoller
Created January 24, 2018 23:29
Show Gist options
  • Save justinstoller/407e52d1c0b476db04ade108eea7f943 to your computer and use it in GitHub Desktop.
Save justinstoller/407e52d1c0b476db04ade108eea7f943 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
def dig(hash, keys)
keys.reduce(hash) {|h, k| i = h[k]; i ? i : break }
end
def to_csv(json, fields)
fields.map{|f| json[f] }.join(',')
end
def get_default_gc_stats(metrics)
[
['jvm.gc.scavenge.count', metrics['PS Scavenge']['count']],
['jvm.gc.scavenge.total-time', metrics['PS Scavenge']['total-time-ms']],
['jvm.gc.marksweep.count', metrics['PS MarkSweep']['count']],
['jvm.gc.marksweep.total-time', metrics['PS MarkSweep']['total-time-ms']]
]
end
def get_cms_gc_stats(metrics)
[
['jvm.gc.parnew.count', metrics['ParNew']['count']],
['jvm.gc.parnew.total-time', metrics['ParNew']['total-time-ms']],
['jvm.gc.marksweep.count', metrics['ConcurrentMarkSweep']['count']],
['jvm.gc.marksweep.total-time', metrics['ConcurrentMarkSweep']['total-time-ms']]
]
end
def get_g1_gc_stats(metrics)
[
['jvm.gc.new-gen.count', metrics['G1 Young Generation']['count']],
['jvm.gc.new-gen.total-time', metrics['G1 Young Generation']['total-time-ms']],
['jvm.gc.old-gen.count', metrics['G1 Old Generation']['count']],
['jvm.gc.old-gen.total-time', metrics['G1 Old Generation']['total-time-ms']]
]
end
JRUBY_METRICS = %w(borrow-timeout-count borrow-retry-count average-borrow-time average-wait-time)
JRUBY_HIERARCHY = %w(jruby-metrics status experimental metrics)
HTTP_HIERARCHY = %w(master status experimental http-metrics)
JVM_HIERARCHY = %w(status-service status experimental jvm-metrics)
HEADER = %w(
jruby-metrics.borrow-timeout-count
jruby-metrics.borrow-retry-count
jruby-metrics.average-borrow-time
jruby-metrics.average-wait-time
jvm.heap-memory.committed
jvm.heap-memory.init
jvm.heap-memory.used
jvm.non-heap-memory.committed
jvm.non-heap-memory.init
jvm.non-heap-memory.used
jvm.file-descriptors
jvm.cpu-usage
jvm.up-time
jvm.thread-count
jvm.gc.scavenge.count
jvm.gc.scavenge.total-time
jvm.gc.parnew.count
jvm.gc.parnew.total-time
jvm.gc.marksweep.count
jvm.gc.marksweep.total-time
jvm.gc.new-gen.count
jvm.gc.new-gen.total-time
jvm.gc.old-gen.count
jvm.gc.old-gen.total-time)
def format_puppet_info(metrics)
dig(metrics, PUPPET_HIERARCHY)
end
def format_jruby_metrics(metrics)
dig(metrics, JRUBY_HIERARCHY).
select {|k, _| JRUBY_METRICS.include? k }.
map {|k, v| ['jruby-metrics.' + k.to_s, v] }.
to_h
end
def format_jvm_metrics(raw_metrics)
metrics = dig(raw_metrics, JVM_HIERARCHY)
memory = %w(heap-memory non-heap-memory).flat_map {|mem_type|
%w(committed init used).map {|mem_metric|
["jvm.#{mem_type}.#{mem_metric}", metrics[mem_type][mem_metric]]
}
}
system = [
['jvm.file-descriptors', metrics['file-descriptors']['used']],
['jvm.cpu-usage', metrics['cpu-usage']],
['jvm.up-time', metrics['up-time-ms']]
]
if metrics['threading']
system << ['jvm.thread-count', metrics['threading']['thread-count']]
end
if metrics['gc-stats']['PS Scavenge']
gc = get_default_gc_stats(metrics['gc-stats'])
elsif metrics['gc-stats']['ParNew']
gc = get_cms_gc_stats(metrics['gc-stats'])
elsif metrics['gc-stats']['G1 Young Generation']
gc = get_g1_gc_stats(metrics['gc-stats'])
end
(gc + system + memory).to_h
end
out = File.open(ARGV[0]).each_line.map do |line|
raw_metrics = JSON.parse line
jruby_metrics = format_jruby_metrics(raw_metrics)
jvm_metrics = format_jvm_metrics(raw_metrics)
metrics = jruby_metrics.merge(jvm_metrics)
HEADER.map {|field| metrics[field] }.join(',')
end
STDOUT.puts HEADER.join(',')
STDOUT.puts out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment