Class: Instrumental::PubNub::Relay
- Inherits:
-
Object
- Object
- Instrumental::PubNub::Relay
- Defined in:
- lib/instrumental/pub_nub/relay.rb
Instance Method Summary collapse
- #disconnect! ⇒ Object
- #gauge_array(name, val) ⇒ Object
- #gauge_metric(name, value) ⇒ Object
-
#initialize(pubnub_subscribe_key, pubnub_channel_name, instrumental_api_key, key_prefix, logger) ⇒ Relay
constructor
A new instance of Relay.
- #on_connect(msg) ⇒ Object
- #on_error(msg) ⇒ Object
- #on_leave(msg) ⇒ Object
- #on_message(msg) ⇒ Object
- #run! ⇒ Object
Constructor Details
#initialize(pubnub_subscribe_key, pubnub_channel_name, instrumental_api_key, key_prefix, logger) ⇒ Relay
Returns a new instance of Relay.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/instrumental/pub_nub/relay.rb', line 8 def initialize(pubnub_subscribe_key, pubnub_channel_name, instrumental_api_key, key_prefix, logger) @logger = logger @agent = Instrumental::Agent.new(instrumental_api_key) @pubnub = Pubnub.new(:subscribe_key => pubnub_subscribe_key, :error_callback => method(:on_error), :connect_callback => method(:on_connect)) @channel = pubnub_channel_name @prefix = key_prefix @disconnecting = false end |
Instance Method Details
#disconnect! ⇒ Object
24 25 26 27 |
# File 'lib/instrumental/pub_nub/relay.rb', line 24 def disconnect! @disconnecting = true @pubnub.leave(:channel => @channel, :callback => method(:on_leave)) end |
#gauge_array(name, val) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/instrumental/pub_nub/relay.rb', line 53 def gauge_array(name, val) if val.size != 1 val.map.with_index do |measurement, i| gauge_metric([name, i].join("."), measurement) end else gauge_metric(name, val.first) end end |
#gauge_metric(name, value) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/instrumental/pub_nub/relay.rb', line 63 def gauge_metric(name, value) case value when Numeric debug "Gauge metric %s with value %s" % [name.inspect, value.inspect] @agent.gauge(name, value) name else warn "Not measuring metric %s, not a numeric value (%s)" % [name.inspect, value.inspect] nil end end |
#on_connect(msg) ⇒ Object
82 83 84 |
# File 'lib/instrumental/pub_nub/relay.rb', line 82 def on_connect(msg) info "Connected" end |
#on_error(msg) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/instrumental/pub_nub/relay.rb', line 75 def on_error(msg) error "%s (%s)" % [msg., msg.response.inspect] if @disconnecting shutdown end end |
#on_leave(msg) ⇒ Object
29 30 31 |
# File 'lib/instrumental/pub_nub/relay.rb', line 29 def on_leave(msg) shutdown end |
#on_message(msg) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/instrumental/pub_nub/relay.rb', line 33 def (msg) data = msg. submitted_metrics = case data when Hash data.flat_map do |k, v| gauge_array([@prefix, k].join("."), Array(v)) end when Array gauge_array(@prefix, data) else if data.to_s.size > 0 gauge_metric(@prefix, data.to_s) end end submitted_metrics = Array(submitted_metrics).compact if submitted_metrics.size > 0 info "Sent #{submitted_metrics.size} metrics" end end |
#run! ⇒ Object
19 20 21 22 |
# File 'lib/instrumental/pub_nub/relay.rb', line 19 def run! @pubnub.subscribe(:channel => @channel, :callback => method(:on_message)) end |