Skip to content

Commit

Permalink
Ensure Sidekiq client middleware is same for clients & servers
Browse files Browse the repository at this point in the history
  • Loading branch information
drcapulet committed Jul 8, 2020
1 parent 64ec745 commit ce121e8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/ddtrace/contrib/sidekiq/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ def target_version

def patch
require 'ddtrace/contrib/sidekiq/client_tracer'
require 'ddtrace/contrib/sidekiq/server_tracer'

::Sidekiq.configure_client do |config|
config.client_middleware do |chain|
chain.add(Sidekiq::ClientTracer)
end
end

require 'ddtrace/contrib/sidekiq/server_tracer'
::Sidekiq.configure_server do |config|
# If a job enqueues another job, make sure it has the same client
# middleware.
config.client_middleware do |chain|
chain.add(Sidekiq::ClientTracer)
end

config.server_middleware do |chain|
chain.add(Sidekiq::ServerTracer)
end
Expand Down
35 changes: 35 additions & 0 deletions spec/ddtrace/contrib/sidekiq/patcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'ddtrace/contrib/support/spec_helper'
require_relative 'support/helper'

RSpec.describe Datadog::Contrib::Sidekiq::Patcher do
before do
Sidekiq.client_middleware.clear
Sidekiq.server_middleware.clear

allow(Sidekiq).to receive(:server?).and_return(server)
# NB: This is needed because we want to patch multiple times.
allow(described_class).to receive(:do_once).with(:patch).and_yield
end

# NB: This needs to be after the before block above so that the use :sidekiq
# executes after the allows are setup.
include_context 'Sidekiq testing'

context 'for a client' do
let(:server) { false }

it 'correctly patches' do
expect(Sidekiq.client_middleware.entries.map(&:klass)).to eq([Datadog::Contrib::Sidekiq::ClientTracer])
expect(Sidekiq.server_middleware.entries.map(&:klass)).to eq([])
end
end

context 'for a server' do
let(:server) { true }

it 'correctly patches' do
expect(Sidekiq.client_middleware.entries.map(&:klass)).to eq([Datadog::Contrib::Sidekiq::ClientTracer])
expect(Sidekiq.server_middleware.entries.map(&:klass)).to eq([Datadog::Contrib::Sidekiq::ServerTracer])
end
end
end

0 comments on commit ce121e8

Please sign in to comment.