Sha256: b5d5d588afc47316b706f31e6ebd5140aef74b2c0079c60cc3d6630272fcd8de

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

require 'vizsla/helpers'

module Vizsla
  class Patches
    include ::Vizsla::Helpers

    class << self
      def patch_postgres
        if block_given?
          @postgres_event_hanlder = Proc.new(&block)
        end

        ::PG::Connection.class_eval do
          alias_method :exec_without_profiling, :exec

          def exec(*args, &blk)
            return exec_without_profiling(*args, &blk)

            start_time   = Time.now
            result       = exec_without_profiling(*args, &blk)
            end_time     = Time.now

            event_data = [
              'sql.postgres_exec',
              start_time,
              end_time,
              {
                sql: args[0]
              }
            ]

            ::Vizsla::Patches.handle_event :postgres, event_data

            result
          end
        end
      end

      def handle_event(handler_name, event_data)
        handler = self.get_instance_variable "@#{handler_name}_event_handler"
        hanlder.call event_data unless hanlder.nil?
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vizsla-0.0.5 lib/vizsla/patches.rb
vizsla-0.0.4 lib/vizsla/patches.rb
vizsla-0.0.3 lib/vizsla/patches.rb
vizsla-0.0.2 lib/vizsla/patches.rb