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',
 'endpoint_run.grape',
 'endpoint_render.grape',
 'endpoint_run_filters.grape',
 'rack-profiler.total_time',
 'rack-profiler.step'
]
VERSION =
"1.1.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ Profiler

Returns a new instance of Profiler.



33
34
35
36
37
38
39
40
# File 'lib/rack/profiler.rb', line 33

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.



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

def authorizator
  @authorizator
end

#backtrace_filterObject (readonly)

Returns the value of attribute backtrace_filter.



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

def backtrace_filter
  @backtrace_filter
end

#dashboard_pathObject

Returns the value of attribute dashboard_path.



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

def dashboard_path
  @dashboard_path
end

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



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

def subscriptions
  @subscriptions
end

Class Method Details

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



27
28
29
30
31
# File 'lib/rack/profiler.rb', line 27

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



79
80
81
# File 'lib/rack/profiler.rb', line 79

def authorize(&block)
  @authorizator = block
end

#call(env) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rack/profiler.rb', line 42

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



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

def filter_backtrace(&block)
  @backtrace_filter = block
end

#subscribe(*events) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rack/profiler.rb', line 56

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