Class: MetaRequest::AppNotifications

Inherits:
Object
  • Object
show all
Defined in:
lib/meta_request/app_notifications.rb

Constant Summary collapse

CACHE_KEY_COLUMNS =

these are the specific keys in the cache payload that we display in the panel view

[:key, :hit, :options, :type]
CACHE_BLOCK =

define this here so we can pass it in to all of our cache subscribe calls

Proc.new {|*args|
  name, start, ending, transaction_id, payload = args

  # from http://edgeguides.rubyonrails.org/active_support_instrumentation.html#cache-fetch-hit-active-support
  #
  # :super_operation	:fetch is added when a read is used with #fetch
  #
  # so if :super_operation is present, we'll use it for the type. otherwise
  # strip (say) 'cache_delete.active_support' down to 'delete'
  payload[:type] = payload.delete(:super_operation) || name.sub(/cache_(.*?)\..*$/, '\1')

  # anything that isn't in CACHE_KEY_COLUMNS gets shoved into :options
  # instead
  payload[:options] = {}
  payload.keys.each do |k|
    payload[:options][k] = payload.delete(k) unless k.in? CACHE_KEY_COLUMNS
  end

  dev_caller = caller.detect { |c| c.include? MetaRequest.rails_root }
  if dev_caller
    c = Callsite.parse(dev_caller)
    payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
  end

  Event.new(name, start, ending, transaction_id, payload)
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.subscribeObject

Subscribe to all events relevant to RailsPanel



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/meta_request/app_notifications.rb', line 37

def self.subscribe
  new.
    subscribe("meta_request.log").
    subscribe("sql.active_record") do |*args|
      name, start, ending, transaction_id, payload = args
      dev_caller = caller.detect { |c| c.include? MetaRequest.rails_root }
      if dev_caller
        c = Callsite.parse(dev_caller)
        payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
      end
      Event.new(name, start, ending, transaction_id, payload)
    end.
    subscribe("render_partial.action_view").
    subscribe("render_template.action_view").
    subscribe("process_action.action_controller.exception").
    subscribe("process_action.action_controller") do |*args|
      name, start, ending, transaction_id, payload = args
      payload[:format] ||= (payload[:formats]||[]).first # Rails 3.0.x Support
      payload[:status] = '500' if payload[:exception]
      Event.new(name, start, ending, transaction_id, payload)
    end.
    subscribe("cache_read.active_support", &CACHE_BLOCK).
    subscribe("cache_generate.active_support", &CACHE_BLOCK).
    subscribe("cache_fetch_hit.active_support", &CACHE_BLOCK).
    subscribe("cache_write.active_support", &CACHE_BLOCK).
    subscribe("cache_delete.active_support", &CACHE_BLOCK).
    subscribe("cache_exist?.active_support", &CACHE_BLOCK)
end

Instance Method Details

#subscribe(event_name) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/meta_request/app_notifications.rb', line 66

def subscribe(event_name)
  ActiveSupport::Notifications.subscribe(event_name) do |*args|
    event = block_given?? yield(*args) : Event.new(*args)
    AppRequest.current.events << event if AppRequest.current
  end
  self
end