Skip to content

Commit

Permalink
Prepopulate Hash of statement types.
Browse files Browse the repository at this point in the history
This should hopefully solve an error we've been seeing in concurrent systems
that results in trying to insert into a Hash during iteration.
  • Loading branch information
packrat386 committed May 20, 2016
1 parent 27003fc commit 2e8f7f6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/active_record_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/active_record_stats/rack_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record_stats/resque_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
4 changes: 2 additions & 2 deletions lib/active_record_stats/sidekiq_server_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 2e8f7f6

Please sign in to comment.