Skip to content

Commit

Permalink
Refactored: Sequel to use Datadog::Contrib::Integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Jun 11, 2018
1 parent afe57f3 commit cacf486
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
13 changes: 13 additions & 0 deletions lib/ddtrace/contrib/sequel/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'ddtrace/contrib/configuration/settings'

module Datadog
module Contrib
module Sequel
module Configuration
class Settings < Contrib::Configuration::Settings
# Add any custom Sequel settings or behavior here.
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/ddtrace/contrib/sequel/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def run(sql, options = ::Sequel::OPTS)
def datadog_pin
@pin ||= Datadog::Pin.new(
Datadog.configuration[:sequel][:service_name] || adapter_name,
app: Patcher::APP,
app: Integration::APP,
app_type: Datadog::Ext::AppTypes::DB,
tracer: Datadog.configuration[:sequel][:tracer] || Datadog.tracer
)
Expand Down
30 changes: 30 additions & 0 deletions lib/ddtrace/contrib/sequel/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/sequel/configuration/settings'
require 'ddtrace/contrib/sequel/patcher'

module Datadog
module Contrib
module Sequel
# Description of Sequel integration
class Integration
include Contrib::Integration

APP = 'sequel'.freeze

register_as :sequel, auto_patch: false

def self.compatible?
RUBY_VERSION >= '2.0.0' && defined?(::Sequel)
end

def default_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
24 changes: 4 additions & 20 deletions lib/ddtrace/contrib/sequel/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'ddtrace/contrib/patcher'
require 'ddtrace/contrib/sequel/database'
require 'ddtrace/contrib/sequel/dataset'

Expand All @@ -7,40 +8,23 @@ module Sequel
# Patcher enables patching of 'sequel' module.
# This is used in monkey.rb to manually apply patches
module Patcher
include Base

APP = 'sequel'.freeze

register_as :sequel, auto_patch: false
option :service_name
option :tracer, default: Datadog.tracer

@patched = false
include Contrib::Patcher

module_function

# patched? tells whether patch has been successfully applied
def patched?
@patched
done?(:sequel)
end

def patch
if !@patched && compatible?
do_once(:sequel) do
begin
patch_sequel_database
patch_sequel_dataset

@patched = true
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply Sequel integration: #{e}")
end
end

@patched
end

def compatible?
RUBY_VERSION >= '2.0.0' && defined?(::Sequel)
end

def patch_sequel_database
Expand Down
2 changes: 1 addition & 1 deletion spec/ddtrace/contrib/sequel/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
let(:span) { spans.first }

before(:each) do
skip unless Datadog::Contrib::Sequel::Patcher.compatible?
skip unless Datadog::Contrib::Sequel::Integration.compatible?
end

describe 'for a SQLite database' do
Expand Down
4 changes: 2 additions & 2 deletions spec/ddtrace/contrib/sequel/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'time'
require 'sequel'
require 'ddtrace'
require 'ddtrace/contrib/sequel/patcher'
require 'ddtrace/contrib/sequel/integration'

RSpec.describe 'Sequel instrumentation' do
let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) }
Expand All @@ -17,7 +17,7 @@
let(:spans) { tracer.writer.spans }

before(:each) do
skip unless Datadog::Contrib::Sequel::Patcher.compatible?
skip('Sequel not compatible.') unless Datadog::Contrib::Sequel::Integration.compatible?

# Reset options (that might linger from other tests)
Datadog.configuration[:sequel].reset_options!
Expand Down

0 comments on commit cacf486

Please sign in to comment.