Skip to content

Commit

Permalink
Refactored: DelayedJob to use Datadog::Contrib::Integration. (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Sep 24, 2018
1 parent 982b739 commit 9089bb1
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def configure(target = configuration, opts = {})
require 'ddtrace/contrib/aws/patcher'
require 'ddtrace/contrib/concurrent_ruby/integration'
require 'ddtrace/contrib/dalli/patcher'
require 'ddtrace/contrib/delayed_job/patcher'
require 'ddtrace/contrib/delayed_job/integration'
require 'ddtrace/contrib/elasticsearch/patcher'
require 'ddtrace/contrib/excon/patcher'
require 'ddtrace/contrib/faraday/patcher'
Expand Down
15 changes: 15 additions & 0 deletions lib/ddtrace/contrib/delayed_job/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/delayed_job/ext'

module Datadog
module Contrib
module DelayedJob
module Configuration
# Custom settings for the DelayedJob 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/delayed_job/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Datadog
module Contrib
module DelayedJob
# DelayedJob integration constants
module Ext
APP = 'delayed_job'.freeze
SERVICE_NAME = 'delayed_job'.freeze

SPAN_JOB = 'delayed_job'.freeze

TAG_ATTEMPTS = 'delayed_job.attempts'.freeze
TAG_ID = 'delayed_job.id'.freeze
TAG_PRIORITY = 'delayed_job.priority'.freeze
TAG_QUEUE = 'delayed_job.queue'.freeze
end
end
end
end
32 changes: 32 additions & 0 deletions lib/ddtrace/contrib/delayed_job/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/delayed_job/configuration/settings'
require 'ddtrace/contrib/delayed_job/patcher'

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

register_as :delayed_job

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

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

def default_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
47 changes: 20 additions & 27 deletions lib/ddtrace/contrib/delayed_job/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
require 'ddtrace/contrib/patcher'
require 'ddtrace/ext/app_types'

module Datadog
module Contrib
module DelayedJob
# DelayedJob integration
# Patcher enables patching of 'delayed_job' module.
module Patcher
include Base
register_as :delayed_job

option :service_name, default: 'delayed_job'.freeze
option :tracer, default: Datadog.tracer

@patched = false
include Contrib::Patcher

class << self
def patch
return @patched if patched? || !defined?(::Delayed)
module_function

require 'ddtrace/ext/app_types'
require_relative 'plugin'

add_instrumentation(::Delayed::Worker)
@patched = true
rescue => e
Tracer.log.error("Unable to apply DelayedJob integration: #{e}")
@patched
end
def patched?
done?(:delayed_job)
end

def patched?
@patched
def patch
do_once(:delayed_job) do
begin
require 'ddtrace/contrib/delayed_job/plugin'
add_instrumentation(::Delayed::Worker)
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply DelayedJob integration: #{e}")
end
end
end

private

def add_instrumentation(klass)
klass.plugins << Plugin
end
def add_instrumentation(klass)
klass.plugins << Plugin
end
end
end
Expand Down
13 changes: 7 additions & 6 deletions lib/ddtrace/contrib/delayed_job/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'delayed/plugin'
require 'ddtrace/contrib/delayed_job/ext'

module Datadog
module Contrib
Expand All @@ -8,12 +9,12 @@ class Plugin < Delayed::Plugin
def self.instrument(job, &block)
return block.call(job) unless tracer && tracer.enabled

tracer.trace('delayed_job'.freeze, service: configuration[:service_name], resource: job.name) do |span|
span.set_tag('delayed_job.id'.freeze, job.id)
span.set_tag('delayed_job.queue'.freeze, job.queue) if job.queue
span.set_tag('delayed_job.priority'.freeze, job.priority)
span.set_tag('delayed_job.attempts'.freeze, job.attempts)
span.span_type = Ext::AppTypes::WORKER
tracer.trace(Ext::SPAN_JOB, service: configuration[:service_name], resource: job.name) do |span|
span.set_tag(Ext::TAG_ID, job.id)
span.set_tag(Ext::TAG_QUEUE, job.queue) if job.queue
span.set_tag(Ext::TAG_PRIORITY, job.priority)
span.set_tag(Ext::TAG_ATTEMPTS, job.attempts)
span.span_type = Datadog::Ext::AppTypes::WORKER

yield job
end
Expand Down
20 changes: 9 additions & 11 deletions spec/ddtrace/contrib/delayed_job/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
let(:worker_plugins) { [] }
let!(:delayed_worker_class) { class_double('Delayed::Worker', plugins: worker_plugins).as_stubbed_const }

before do
described_class.instance_variable_set(:@patched, false)
end

context 'when delayed job is not present' do
before do
hide_const('Delayed')
end

it "shouldn't patch the code" do
expect { described_class.patch }.not_to(change { described_class.patched? })
def remove_patch!
Datadog.registry[:delayed_job].patcher.tap do |patcher|
if patcher.instance_variable_defined?(:@done_once)
patcher.instance_variable_get(:@done_once).delete(:delayed_job)
end
end
end

# Prevents random order from breaking tests
before(:each) { remove_patch! }
after(:each) { remove_patch! }

it 'should patch the code' do
expect { described_class.patch }.to change { described_class.patched? }.from(false).to(true)
end
Expand Down

0 comments on commit 9089bb1

Please sign in to comment.