class Tracebin::Subscribers

Subscribes to certain events, handels them, and passes event data to the Recorder class. The general workflow goes like this:

  1. Patch the method you want to profile. It should generate an array that

looks like the following:

[
  "event_type.event_domain",
  start_time,
  stop_time,
  etc...,
  { event: :data }
]

Note that the event hash must be the last element in the array (this is to maintain consistency with ActiveSupport::Notifications).

  1. Store that event array into an appropriate Event subclass.

  2. Add each Event object to +@events_data+ using the +#<<+ method.

Public Class Methods

new() click to toggle source
# File lib/tracebin/subscribers.rb, line 29
def initialize
  @events_data = Recorder
  collect_events_data
end

Private Instance Methods

active_job_hook() click to toggle source
# File lib/tracebin/subscribers.rb, line 102
def active_job_hook
  ::Tracebin::BackgroundJobInstrumentation.install :active_job
end
background_job_hooks() click to toggle source
# File lib/tracebin/subscribers.rb, line 59
def background_job_hooks
  if defined? ::ActiveJob
    active_job_hook
  else
    sidekiq_hook
    resque_hook
  end
end
collect_events_data() click to toggle source
# File lib/tracebin/subscribers.rb, line 36
def collect_events_data
  if rails_app?
    rails_hooks
  else
    other_hooks
  end

  background_job_hooks
end
db_hooks() click to toggle source
# File lib/tracebin/subscribers.rb, line 68
def db_hooks
  postgres_hook
  mysql2_hook
end
mysql2_hook() click to toggle source
# File lib/tracebin/subscribers.rb, line 119
def mysql2_hook
  return unless defined? ::Mysql2
  ::Tracebin::Patches.patch_mysql2 do |event_data|
    event = SQLEvent.new event_data
    @events_data << event
  end
end
other_hooks() click to toggle source
# File lib/tracebin/subscribers.rb, line 54
def other_hooks
  sinatra_hook if sinatra_app?
  db_hooks
end
postgres_hook() click to toggle source

—————————===

DB Hooks

—————————===

# File lib/tracebin/subscribers.rb, line 110
def postgres_hook
  return unless defined? ::PG
  ::Tracebin::Patches.patch_postgres do |event_data|
    event = SQLEvent.new event_data
    @events_data << event
  end
end
process_action_hook() click to toggle source
# File lib/tracebin/subscribers.rb, line 81
def process_action_hook
  subscribe_asn 'process_action.action_controller', ControllerEvent
end
rails_app?() click to toggle source
# File lib/tracebin/subscribers.rb, line 163
def rails_app?
  defined? ::Rails
end
rails_hooks() click to toggle source
# File lib/tracebin/subscribers.rb, line 46
def rails_hooks
  sql_hook
  process_action_hook
  render_layout_hook
  render_template_hook
  render_partial_hook
end
render_layout_hook() click to toggle source
# File lib/tracebin/subscribers.rb, line 85
def render_layout_hook
  unless [ActionPack::VERSION::MAJOR, ActionPack::VERSION::MINOR] == [3, 0]
    ::Tracebin::Patches.patch_action_view_layout do |event_data|
      event = ViewEvent.new event_data
      @events_data << event
    end
  end
end
render_partial_hook() click to toggle source
# File lib/tracebin/subscribers.rb, line 98
def render_partial_hook
  subscribe_asn 'render_partial.action_view', ViewEvent
end
render_template_hook() click to toggle source
# File lib/tracebin/subscribers.rb, line 94
def render_template_hook
  subscribe_asn 'render_template.action_view', ViewEvent
end
resque_hook() click to toggle source
# File lib/tracebin/subscribers.rb, line 136
def resque_hook
  return unless defined? ::Resque
  ::Tracebin::BackgroundJobInstrumentation.install :resque
end
sidekiq_hook() click to toggle source

—————————===

Background Job Hooks

—————————===

# File lib/tracebin/subscribers.rb, line 131
def sidekiq_hook
  return unless defined? ::Sidekiq
  ::Tracebin::BackgroundJobInstrumentation.install :sidekiq
end
sinatra_app?() click to toggle source
# File lib/tracebin/subscribers.rb, line 167
def sinatra_app?
  defined? ::Sinatra
end
sinatra_hook() click to toggle source

—————————===

Sinatra Hooks

—————————===

# File lib/tracebin/subscribers.rb, line 145
def sinatra_hook
  ::Tracebin::Patches.patch_sinatra do |event_data|
    event = SinatraEvent.new event_data
    @events_data << event
  end
end
sql_hook() click to toggle source

—————————===

Rails Hooks

—————————===

# File lib/tracebin/subscribers.rb, line 77
def sql_hook
  subscribe_asn 'sql.active_record', SQLEvent
end
subscribe_asn(event_name, event_klass) click to toggle source

—————————===

Aux

—————————===

# File lib/tracebin/subscribers.rb, line 156
def subscribe_asn(event_name, event_klass)
  ActiveSupport::Notifications.subscribe event_name do |*args|
    event = event_klass.new args
    @events_data << event if event.valid?
  end
end