Skip to content

Commit

Permalink
use alternative sql subscription method
Browse files Browse the repository at this point in the history
- fixes tchak#1
  • Loading branch information
mattyr committed Mar 30, 2017
1 parent 3daaa11 commit 69ef5a1
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/grape/middleware/lograge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,30 @@ def initialize(_, options = {})
@options[:filter] ||= self.class.filter
end

class SQLSubscriber
attr_reader :db_duration

def initialize
@db_duration = 0
end

def start(name, id, payload)
@start = Time.now
end

def finish(name, id, payload)
@db_duration += 1000.0 * (Time.now - @start)
end
end

def before
super

@db_duration = 0
@sql_subscriber = SQLSubscriber.new

@db_subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
@db_duration += event.duration
end if defined?(ActiveRecord)
if defined?(ActiveRecord)
@db_subscription = ActiveSupport::Notifications.subscribe('sql.active_record', @sql_subscriber)
end

ActiveSupport::Notifications.instrument("start_processing.grape", raw_payload)
end
Expand Down Expand Up @@ -66,7 +81,7 @@ def after(payload, status)
payload[:status] = status
payload[:format] = env['api.format']
payload[:version] = env['api.version']
payload[:db_runtime] = @db_duration
payload[:db_runtime] = @sql_subscriber.try(:db_duration)
end

def after_exception(payload, e)
Expand Down

0 comments on commit 69ef5a1

Please sign in to comment.