Class: Rollbar::Middleware::Rails::RollbarMiddleware

Inherits:
Object
  • Object
show all
Includes:
ExceptionReporter, RequestDataExtractor
Defined in:
lib/rollbar/middleware/rails/rollbar.rb

Constant Summary

Constants included from RequestDataExtractor

RequestDataExtractor::ALLOWED_BODY_PARSEABLE_METHODS, RequestDataExtractor::ALLOWED_HEADERS_REGEX

Instance Method Summary collapse

Methods included from ExceptionReporter

#capture_uncaught?, #exception_data, #log_exception_message, #report_exception_to_rollbar

Methods included from RequestDataExtractor

#extract_person_data_from_controller, #extract_request_data_from_rack, #scrub_params, #scrub_url

Constructor Details

#initialize(app) ⇒ RollbarMiddleware

Returns a new instance of RollbarMiddleware.



11
12
13
# File 'lib/rollbar/middleware/rails/rollbar.rb', line 11

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rollbar/middleware/rails/rollbar.rb', line 15

def call(env)
  self.request_data = nil

  Rollbar.reset_notifier!

  env['rollbar.scope'] = scope = fetch_scope(env)

  Rollbar.scoped(scope) do
    begin
      Rollbar.notifier.enable_locals
      response = @app.call(env)

      if (framework_exception = env['action_dispatch.exception'])
        report_exception_to_rollbar(env, framework_exception)
      end

      response
    rescue Exception => e # rubocop:disable Lint/RescueException
      report_exception_to_rollbar(env, e)
      raise
    ensure
      Rollbar.notifier.disable_locals
    end
  end
end

#context(request_data) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/rollbar/middleware/rails/rollbar.rb', line 79

def context(request_data)
  route_params = request_data[:params]

  # make sure route is a hash built by RequestDataExtractor
  return unless route_params.is_a?(Hash) && !route_params.empty?

  "#{route_params[:controller]}##{route_params[:action]}"
end

#extract_request_data(env) ⇒ Object



60
61
62
# File 'lib/rollbar/middleware/rails/rollbar.rb', line 60

def extract_request_data(env)
  extract_request_data_from_rack(env)
end

#fetch_scope(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/rollbar/middleware/rails/rollbar.rb', line 41

def fetch_scope(env)
  # Scope a new notifier with request data and a Proc for person data
  # for any reports that happen while a controller is handling a request

  {
    :request => proc { request_data(env) },
    :person => person_data_proc(env),
    :context => proc { context(request_data(env)) }
  }
end

#person_data_proc(env) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rollbar/middleware/rails/rollbar.rb', line 64

def person_data_proc(env)
  block = proc { extract_person_data_from_controller(env) }
  unless defined?(ActiveRecord::Base) && ActiveRecord::Base.connected?
    return block
  end

  proc do
    begin
      ActiveRecord::Base.connection_pool.with_connection(&block)
    rescue ActiveRecord::ConnectionTimeoutError
      {}
    end
  end
end

#request_data(env) ⇒ Object



52
53
54
# File 'lib/rollbar/middleware/rails/rollbar.rb', line 52

def request_data(env)
  Thread.current[:'_rollbar.rails.request_data'] ||= extract_request_data(env)
end

#request_data=(value) ⇒ Object



56
57
58
# File 'lib/rollbar/middleware/rails/rollbar.rb', line 56

def request_data=(value)
  Thread.current[:'_rollbar.rails.request_data'] = value
end