Sha256: d0d303b0c4ca9d2dcaa17437de1a69ae1b62bbfd59286c3c888f3dbd015f83b0

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

require 'rollbar/request_data_extractor'
require 'rollbar/exception_reporter'

module Rollbar
  module Middleware
    module Rails
      class RollbarMiddleware
        include RequestDataExtractor
        include ExceptionReporter

        def initialize(app)
          @app = app
        end

        def call(env)
          Rollbar.reset_notifier!

          Rollbar.scoped(fetch_scope(env)) do
            begin
              response = @app.call(env)

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

              response
            rescue Exception => exception
              report_exception_to_rollbar(env, exception)
              raise
            end
          end
        end

        def fetch_scope(env)
          request_data = extract_request_data_from_rack(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 => request_data,
            :person => person_data_proc(env),
            :context => context(request_data)
          }
        end

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

          proc { ActiveRecord::Base.connection_pool.with_connection(&block) }
        end

        def context(request_data)
          return unless request_data[:route]

          route = request_data[:route]
          # make sure route is a hash built by RequestDataExtractor
          return "#{route[:controller]}" + '#' + "#{route[:action]}" if route.is_a?(Hash) && !route.empty?
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rollbar-1.2.8 lib/rollbar/middleware/rails/rollbar.rb
rollbar-1.2.7 lib/rollbar/middleware/rails/rollbar.rb
rollbar-1.2.6 lib/rollbar/middleware/rails/rollbar.rb
rollbar-1.2.5 lib/rollbar/middleware/rails/rollbar.rb
rollbar-1.2.4 lib/rollbar/middleware/rails/rollbar.rb