diff --git a/lib/ddtrace.rb b/lib/ddtrace.rb index 3ac6996ab0..2ed8b1e7d0 100644 --- a/lib/ddtrace.rb +++ b/lib/ddtrace.rb @@ -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' diff --git a/lib/ddtrace/contrib/resque/configuration/settings.rb b/lib/ddtrace/contrib/resque/configuration/settings.rb new file mode 100644 index 0000000000..1a7fe6bbff --- /dev/null +++ b/lib/ddtrace/contrib/resque/configuration/settings.rb @@ -0,0 +1,16 @@ +require 'ddtrace/contrib/configuration/settings' +require 'ddtrace/contrib/resque/ext' + +module Datadog + module Contrib + module Resque + module Configuration + # Custom settings for the Resque integration + class Settings < Contrib::Configuration::Settings + option :service_name, default: Ext::SERVICE_NAME + option :workers, default: [] + end + end + end + end +end diff --git a/lib/ddtrace/contrib/resque/ext.rb b/lib/ddtrace/contrib/resque/ext.rb new file mode 100644 index 0000000000..a551705eae --- /dev/null +++ b/lib/ddtrace/contrib/resque/ext.rb @@ -0,0 +1,13 @@ +module Datadog + module Contrib + module Resque + # Resque integration constants + module Ext + APP = 'resque'.freeze + SERVICE_NAME = 'resque'.freeze + + SPAN_JOB = 'resque.job'.freeze + end + end + end +end diff --git a/lib/ddtrace/contrib/resque/integration.rb b/lib/ddtrace/contrib/resque/integration.rb new file mode 100644 index 0000000000..c5f7d812fd --- /dev/null +++ b/lib/ddtrace/contrib/resque/integration.rb @@ -0,0 +1,37 @@ +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 + + 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 diff --git a/lib/ddtrace/contrib/resque/patcher.rb b/lib/ddtrace/contrib/resque/patcher.rb index a40750db17..0e7a9f8c14 100644 --- a/lib/ddtrace/contrib/resque/patcher.rb +++ b/lib/ddtrace/contrib/resque/patcher.rb @@ -1,49 +1,40 @@ +require 'ddtrace/contrib/patcher' +require 'ddtrace/ext/app_types' +require 'ddtrace/contrib/sidekiq/ext' + 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: Ext::APP, app_type: Datadog::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 diff --git a/lib/ddtrace/contrib/resque/resque_job.rb b/lib/ddtrace/contrib/resque/resque_job.rb index d3eac5bd4b..d83f55dcda 100644 --- a/lib/ddtrace/contrib/resque/resque_job.rb +++ b/lib/ddtrace/contrib/resque/resque_job.rb @@ -1,5 +1,6 @@ require 'ddtrace/ext/app_types' require 'ddtrace/sync_writer' +require 'ddtrace/contrib/sidekiq/ext' require 'resque' module Datadog @@ -10,7 +11,7 @@ module ResqueJob def around_perform(*args) pin = Pin.get_from(::Resque) return yield unless pin && pin.tracer - pin.tracer.trace('resque.job', service: pin.service) do |span| + pin.tracer.trace(Ext::SPAN_JOB, service: pin.service) do |span| span.resource = name span.span_type = pin.app_type yield diff --git a/spec/support/configuration_helpers.rb b/spec/support/configuration_helpers.rb index 3419176b1c..0556ff1d74 100644 --- a/spec/support/configuration_helpers.rb +++ b/spec/support/configuration_helpers.rb @@ -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