Class: PerfCheck::Middleware
- Inherits:
-
Object
- Object
- PerfCheck::Middleware
- Defined in:
- lib/perf_check/middleware.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#query_count ⇒ Object
Returns the value of attribute query_count.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
-
#stacktrace_for(e) ⇒ Object
These files are used by the perf_check daemon app.
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
8 9 10 11 12 13 14 15 |
# File 'lib/perf_check/middleware.rb', line 8 def initialize(app) @app = app self.query_count = 0 ActiveSupport::Notifications.subscribe('sql.active_record') do |_| self.query_count += 1 end end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
5 6 7 |
# File 'lib/perf_check/middleware.rb', line 5 def app @app end |
#query_count ⇒ Object
Returns the value of attribute query_count.
6 7 8 |
# File 'lib/perf_check/middleware.rb', line 6 def query_count @query_count end |
Instance Method Details
#call(env) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/perf_check/middleware.rb', line 17 def call(env) self.query_count = 0 begin status, headers, body = app.call(env) rescue => error status, headers, body = 500, { "X-PerfCheck-StackTrace" => stacktrace_for(error) }, [''] end headers['X-PerfCheck-Query-Count'] = query_count.to_s [status, headers, body] end |
#stacktrace_for(e) ⇒ Object
These files are used by the perf_check daemon app
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/perf_check/middleware.rb', line 30 def stacktrace_for(e) trace_file = "#{Rails.root}/tmp/perf_check_traces" << "/trace-#{SecureRandom.hex(16)}.txt" FileUtils.mkdir_p(File.dirname(trace_file)) File.open(trace_file, 'w') do |f| f.puts("#{e.class}: #{e.message}") f.write(e.backtrace.join("\n")) end trace_file end |