diff --git a/lib/active_record_stats.rb b/lib/active_record_stats.rb index c2524b1..6c7b6f3 100644 --- a/lib/active_record_stats.rb +++ b/lib/active_record_stats.rb @@ -3,6 +3,9 @@ require 'statsd-instrument' module ActiveRecordStats + STATEMENT_KEYS = %w[BEGIN COMMIT DELETE EXPLAIN INSERT + RELEASE ROLLBACK SAVEPOINT SELECT UPDATE WITH].freeze + def self.statement_type(sql) return if sql.nil? @@ -12,4 +15,10 @@ def self.statement_type(sql) type = cleaned.split(' ', 2).first type.try(:upcase) end + + def self.statement_hash + hash = {} + STATEMENT_KEYS.each { |k| hash[k] = 0 } + hash + end end diff --git a/lib/active_record_stats/rack_middleware.rb b/lib/active_record_stats/rack_middleware.rb index d05e115..ea26203 100644 --- a/lib/active_record_stats/rack_middleware.rb +++ b/lib/active_record_stats/rack_middleware.rb @@ -14,7 +14,7 @@ def initialize(app) end def call(env) - totals = {} + totals = ActiveRecordStats.statement_hash db_time = 0 gather_sql = ->(_name, _started_at, _finished_at, _unique_id, payload) { diff --git a/lib/active_record_stats/resque_plugin.rb b/lib/active_record_stats/resque_plugin.rb index 0978b8b..78a4ea6 100644 --- a/lib/active_record_stats/resque_plugin.rb +++ b/lib/active_record_stats/resque_plugin.rb @@ -3,7 +3,7 @@ module ActiveRecordStats module ResquePlugin def around_perform_active_record_stats(*args, &block) - totals = {} + totals = ActiveRecordStats.statement_hash gather_sql = ->(_name, _started_at, _finished_at, _unique_id, payload) { return if payload[:name] == 'SCHEMA' || payload[:sql].blank? diff --git a/lib/active_record_stats/sidekiq_server_middleware.rb b/lib/active_record_stats/sidekiq_server_middleware.rb index 03314af..aecb26c 100644 --- a/lib/active_record_stats/sidekiq_server_middleware.rb +++ b/lib/active_record_stats/sidekiq_server_middleware.rb @@ -3,8 +3,8 @@ module ActiveRecordStats class SidekiqServerMiddleware def call(worker, job, queue) - totals = {} - + totals = ActiveRecordStats.statement_hash + 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])