Created
June 16, 2017 16:49
-
-
Save colinsurprenant/671965da7ddd7b141bb73c24b9dd0425 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "net/http" | |
require "json" | |
require "thread" | |
Thread.abort_on_exception = true | |
queue = Queue.new | |
POLL_DELAY = 1 # in seconds | |
poller_thread = Thread.new do | |
seq = 0 | |
loop do | |
begin | |
response = Net::HTTP.start("localhost", 9600, {:read_timeout => 1, :open_timeout => 1}) {|http| http.get("/_node/stats")} | |
rescue Net::OpenTimeout, Net::ReadTimeout => e | |
$stderr.puts("http request exception: #{e.inspect}") | |
end | |
if response.is_a?(Net::HTTPSuccess) | |
# ignore json exceptions, just crash | |
stats = JSON.parse(response.body) | |
# out = stats["pipeline"]["events"]["out"] # path for master | |
out = stats["events"]["out"] # path for 5.4 | |
queue << { :seq => seq, :out => out } | |
else | |
$stderr.puts("http response error #{response.inspect}") | |
end | |
seq += POLL_DELAY | |
sleep(POLL_DELAY) | |
end | |
end | |
stats_thread = Thread.new do | |
last_stat = queue.pop | |
loop do | |
stat = queue.pop | |
rate = (stat[:out] - last_stat[:out]) / (stat[:seq] - last_stat[:seq]) | |
last_stat = stat | |
puts("#{stat[:seq]},#{rate}") | |
end | |
end | |
stats_thread.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment