Class: ActiveRecordQueryCounter::SidekiqMiddleware

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::ServerMiddleware
Defined in:
lib/active_record_query_counter/sidekiq_middleware.rb

Overview

Sidekiq middleware to count queries on a job.

Notification thresholds can be set per worker with the ‘active_record_query_counter.thresholds` key in the `sidekiq_options` hash. 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.

Thresholds can be disabled for a worker by setting ‘active_record_query_counter.thresholds` to `false`.

Examples:


class MyWorker
  include Sidekiq::Worker

  sidekiq_options active_record_query_counter: {thresholds: {query_time: 1.5}}

  def perform
    # ...
  end
end

Instance Method Summary collapse

Instance Method Details

#call(job_instance, job_payload, queue) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_record_query_counter/sidekiq_middleware.rb', line 31

def call(job_instance, job_payload, queue)
  ActiveRecordQueryCounter.count_queries do
    thresholds = job_payload.dig("active_record_query_counter", "thresholds")
    if thresholds.is_a?(Hash)
      ActiveRecordQueryCounter.thresholds.set(thresholds)
    elsif thresholds == false
      ActiveRecordQueryCounter.thresholds.clear
    end

    yield
  end
end