From da9a395fa4c88165deb0ebebb1f12513371a9693 Mon Sep 17 00:00:00 2001 From: David Elner Date: Thu, 20 Sep 2018 12:54:04 -0400 Subject: [PATCH] Implement Resque integration configuration (#546) * Changed: ConfigurationHelpers#remove_patch! to work with Integration patchers. * Refactored: Resque to use Datadog::Contrib::Integration. --- lib/ddtrace.rb | 2 +- .../contrib/resque/configuration/settings.rb | 15 +++++ lib/ddtrace/contrib/resque/integration.rb | 39 ++++++++++++ lib/ddtrace/contrib/resque/patcher.rb | 62 ++++++++----------- spec/support/configuration_helpers.rb | 14 ++++- 5 files changed, 92 insertions(+), 40 deletions(-) create mode 100644 lib/ddtrace/contrib/resque/configuration/settings.rb create mode 100644 lib/ddtrace/contrib/resque/integration.rb 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..716f6e2c9a --- /dev/null +++ b/lib/ddtrace/contrib/resque/configuration/settings.rb @@ -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 diff --git a/lib/ddtrace/contrib/resque/integration.rb b/lib/ddtrace/contrib/resque/integration.rb new file mode 100644 index 0000000000..6aac50125a --- /dev/null +++ b/lib/ddtrace/contrib/resque/integration.rb @@ -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 diff --git a/lib/ddtrace/contrib/resque/patcher.rb b/lib/ddtrace/contrib/resque/patcher.rb index a40750db17..90e42e794a 100644 --- a/lib/ddtrace/contrib/resque/patcher.rb +++ b/lib/ddtrace/contrib/resque/patcher.rb @@ -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 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