Skip to content

Instantly share code, notes, and snippets.

@dtaniwaki
Last active February 8, 2016 09:51
Show Gist options
  • Save dtaniwaki/d1dfcd624136432ee2f2 to your computer and use it in GitHub Desktop.
Save dtaniwaki/d1dfcd624136432ee2f2 to your computer and use it in GitHub Desktop.
ActiveRecordでSlowQueryを通知する方法 ref: http://qiita.com/dtaniwaki/items/2acf9e7f54eec05d48f3
# Detect slow query slower than 1000 msec
Ext::ActiveRecord::SlowQuerySubscriber.load!(1000)
module Ext::ActiveRecord
class SlowQuerySubscriber < ActiveSupport::Subscriber
class SlowQuery < StandardError
end
cattr_accessor :threshold
def self.load!(threshold)
self.threshold = threshold
attach_to :active_record
end
def sql(event)
if event.duration > self.threshold
e = SlowQuery.new("Found slow query #{event.payload[:sql].inspect} : #{event.duration} msec > #{self.threshold} msec")
e.set_backtrace caller
ActiveRecord::Base.logger.warn(e.message)
Bugsnag.notify_or_ignore(e, severity: "warning", grouping_hash: event.payload[:sql], payload: event.payload, duration: event.duration)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment