Skip to content

Commit

Permalink
Refactored: SuckerPunch to use Datadog::Contrib::Integration. (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Sep 21, 2018
1 parent 90573a2 commit e7f8cca
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ def configure(target = configuration, opts = {})
require 'ddtrace/contrib/rest_client/integration'
require 'ddtrace/contrib/sequel/integration'
require 'ddtrace/contrib/sidekiq/integration'
require 'ddtrace/contrib/sucker_punch/patcher'
require 'ddtrace/contrib/sucker_punch/integration'
require 'ddtrace/monkey'
14 changes: 14 additions & 0 deletions lib/ddtrace/contrib/sucker_punch/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'ddtrace/contrib/configuration/settings'

module Datadog
module Contrib
module SuckerPunch
module Configuration
# Custom settings for the SuckerPunch integration
class Settings < Contrib::Configuration::Settings
option :service_name, default: 'sucker_punch'
end
end
end
end
end
38 changes: 38 additions & 0 deletions lib/ddtrace/contrib/sucker_punch/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/sucker_punch/configuration/settings'
require 'ddtrace/contrib/sucker_punch/patcher'

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

APP = 'sucker_punch'.freeze

register_as :sucker_punch, auto_patch: true

def self.version
Gem.loaded_specs['sucker_punch'] && Gem.loaded_specs['sucker_punch'].version
end

def self.present?
super && defined?(::SuckerPunch)
end

def self.compatible?
super && Gem::Version.new(::SuckerPunch::VERSION) >= Gem::Version.new('2.0.0')
end

def default_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
56 changes: 23 additions & 33 deletions lib/ddtrace/contrib/sucker_punch/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,43 @@
require 'ddtrace/contrib/patcher'
require 'ddtrace/ext/app_types'

module Datadog
module Contrib
module SuckerPunch
SERVICE = 'sucker_punch'.freeze
COMPATIBLE_WITH = Gem::Version.new('2.0.0')

# Responsible for hooking the instrumentation into `sucker_punch`
# Patcher enables patching of 'sucker_punch' module.
module Patcher
include Base
register_as :sucker_punch, auto_patch: true
option :service_name, default: SERVICE

@patched = false
include Contrib::Patcher

module_function

def patch
return @patched if patched? || !compatible?

require 'ddtrace/ext/app_types'
require_relative 'exception_handler'
require_relative 'instrumentation'

add_pin!
ExceptionHandler.patch!
Instrumentation.patch!

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

def patched?
@patched
done?(:sucker_punch)
end

def compatible?
return unless defined?(::SuckerPunch::VERSION)

Gem::Version.new(::SuckerPunch::VERSION) >= COMPATIBLE_WITH
def patch
do_once(:sucker_punch) do
begin
require 'ddtrace/contrib/sucker_punch/exception_handler'
require 'ddtrace/contrib/sucker_punch/instrumentation'

add_pin!
ExceptionHandler.patch!
Instrumentation.patch!
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply SuckerPunch integration: #{e}")
end
end
end

def add_pin!
Pin.new(get_option(:service_name), app: 'sucker_punch', app_type: Ext::AppTypes::WORKER).tap do |pin|
Pin.new(get_option(:service_name), app: Integration::APP, app_type: Ext::AppTypes::WORKER).tap do |pin|
pin.onto(::SuckerPunch)
end
end

private_class_method :compatible?, :add_pin!
def get_option(option)
Datadog.configuration[:sucker_punch].get_option(option)
end
end
end
end
Expand Down

0 comments on commit e7f8cca

Please sign in to comment.