Sha256: 85d8058f0b12828c1913a3f31e2884070d17ee819dd1922f697ee03965a26bcc

Contents?: true

Size: 876 Bytes

Versions: 2

Compression:

Stored size: 876 Bytes

Contents

require 'active_record_stats'

module ActiveRecordStats
  module ResquePlugin
    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
      emit_active_record_stats(name, totals)
      ActiveSupport::Notifications.unsubscribe(sub)
    end

    private

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_record_stats-0.1.1 lib/active_record_stats/resque_plugin.rb
active_record_stats-0.1.0 lib/active_record_stats/resque_plugin.rb