Last active
February 8, 2016 09:51
-
-
Save dtaniwaki/d1dfcd624136432ee2f2 to your computer and use it in GitHub Desktop.
ActiveRecordでSlowQueryを通知する方法 ref: http://qiita.com/dtaniwaki/items/2acf9e7f54eec05d48f3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Detect slow query slower than 1000 msec | |
Ext::ActiveRecord::SlowQuerySubscriber.load!(1000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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