Class: Rack::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/profiler.rb,
lib/rack/profiler/version.rb

Defined Under Namespace

Classes: DummyError

Constant Summary collapse

DEFAULT_SUBSCRIPTIONS =
['sql.active_record',
'render_template.action_view',
'render_partial.action_view',
'process_action.action_controller',
'rack-profiler.total_time',
'rack-profiler.step']
VERSION =
"1.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ Profiler

Returns a new instance of Profiler.



28
29
30
31
32
33
34
35
# File 'lib/rack/profiler.rb', line 28

def initialize(app, &block)
  @events         = []
  @subscriptions  = []
  @dashboard_path = '/rack-profiler'
  @app            = app
  subscribe_to_default
  block.call(self) if block_given?
end

Instance Attribute Details

#authorizatorObject (readonly)

Returns the value of attribute authorizator.



10
11
12
# File 'lib/rack/profiler.rb', line 10

def authorizator
  @authorizator
end

#backtrace_filterObject (readonly)

Returns the value of attribute backtrace_filter.



10
11
12
# File 'lib/rack/profiler.rb', line 10

def backtrace_filter
  @backtrace_filter
end

#dashboard_pathObject

Returns the value of attribute dashboard_path.



11
12
13
# File 'lib/rack/profiler.rb', line 11

def dashboard_path
  @dashboard_path
end

#eventsObject (readonly)

Returns the value of attribute events.



10
11
12
# File 'lib/rack/profiler.rb', line 10

def events
  @events
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



10
11
12
# File 'lib/rack/profiler.rb', line 10

def subscriptions
  @subscriptions
end

Class Method Details

.step(name, payload = {}) ⇒ Object



22
23
24
25
26
# File 'lib/rack/profiler.rb', line 22

def self.step(name, payload = {})
  ActiveSupport::Notifications.instrument('rack-profiler.step', payload.merge(step_name: name)) do
    yield
  end
end

Instance Method Details

#authorize(&block) ⇒ Object



74
75
76
# File 'lib/rack/profiler.rb', line 74

def authorize(&block)
  @authorizator = block
end

#call(env) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rack/profiler.rb', line 37

def call(env)
  @events = []
  req = Rack::Request.new(env)
  env['rack-profiler'] = self

  if req.path == dashboard_path
    render_dashboard
  elsif req.params.has_key?('rack-profiler')
    render_profiler_results(env)
  else
    @app.call(env)
  end
end

#filter_backtrace(&block) ⇒ Object



70
71
72
# File 'lib/rack/profiler.rb', line 70

def filter_backtrace(&block)
  @backtrace_filter = block
end

#subscribe(*events) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rack/profiler.rb', line 51

def subscribe(*events)
  events.each do |event_name|
    next if @subscriptions.include?(event_name)
    ActiveSupport::Notifications.subscribe(event_name) do |name, start, finish, id, payload|
      evt = {
        id:        id,
        name:      name,
        start:     start.to_f,
        finish:    finish.to_f,
        duration:  (finish - start) * 1000,
        payload:   payload,
        backtrace: filtered_backtrace(caller(1))
      }
      (@events ||= []) << evt
    end
    @subscriptions << event_name
  end
end