Skip to content

Commit

Permalink
be compatible with peek-mysql2
Browse files Browse the repository at this point in the history
don't use alias_method since peek-mysql2 uses prepend on rails >= 5
related to MiniProfiler#437 and MiniProfiler#444
  • Loading branch information
glaszig committed Jan 23, 2021
1 parent c774215 commit ec110ab
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions lib/patches/db/mysql2.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
# frozen_string_literal: true

# The best kind of instrumentation is in the actual db provider, however we don't want to double instrument

class Mysql2::Result
alias_method :each_without_profiling, :each
def each(*args, &blk)
return each_without_profiling(*args, &blk) unless defined?(@miniprofiler_sql_id)
module MiniProfiler
def each(*args, &blk)
return super unless defined?(@miniprofiler_sql_id)

start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = each_without_profiling(*args, &blk)
elapsed_time = SqlPatches.elapsed_time(start)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = super
elapsed_time = SqlPatches.elapsed_time(start)

@miniprofiler_sql_id.report_reader_duration(elapsed_time)
result
@miniprofiler_sql_id.report_reader_duration(elapsed_time)
result
end
end

prepend MiniProfiler
end

class Mysql2::Client
alias_method :query_without_profiling, :query
def query(*args, &blk)
return query_without_profiling(*args, &blk) unless SqlPatches.should_measure?
module MiniProfiler
def query(*args, &blk)
return super unless SqlPatches.should_measure?

result, record = SqlPatches.record_sql(args[0]) do
query_without_profiling(*args, &blk)
result, record = SqlPatches.record_sql(args[0]) do
super
end
result.instance_variable_set("@miniprofiler_sql_id", record) if result
result
end
result.instance_variable_set("@miniprofiler_sql_id", record) if result
result
end

prepend MiniProfiler
end

0 comments on commit ec110ab

Please sign in to comment.