Skip to content

Commit

Permalink
Add stream consumer example
Browse files Browse the repository at this point in the history
  • Loading branch information
malditogeek committed Sep 8, 2012
1 parent f9080d1 commit 4148694
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/client.rb
@@ -1,6 +1,10 @@
#!/usr/bin/env ruby
# encoding: UTF-8

##
## Send metrics to Astor using the StatsD client.
##

if ARGV.empty?
puts 'Usage: ./client.rb <namespace> <metric> <value>'
exit(2)
Expand Down
38 changes: 38 additions & 0 deletions examples/stream_consumer.rb
@@ -0,0 +1,38 @@
#!/usr/bin/env ruby
# encoding: UTF-8

##
## Consuming data from the event stream (zmq subscriber):
## MaxValueTracker will track and display the metric with the highest value.
##

require 'bundler'
Bundler.require :server
require 'time'

class MaxValueTracker
def initialize
@max = 0
end

def on_readable(socket, messages)
messages.each {|msg| process *msg.copy_out_string.split(' ') }
end

def process(channel, metric_in_json)
metric = JSON.parse(metric_in_json)

if metric['value'] > @max
@max = metric['value']
p [:NEW_MAXIMUM, metric]
end
end
end

ctx = EM::ZeroMQ::Context.new(1)
EM::run do
socket = ctx.socket ZMQ::SUB, MaxValueTracker.new
socket.identity = "tracker-#{Process.pid}"
socket.connect "tcp://127.0.0.1:8890"
socket.subscribe 'metric'
end

0 comments on commit 4148694

Please sign in to comment.