Class: Tuttle::Instrumenter

Inherits:
Object
  • Object
show all
Defined in:
lib/tuttle/instrumenter.rb

Constant Summary collapse

@@events =
[]
@@event_counts =
Hash.new(0)
@@cache_events =
[]

Class Method Summary collapse

Class Method Details

.initialize_tuttle_instrumenterObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tuttle/instrumenter.rb', line 9

def self.initialize_tuttle_instrumenter
  # For now, only instrument non-production mode
  unless Rails.env.production?
    ActiveSupport::Notifications.subscribe(/.*/) do |*args|
      event = ActiveSupport::Notifications::Event.new(*args)
      Tuttle::Instrumenter.events << event
      Tuttle::Instrumenter.event_counts[event.name] += 1
    end
  end

  # Note: For Rails < 4.2 instrumentation is not enabled by default. Hitting the cache inspector page will enable it for that session.
  Tuttle::Engine.logger.info('Initializing cache_read subscriber')
  ActiveSupport::Notifications.subscribe('cache_read.active_support') do |*args|
    cache_call_location = caller_locations.detect { |cl| cl.path.start_with?("#{Rails.root}/app".freeze) }
    event = ActiveSupport::Notifications::Event.new(*args)

    Tuttle::Engine.logger.info("Cache Read called: #{cache_call_location.path} on line #{cache_call_location.lineno} :: #{event.payload.inspect}")

    event.payload.merge!({:call_location_path => cache_call_location.path, :call_location_lineno => cache_call_location.lineno })
    Tuttle::Instrumenter.cache_events << event
  end

  ActiveSupport::Notifications.subscribe('cache_generate.active_support') do |*args|
    Tuttle::Engine.logger.info('Cache Generate called')
  end

end