Sha256: 40c75153403161335ea12b22ba030674db228afc7b203de4d2e1fbf0d9783424

Contents?: true

Size: 880 Bytes

Versions: 4

Compression:

Stored size: 880 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
      ActiveSupport::Notifications.unsubscribe(sub)
      emit_active_record_stats(name, totals.dup)
    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

4 entries across 4 versions & 1 rubygems

Version Path
active_record_stats-0.1.6 lib/active_record_stats/resque_plugin.rb
active_record_stats-0.1.5 lib/active_record_stats/resque_plugin.rb
active_record_stats-0.1.4 lib/active_record_stats/resque_plugin.rb
active_record_stats-0.1.3 lib/active_record_stats/resque_plugin.rb