Class: RailsPerformance::Rails::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_performance/rails/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



33
34
35
# File 'lib/rails_performance/rails/middleware.rb', line 33

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



37
38
39
# File 'lib/rails_performance/rails/middleware.rb', line 37

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails_performance/rails/middleware.rb', line 41

def call!(env)
  @status, @headers, @response = @app.call(env)

  #t = Time.current
  if !RailsPerformance.skip
    if !CurrentRequest.current.ignore.include?(:performance) # grape is executed first, and than ignore regular future storage of "controller"-like request
      if data = CurrentRequest.current.data
        record = RailsPerformance::Models::RequestRecord.new(**data.merge({request_id: CurrentRequest.current.request_id}))

        # for 500 errors
        record.status ||= @status

        # capture referer from where this page was opened
        record.http_referer = env["HTTP_REFERER"] if record.status == 404

        # we can add custom data, for example Http User-Agent
        # or even devise current_user
        if RailsPerformance.custom_data_proc
          # just to be sure it won't break format how we store in redis
          record.custom_data = RailsPerformance.custom_data_proc.call(env)
        end

        # store for section "recent requests"
        # store request information (regular rails request)
        record.save
      end
    end
  end
  #puts "==> store performance data: #{(Time.current - t).round(3)}ms"

  [@status, @headers, @response]
end