-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed: Inject :connection to event instead of using #_id2ref.
- Loading branch information
Showing
5 changed files
with
133 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
72 changes: 72 additions & 0 deletions
72
lib/ddtrace/contrib/active_record/patches/abstract_adapter.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'set' | ||
require 'ddtrace/augmentation/shim' | ||
|
||
module Datadog | ||
module Contrib | ||
module ActiveRecord | ||
# Defines basic behaviors for an ActiveRecord event. | ||
module Patches | ||
# Adds patch to AbstractAdapter to make it pass more information through | ||
# ActiveSupport notifications, for better instrumentation. | ||
module AbstractAdapter | ||
def self.included(base) | ||
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0') | ||
base.class_eval do | ||
alias_method :log_without_datadog, :log | ||
remove_method :log | ||
include InstanceMethods | ||
end | ||
else | ||
base.send(:prepend, InstanceMethods) | ||
end | ||
end | ||
|
||
# Compatibility shim for Rubies not supporting `.prepend` | ||
module InstanceMethodsCompatibility | ||
def log(*args, &block) | ||
log_without_datadog(*args, &block) | ||
end | ||
end | ||
|
||
# InstanceMethods - implementing instrumentation | ||
module InstanceMethods | ||
include InstanceMethodsCompatibility unless Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.0.0') | ||
|
||
EVENT_ACTIVERECORD_SQL = 'sql.active_record'.freeze | ||
|
||
# Override #log since sometimes connections are initialized prior | ||
# to when the patch is applied; this will allow existing connections | ||
# to receive the Shim as well. | ||
def log(*args, &block) | ||
insert_shim! unless shim_inserted? | ||
super | ||
end | ||
|
||
private | ||
|
||
def shim_inserted? | ||
instance_variable_defined?(:@instrumenter) \ | ||
&& Datadog::Shim.shim?(@instrumenter) | ||
end | ||
|
||
def insert_shim! | ||
@instrumenter = Datadog::Shim.new(@instrumenter) do |shim| | ||
connection = self | ||
|
||
shim.override_method!(:instrument) do |*args, &block| | ||
# Inject connection into arguments | ||
if args[0] == EVENT_ACTIVERECORD_SQL && args[1].is_a?(Hash) | ||
args[1][:connection] ||= connection | ||
end | ||
|
||
# Call original | ||
shim_target.instrument(*args, &block) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'spec_helper' | ||
require 'ddtrace' | ||
|
||
require 'active_record' | ||
require 'sqlite3' | ||
|
||
RSpec.describe 'ActiveRecord tracing performance' do | ||
let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } | ||
let(:options) { { tracer: tracer } } | ||
let(:spans) { tracer.writer.spans } | ||
|
||
before(:each) do | ||
skip('Performance test does not run in CI.') | ||
|
||
# Configure the tracer | ||
Datadog.configure do |c| | ||
c.use :active_record, options | ||
end | ||
end | ||
|
||
after(:each) { Datadog.registry[:active_record].reset_configuration! } | ||
|
||
describe 'for an in-memory database' do | ||
let!(:connection) do | ||
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | ||
end | ||
|
||
describe 'when queried with a simple select' do | ||
subject(:measurement) { measure(iterations) } | ||
let(:iterations) { 100_000 } | ||
|
||
def measure(iterations = 1) | ||
Benchmark.measure do | ||
iterations.times do | ||
connection.connection.execute('SELECT 42') | ||
end | ||
end | ||
end | ||
|
||
before(:each) do | ||
# Perform a measurement to warm up | ||
measure(10) | ||
|
||
# Discard warm-up spans | ||
tracer.writer.spans | ||
end | ||
|
||
it 'produces a measurement' do | ||
expect { measurement }.to_not raise_error | ||
expect(spans).to have(iterations).items | ||
puts "\nRun time for #{iterations} iterations: #{measurement.utime}\n" | ||
end | ||
end | ||
end | ||
end |