Skip to content

Commit

Permalink
Refactored: Sidekiq to use Datadog::Contrib::Integration. (#545)
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Sep 21, 2018
1 parent 67eec25 commit 11bce27
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ def configure(target = configuration, opts = {})
require 'ddtrace/contrib/resque/patcher'
require 'ddtrace/contrib/rest_client/integration'
require 'ddtrace/contrib/sequel/integration'
require 'ddtrace/contrib/sidekiq/patcher'
require 'ddtrace/contrib/sidekiq/integration'
require 'ddtrace/contrib/sucker_punch/patcher'
require 'ddtrace/monkey'
14 changes: 14 additions & 0 deletions lib/ddtrace/contrib/sidekiq/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'ddtrace/contrib/configuration/settings'

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

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

APP = 'sidekiq'.freeze

register_as :sidekiq

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

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

def self.compatible?
super && version >= Gem::Version.new('4.0.0')
end

def default_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
36 changes: 18 additions & 18 deletions lib/ddtrace/contrib/sidekiq/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
require 'ddtrace/contrib/patcher'

module Datadog
module Contrib
module Sidekiq
# Provides instrumentation support for Sidekiq
# Patcher enables patching of 'sidekiq' module.
module Patcher
include Base
VERSION_REQUIRED = Gem::Version.new('4.0.0')
register_as :sidekiq
option :service_name, default: 'sidekiq'
option :tracer, default: Datadog.tracer
include Contrib::Patcher

module_function

def patch
return unless compatible?

require_relative 'tracer'
def patched?
done?(:sidekiq)
end

::Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add(Sidekiq::Tracer)
def patch
do_once(:sidekiq) do
begin
require 'ddtrace/contrib/sidekiq/tracer'
::Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add(Sidekiq::Tracer)
end
end
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply Sidekiq integration: #{e}")
end
end
end

def compatible?
defined?(::Sidekiq) &&
Gem::Version.new(::Sidekiq::VERSION) >= VERSION_REQUIRED
end
end
end
end
Expand Down
7 changes: 3 additions & 4 deletions lib/ddtrace/contrib/sidekiq/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ module Sidekiq
# Tracer is a Sidekiq server-side middleware which traces executed jobs
class Tracer
def initialize(options = {})
config = Datadog.configuration[:sidekiq].merge(options)
@tracer = config[:tracer]
@sidekiq_service = config[:service_name]
@tracer = options[:tracer] || Datadog.configuration[:sidekiq][:tracer]
@sidekiq_service = options[:service_name] || Datadog.configuration[:sidekiq][:service_name]
end

def call(worker, job, queue)
Expand Down Expand Up @@ -62,7 +61,7 @@ def set_service_info(service)
return if @tracer.services[service]
@tracer.set_service_info(
service,
'sidekiq',
Integration::APP,
Datadog::Ext::AppTypes::WORKER
)
end
Expand Down

0 comments on commit 11bce27

Please sign in to comment.