Class: ActiveRecordQueryCounter::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_query_counter/rack_middleware.rb

Overview

Rack middleware to count queries on a request.

Instance Method Summary collapse

Constructor Details

#initialize(app, thresholds: nil) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.

Parameters:

  • app (Object)

    The Rack application.

  • thresholds (Hash) (defaults to: nil)

    Options for the notification thresholds. Valid keys are:

    • ‘:query_time` - The minimum query time to send a notification for.

    • ‘:row_count` - The minimum row count to send a notification for.

    • ‘:transaction_time` - The minimum transaction time to send a notification for.

    • ‘:transaction_count` - The minimum transaction count to send a notification for.



12
13
14
15
# File 'lib/active_record_query_counter/rack_middleware.rb', line 12

def initialize(app, thresholds: nil)
  @app = app
  @thresholds = thresholds.dup.freeze if thresholds
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/active_record_query_counter/rack_middleware.rb', line 17

def call(env)
  ActiveRecordQueryCounter.count_queries do
    ActiveRecordQueryCounter.thresholds.set(@thresholds) if @thresholds

    @app.call(env)
  end
end