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 66914fb commit e772421
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 43 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'
15 changes: 15 additions & 0 deletions lib/ddtrace/contrib/sucker_punch/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'ddtrace/contrib/configuration/settings'
require 'ddtrace/contrib/sucker_punch/ext'

module Datadog
module Contrib
module SuckerPunch
module Configuration
# Custom settings for the SuckerPunch integration
class Settings < Contrib::Configuration::Settings
option :service_name, default: Ext::SERVICE_NAME
end
end
end
end
end
18 changes: 18 additions & 0 deletions lib/ddtrace/contrib/sucker_punch/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Datadog
module Contrib
module SuckerPunch
# SuckerPunch integration constants
module Ext
APP = 'sucker_punch'.freeze
SERVICE_NAME = 'sucker_punch'.freeze

SPAN_PERFORM = 'sucker_punch.perform'.freeze
SPAN_PERFORM_ASYNC = 'sucker_punch.perform_async'.freeze
SPAN_PERFORM_IN = 'sucker_punch.perform_in'.freeze

TAG_PERFORM_IN = 'sucker_punch.perform_in'.freeze
TAG_QUEUE = 'sucker_punch.queue'.freeze
end
end
end
end
11 changes: 6 additions & 5 deletions lib/ddtrace/contrib/sucker_punch/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'sucker_punch'
require 'ddtrace/contrib/sucker_punch/ext'

module Datadog
module Contrib
Expand All @@ -15,7 +16,7 @@ def __run_perform(*args)
pin = Datadog::Pin.get_from(::SuckerPunch)
pin.tracer.provider.context = Datadog::Context.new

__with_instrumentation('sucker_punch.perform') do |span|
__with_instrumentation(Ext::SPAN_PERFORM) do |span|
span.resource = "PROCESS #{self}"
__run_perform_without_datadog(*args)
end
Expand All @@ -25,17 +26,17 @@ def __run_perform(*args)

alias_method :__perform_async, :perform_async
def perform_async(*args)
__with_instrumentation('sucker_punch.perform_async') do |span|
__with_instrumentation(Ext::SPAN_PERFORM_ASYNC) do |span|
span.resource = "ENQUEUE #{self}"
__perform_async(*args)
end
end

alias_method :__perform_in, :perform_in
def perform_in(interval, *args)
__with_instrumentation('sucker_punch.perform_in') do |span|
__with_instrumentation(Ext::SPAN_PERFORM_IN) do |span|
span.resource = "ENQUEUE #{self}"
span.set_tag('sucker_punch.perform_in', interval)
span.set_tag(Ext::TAG_PERFORM_IN, interval)
__perform_in(interval, *args)
end
end
Expand All @@ -48,7 +49,7 @@ def __with_instrumentation(name)
pin.tracer.trace(name) do |span|
span.service = pin.service
span.span_type = pin.app_type
span.set_tag('sucker_punch.queue', to_s)
span.set_tag(Ext::TAG_QUEUE, to_s)
yield span
end
end
Expand Down
36 changes: 36 additions & 0 deletions lib/ddtrace/contrib/sucker_punch/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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

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
57 changes: 24 additions & 33 deletions lib/ddtrace/contrib/sucker_punch/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
require 'ddtrace/contrib/patcher'
require 'ddtrace/ext/app_types'
require 'ddtrace/contrib/sucker_punch/ext'

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: Ext::APP, app_type: Datadog::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
8 changes: 4 additions & 4 deletions test/contrib/sucker_punch/patcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_successful_job
assert_equal('sucker_punch.perform', span.name)
assert_equal('PROCESS DummyWorker', span.resource)
assert_equal('DummyWorker', span.get_tag('sucker_punch.queue'))
refute_equal(Ext::Errors::STATUS, span.status)
refute_equal(Datadog::Ext::Errors::STATUS, span.status)
end

def test_failed_job
Expand All @@ -47,9 +47,9 @@ def test_failed_job
assert_equal('sucker_punch.perform', span.name)
assert_equal('PROCESS DummyWorker', span.resource)
assert_equal('DummyWorker', span.get_tag('sucker_punch.queue'))
assert_equal(Ext::Errors::STATUS, span.status)
assert_equal('ZeroDivisionError', span.get_tag(Ext::Errors::TYPE))
assert_equal('divided by 0', span.get_tag(Ext::Errors::MSG))
assert_equal(Datadog::Ext::Errors::STATUS, span.status)
assert_equal('ZeroDivisionError', span.get_tag(Datadog::Ext::Errors::TYPE))
assert_equal('divided by 0', span.get_tag(Datadog::Ext::Errors::MSG))
end

def test_async_enqueueing
Expand Down

0 comments on commit e772421

Please sign in to comment.