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 24, 2018
1 parent 3df964b commit 8074a42
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 29 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'
15 changes: 15 additions & 0 deletions lib/ddtrace/contrib/sidekiq/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/sidekiq/ext'

module Datadog
module Contrib
module Sidekiq
module Configuration
# Custom settings for the Sidekiq integration
class Settings < Contrib::Configuration::Settings
option :service_name, default: Ext::SERVICE_NAME
end
end
end
end
end
19 changes: 19 additions & 0 deletions lib/ddtrace/contrib/sidekiq/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Datadog
module Contrib
module Sidekiq
# Sidekiq integration constants
module Ext
APP = 'sidekiq'.freeze
SERVICE_NAME = 'sidekiq'.freeze

SPAN_JOB = 'sidekiq.job'.freeze

TAG_JOB_DELAY = 'sidekiq.job.delay'.freeze
TAG_JOB_ID = 'sidekiq.job.id'.freeze
TAG_JOB_QUEUE = 'sidekiq.job.queue'.freeze
TAG_JOB_RETRY = 'sidekiq.job.retry'.freeze
TAG_JOB_WRAPPER = 'sidekiq.job.wrapper'.freeze
end
end
end
end
36 changes: 36 additions & 0 deletions lib/ddtrace/contrib/sidekiq/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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

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
20 changes: 10 additions & 10 deletions lib/ddtrace/contrib/sidekiq/tracer.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require 'sidekiq/api'

require 'ddtrace/ext/app_types'
require 'ddtrace/contrib/sidekiq/ext'

module Datadog
module Contrib
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 All @@ -26,13 +26,13 @@ def call(worker, job, queue)
service = sidekiq_service(resource_worker(resource))
set_service_info(service)

@tracer.trace('sidekiq.job', service: service, span_type: Datadog::Ext::AppTypes::WORKER) do |span|
@tracer.trace(Ext::SPAN_JOB, service: service, span_type: Datadog::Ext::AppTypes::WORKER) do |span|
span.resource = resource
span.set_tag('sidekiq.job.id', job['jid'])
span.set_tag('sidekiq.job.retry', job['retry'])
span.set_tag('sidekiq.job.queue', job['queue'])
span.set_tag('sidekiq.job.wrapper', job['class']) if job['wrapped']
span.set_tag('sidekiq.job.delay', 1000.0 * (Time.now.utc.to_f - job['enqueued_at'].to_f))
span.set_tag(Ext::TAG_JOB_ID, job['jid'])
span.set_tag(Ext::TAG_JOB_RETRY, job['retry'])
span.set_tag(Ext::TAG_JOB_QUEUE, job['queue'])
span.set_tag(Ext::TAG_JOB_WRAPPER, job['class']) if job['wrapped']
span.set_tag(Ext::TAG_JOB_DELAY, 1000.0 * (Time.now.utc.to_f - job['enqueued_at'].to_f))

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

0 comments on commit 8074a42

Please sign in to comment.