Class: NewRelic::Agent::EventListener

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/event_listener.rb

Overview

Basic mechanism for the agent instance to provide agent-wide eventing. It is intended to keep different pieces of the app decoupled from each other.

While an EventListener could be used elsewhere, it’s strongly expected your eventing needs should be met by the agent’s instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventListener

Returns a new instance of EventListener.



11
12
13
14
# File 'lib/new_relic/agent/event_listener.rb', line 11

def initialize
  @events = {}
  @runaway_threshold = 100
end

Instance Attribute Details

#runaway_thresholdObject

Returns the value of attribute runaway_threshold.



9
10
11
# File 'lib/new_relic/agent/event_listener.rb', line 9

def runaway_threshold
  @runaway_threshold
end

Instance Method Details

#check_for_runaway_subscriptions(event) ⇒ Object



22
23
24
25
# File 'lib/new_relic/agent/event_listener.rb', line 22

def check_for_runaway_subscriptions(event)
  count = @events[event].size
  NewRelic::Agent.logger.debug("Run-away event subscription on #{event}? Subscribed #{count}") if count > @runaway_threshold
end

#clearObject



27
28
29
# File 'lib/new_relic/agent/event_listener.rb', line 27

def clear
  @events.clear
end

#notify(event, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/new_relic/agent/event_listener.rb', line 31

def notify(event, *args)
  return unless @events.has_key?(event)

  @events[event].each do |handler|
    begin
      handler.call(*args)
    rescue => err
      NewRelic::Agent.logger.debug("Failure during notify for #{@event}", err)
    end
  end
end

#subscribe(event, &handler) ⇒ Object



16
17
18
19
20
# File 'lib/new_relic/agent/event_listener.rb', line 16

def subscribe(event, &handler)
  @events[event] ||= []
  @events[event] << handler
  check_for_runaway_subscriptions(event)
end