Class: NewRelic::Agent::Instrumentation::EventedSubscriber

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventedSubscriber

Returns a new instance of EventedSubscriber.



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

def initialize
  @queue_key = ['NewRelic', self.class.name, object_id].join('-')
end

Class Method Details

.subscribe(pattern) ⇒ Object



20
21
22
23
24
# File 'lib/new_relic/agent/instrumentation/evented_subscriber.rb', line 20

def self.subscribe(pattern)
  if !subscribed?
    ActiveSupport::Notifications.subscribe(pattern, new)
  end
end

.subscribed?Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
# File 'lib/new_relic/agent/instrumentation/evented_subscriber.rb', line 13

def self.subscribed?
  # TODO: need to talk to Rails core about an API for this,
  # rather than digging through Listener ivars
  ActiveSupport::Notifications.notifier.instance_variable_get(:@subscribers) \
    .find{|s| s.instance_variable_get(:@delegate).class == self }
end

Instance Method Details

#event_stackObject



58
59
60
# File 'lib/new_relic/agent/instrumentation/evented_subscriber.rb', line 58

def event_stack
  Thread.current[@queue_key] ||= Hash.new {|h,id| h[id] = [] }
end

#finish(name, id, payload) ⇒ Object



32
33
34
# File 'lib/new_relic/agent/instrumentation/evented_subscriber.rb', line 32

def finish(name, id, payload)
  pop_event(id)
end

#log_notification_error(error, name, event_type) ⇒ Object



36
37
38
39
40
41
# File 'lib/new_relic/agent/instrumentation/evented_subscriber.rb', line 36

def log_notification_error(error, name, event_type)
  # These are important enough failures that we want the backtraces
  # logged at error level, hence the explicit log_exception call.
  NewRelic::Agent.logger.error("Error during #{event_type} callback for event '#{name}':")
  NewRelic::Agent.logger.log_exception(:error, error)
end

#pop_event(transaction_id) ⇒ Object



52
53
54
55
56
# File 'lib/new_relic/agent/instrumentation/evented_subscriber.rb', line 52

def pop_event(transaction_id)
  event = event_stack[transaction_id].pop
  event.end = Time.now
  return event
end

#push_event(event) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/new_relic/agent/instrumentation/evented_subscriber.rb', line 43

def push_event(event)
  parent = event_stack[event.transaction_id].last
  if parent && event.respond_to?(:parent=)
    event.parent = parent
    parent << event
  end
  event_stack[event.transaction_id].push event
end

#start(name, id, payload) ⇒ Object



26
27
28
29
30
# File 'lib/new_relic/agent/instrumentation/evented_subscriber.rb', line 26

def start(name, id, payload)
  event = ActiveSupport::Notifications::Event.new(name, Time.now, nil, id, payload)
  push_event(event)
  return event
end