Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Sidekiq client middleware is same for clients & servers #1099

Merged
merged 1 commit into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 39 additions & 0 deletions spec/ddtrace/contrib/sidekiq/patcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'ddtrace/contrib/support/spec_helper'
require_relative 'support/helper'

RSpec.describe Datadog::Contrib::Sidekiq::Patcher do
before do
# Sidekiq 3.x unfortunately doesn't let us access server_middleware unless
# actually a server, so we just have to skip them.
skip if Gem.loaded_specs['sidekiq'].version < Gem::Version.new('4.0')

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