module ActiveRecordStats::ResquePlugin

Public Instance Methods

around_perform_active_record_stats(*args) { || ... } click to toggle source
# File lib/active_record_stats/resque_plugin.rb, line 5
def around_perform_active_record_stats(*args, &block)
  totals = {}

  gather_sql = ->(_name, _started_at, _finished_at, _unique_id, payload) {
    return if payload[:name] == 'SCHEMA' || payload[:sql].blank?
    return unless type = ActiveRecordStats.statement_type(payload[:sql])
    totals[type] ||= 0
    totals[type] += 1
  }

  sub = ActiveSupport::Notifications.subscribe('sql.active_record', &gather_sql)
  yield

ensure
  ActiveSupport::Notifications.unsubscribe(sub)
  emit_active_record_stats(name, totals.dup)
end

Private Instance Methods

emit_active_record_stats(name, totals) click to toggle source
# File lib/active_record_stats/resque_plugin.rb, line 25
def emit_active_record_stats(name, totals)
  job = name.underscore.gsub('/', '__')
  totals.each do |verb, count|
    StatsD.gauge "db.job.#{job}.#{verb}", count
  end
end