Sha256: f5cc96a8ffcbd70ab11e7eca86095c51cfde387d94308f8244eec95461283c9c

Contents?: true

Size: 1.61 KB

Versions: 21

Compression:

Stored size: 1.61 KB

Contents

require 'active_record/log_subscriber'

module ActiveReplicas
  class LogSubscriber < ActiveRecord::LogSubscriber
    def sql(event)
      self.class.runtime += event.duration
      return unless logger.debug?

      payload = event.payload

      return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])

      pool  = nil
      name  = "#{payload[:name]} (#{event.duration.round(1)}ms)"
      sql   = payload[:sql]
      binds = nil

      proxy = ActiveRecord::Base.connection_handler.proxying_connection_pool
      connection_pool = proxy.pool_which_owns_connection payload[:connection_id]
      if connection_pool
        role =
          if proxy.primary_pool? connection_pool
            'primary'
          elsif proxy.replica_pool? connection_pool
            pool_name = proxy.replica_pools.key connection_pool
            "replica=#{pool_name}"
          else
            'unknown'
          end

        pool = "[#{role}] "
      end

      unless (payload[:binds] || []).empty?
        binds = ' ' + payload[:binds].map { |column, value| render_bind(column, value) }.inspect
      end

      debug "#{pool}#{name} #{sql}#{binds}"
    end

    def logger
      ActiveRecord::Base.logger
    end

    # Take over logging duties from `ActiveRecord::LogSubscriber`.
    def self.hijack_active_record
      self.attach_to :active_record

      subscriber = ActiveSupport::Notifications.notifier.listeners_for('sql.active_record').find do |subscriber|
        ActiveRecord::LogSubscriber === subscriber.instance_eval { @delegate }
      end

      ActiveSupport::Notifications.notifier.unsubscribe(subscriber) if subscriber
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
active_replicas-0.5.0 lib/active_replicas/log_subscriber.rb
active_replicas-0.4.0 lib/active_replicas/log_subscriber.rb
active_replicas-0.3.2 lib/active_replicas/log_subscriber.rb
active_replicas-0.3.1 lib/active_replicas/log_subscriber.rb
active_replicas-0.3.0 lib/active_replicas/log_subscriber.rb
active_replicas-0.2.9 lib/active_replicas/log_subscriber.rb
active_replicas-0.2.8 lib/active_replicas/log_subscriber.rb
active_replicas-0.2.7 lib/active_replicas/log_subscriber.rb
active_replicas-0.2.6 lib/active_replicas/log_subscriber.rb
active_replicas-0.2.3 lib/active_replicas/log_subscriber.rb
active_replicas-0.2.2 lib/active_replicas/log_subscriber.rb
active_replicas-0.2.1 lib/active_replicas/log_subscriber.rb
active_replicas-0.2.0 lib/active_replicas/log_subscriber.rb
active_replicas-0.1.7 lib/active_replicas/log_subscriber.rb
active_replicas-0.1.6 lib/active_replicas/log_subscriber.rb
active_replicas-0.1.5 lib/active_replicas/log_subscriber.rb
active_replicas-0.1.4 lib/active_replicas/log_subscriber.rb
active_replicas-0.1.3 lib/active_replicas/log_subscriber.rb
active_replicas-0.1.2 lib/active_replicas/log_subscriber.rb
active_replicas-0.1.1 lib/active_replicas/log_subscriber.rb