Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Sequel integration configuration #452

Merged
merged 1 commit into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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