Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Created January 31, 2014 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcoglan/8733251 to your computer and use it in GitHub Desktop.
Save jcoglan/8733251 to your computer and use it in GitHub Desktop.
require 'faye'
require 'logger'
Faye::WebSocket.load_adapter('thin')
faye = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
#Define hash to store metrics each time a monitoring event occurs in the server
event_metrics = {
:connections => {count: 0, latest_timestamp: nil},
:active_subscriptions => {count: 0, latest_timestamp: nil},
:inactive_subscriptions => {count: 0, latest_timestamp: nil},
:published_messages => {count: 0, latest_timestamp: nil},
:disconnections => {count: 0, latest_timestamp: nil}
}
c_metrics = {
:connections => 0,
:active_subscriptions => 0,
:inactive_subscriptions => 0,
:published_messages => 0,
:disconnections => 0
}
#Define hash to store metrics each time a monitoring event occurs on a channel in the server
channel_metrics = Hash.new { |k,v| k[v] = c_metrics.dup }
faye.bind(:handshake) do |client_id|
event_timestamp = Time.now
event_metrics[:connections][:count] += 1
event_metrics[:connections][:latest_timestamp] = event_timestamp
latest_metrics = {:event_metrics => event_metrics, :channel_metrics => channel_metrics}
faye.get_client.publish('/metrics',latest_metrics)
end
faye.bind(:subscribe) do |client_id, channel|
event_timestamp = Time.now
event_metrics[:active_subscriptions][:count] += 1
event_metrics[:active_subscriptions][:latest_timestamp] = event_timestamp
unless channel.eql?('/metrics')
channel_metrics[channel][:active_subscriptions] += 1
channel_metrics[channel][:latest_timestamp] = event_timestamp
end
latest_metrics = {:event_metrics => event_metrics, :channel_metrics => channel_metrics}
faye.get_client.publish('/metrics',latest_metrics)
end
faye.bind(:unsubscribe) do |client_id, channel|
event_timestamp = Time.now
event_metrics[:inactive_subscriptions][:count] += 1
event_metrics[:inactive_subscriptions][:latest_timestamp] = event_timestamp
event_metrics[:active_subscriptions][:count] -= 1
event_metrics[:active_subscriptions][:latest_timestamp] = event_timestamp
unless channel.eql?('/metrics')
channel_metrics[channel][:active_subscriptions] -= 1
channel_metrics[channel][:latest_timestamp] = event_timestamp
end
latest_metrics = {:event_metrics => event_metrics, :channel_metrics => channel_metrics}
faye.get_client.publish('/metrics',latest_metrics)
end
faye.bind(:publish) do |client_id, channel, data|
event_timestamp = Time.now
event_metrics[:published_messages][:count] += 1
event_metrics[:published_messages][:latest_timestamp] = event_timestamp
unless channel.eql?('/metrics')
channel_metrics[channel][:published_messages] += 1
channel_metrics[channel][:latest_timestamp] = event_timestamp
latest_metrics = {:event_metrics => event_metrics, :channel_metrics => channel_metrics}
faye.get_client.publish('/metrics',latest_metrics)
else
# Do nothing; no need to publish unchanged latest_metrics to /metrics channel
end
end
faye.bind(:disconnect) do |client_id|
event_timestamp = Time.now
event_metrics[:disconnections][:count] += 1
event_metrics[:disconnections][:latest_timestamp] = event_timestamp
latest_metrics = {:event_metrics => event_metrics, :channel_metrics => channel_metrics}
faye.get_client.publish('/metrics',latest_metrics)
end
run faye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment