Skip to content

Commit

Permalink
Implement Resque integration configuration (#546)
Browse files Browse the repository at this point in the history
* Changed: ConfigurationHelpers#remove_patch! to work with Integration patchers.

* Refactored: Resque to use Datadog::Contrib::Integration.
  • Loading branch information
delner committed Sep 21, 2018
1 parent 677476b commit da9a395
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def configure(target = configuration, opts = {})
require 'ddtrace/contrib/rails/integration'
require 'ddtrace/contrib/rake/patcher'
require 'ddtrace/contrib/redis/patcher'
require 'ddtrace/contrib/resque/patcher'
require 'ddtrace/contrib/resque/integration'
require 'ddtrace/contrib/rest_client/integration'
require 'ddtrace/contrib/sequel/integration'
require 'ddtrace/contrib/sidekiq/integration'
Expand Down
15 changes: 15 additions & 0 deletions lib/ddtrace/contrib/resque/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'ddtrace/contrib/configuration/settings'

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

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

APP = 'resque'.freeze

register_as :resque, auto_patch: true

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

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

def default_configuration
Configuration::Settings.new
end

def patcher
Patcher
end

class << self
# Globally-acccesible reference for pre-forking optimization
attr_accessor :sync_writer
end
end
end
end
end
62 changes: 26 additions & 36 deletions lib/ddtrace/contrib/resque/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,39 @@
require 'ddtrace/contrib/patcher'
require 'ddtrace/ext/app_types'

module Datadog
module Contrib
# Namespace for `resque` integration
module Resque
SERVICE = 'resque'.freeze

class << self
# Globally-acccesible reference for pre-forking optimization
attr_accessor :sync_writer
end

# Patcher for Resque integration - sets up the pin for the integration
# Patcher enables patching of 'resque' module.
module Patcher
include Base
register_as :resque, auto_patch: true
option :service_name, default: SERVICE

@patched = false
option :workers, default: []

class << self
def patch
return @patched if patched? || !defined?(::Resque)
include Contrib::Patcher

require 'ddtrace/ext/app_types'
require_relative 'resque_job'
module_function

add_pin
get_option(:workers).each { |worker| worker.extend(ResqueJob) }
@patched = true
rescue => e
Tracer.log.error("Unable to apply Resque integration: #{e}")
@patched
end
def patched?
done?(:resque)
end

def patched?
@patched
def patch
do_once(:resque) do
begin
require_relative 'resque_job'
add_pin
get_option(:workers).each { |worker| worker.extend(ResqueJob) }
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply Resque integration: #{e}")
end
end
end

private
def add_pin
Pin
.new(get_option(:service_name), app: Integration::APP, app_type: Ext::AppTypes::WORKER)
.onto(::Resque)
end

def add_pin
Pin
.new(get_option(:service_name), app: 'resque', app_type: Ext::AppTypes::WORKER)
.onto(::Resque)
end
def get_option(option)
Datadog.configuration[:resque].get_option(option)
end
end
end
Expand Down
14 changes: 11 additions & 3 deletions spec/support/configuration_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
module ConfigurationHelpers
def remove_patch!(integration)
Datadog
.registry[integration]
.instance_variable_set('@patched', false)
if Datadog.registry[integration].respond_to?(:patcher)
Datadog.registry[integration].patcher.tap do |patcher|
if patcher.instance_variable_defined?(:@done_once)
patcher.instance_variable_get(:@done_once).delete(integration)
end
end
else
Datadog
.registry[integration]
.instance_variable_set('@patched', false)
end
end
end

0 comments on commit da9a395

Please sign in to comment.