Skip to content

Commit 3d7ee98

Browse files
authored
fix: make sidekiq instrumentation compatible with sidekiq 6.5.0 (#1304)
* fix: make sidekiq instrumentation compatible with sidekiq 6.5.0 This commit: - changes our config method to instrumentation_config. Sidekiq's new Component module is mixed in in a lot of places, and that module includes a config method that we very much do not want to overwrite. - changes the MockLoader in our test suite to be compatible with Sidekiq 6.5.0. Starting in Sidekiq 6.5.0, Sidekiq accepts the Sidekiq singleton as an argument to Launcher.new. It seems like this is an intermediate step to making Sidekiq::Config its own class, which is planned for Sidekiq 7.0. - adds appraisals for sidekiq 6.4.0 and 6.5.0 * appease the cop
1 parent 8f44895 commit 3d7ee98

File tree

8 files changed

+62
-19
lines changed

8 files changed

+62
-19
lines changed

instrumentation/sidekiq/Appraisals

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# frozen_string_literal: true
22

3+
appraise 'sidekiq-6.5' do
4+
gem 'sidekiq', '~> 6.5'
5+
end
6+
7+
appraise 'sidekiq-6.4' do
8+
gem 'sidekiq', '~> 6.4'
9+
end
10+
311
appraise 'sidekiq-6.3' do
412
gem 'sidekiq', '~> 6.3'
513
end

instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def call(_worker_class, job, _queue, _redis_pool) # rubocop:disable Metrics/AbcS
2020
'messaging.destination' => job['queue'],
2121
'messaging.destination_kind' => 'queue'
2222
}
23-
attributes['peer.service'] = config[:peer_service] if config[:peer_service]
23+
attributes['peer.service'] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service]
2424

25-
span_name = case config[:span_naming]
25+
span_name = case instrumentation_config[:span_naming]
2626
when :job_class then "#{job['wrapped']&.to_s || job['class']} send"
2727
else "#{job['queue']} send"
2828
end
@@ -36,7 +36,7 @@ def call(_worker_class, job, _queue, _redis_pool) # rubocop:disable Metrics/AbcS
3636

3737
private
3838

39-
def config
39+
def instrumentation_config
4040
Sidekiq::Instrumentation.instance.config
4141
end
4242

instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ def call(_worker, msg, _queue) # rubocop:disable Metrics/AbcSize, Metrics/Cyclom
2121
'messaging.destination_kind' => 'queue',
2222
'messaging.operation' => 'process'
2323
}
24-
attributes['peer.service'] = config[:peer_service] if config[:peer_service]
24+
attributes['peer.service'] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service]
2525

26-
span_name = case config[:span_naming]
26+
span_name = case instrumentation_config[:span_naming]
2727
when :job_class then "#{msg['wrapped']&.to_s || msg['class']} process"
2828
else "#{msg['queue']} process"
2929
end
3030

3131
extracted_context = OpenTelemetry.propagation.extract(msg)
3232
OpenTelemetry::Context.with_current(extracted_context) do
33-
if config[:propagation_style] == :child
33+
if instrumentation_config[:propagation_style] == :child
3434
tracer.in_span(span_name, attributes: attributes, kind: :consumer) do |span|
3535
span.add_event('created_at', timestamp: msg['created_at'])
3636
span.add_event('enqueued_at', timestamp: msg['enqueued_at'])
@@ -39,7 +39,7 @@ def call(_worker, msg, _queue) # rubocop:disable Metrics/AbcSize, Metrics/Cyclom
3939
else
4040
links = []
4141
span_context = OpenTelemetry::Trace.current_span(extracted_context).context
42-
links << OpenTelemetry::Trace::Link.new(span_context) if config[:propagation_style] == :link && span_context.valid?
42+
links << OpenTelemetry::Trace::Link.new(span_context) if instrumentation_config[:propagation_style] == :link && span_context.valid?
4343
span = tracer.start_root_span(span_name, attributes: attributes, links: links, kind: :consumer)
4444
OpenTelemetry::Trace.with_span(span) do
4545
span.add_event('created_at', timestamp: msg['created_at'])
@@ -58,7 +58,7 @@ def call(_worker, msg, _queue) # rubocop:disable Metrics/AbcSize, Metrics/Cyclom
5858

5959
private
6060

61-
def config
61+
def instrumentation_config
6262
Sidekiq::Instrumentation.instance.config
6363
end
6464

instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/patches/launcher.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ module Launcher
1313
private
1414

1515
def # rubocop:disable Naming/MethodName
16-
if config[:trace_launcher_heartbeat]
16+
if instrumentation_config[:trace_launcher_heartbeat]
1717
attributes = {}
18-
attributes['peer.service'] = config[:peer_service] if config[:peer_service]
18+
attributes['peer.service'] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service]
1919
tracer.in_span('Sidekiq::Launcher#heartbeat', attributes: attributes) { super }
2020
else
2121
OpenTelemetry::Common::Utilities.untraced { super }
@@ -26,7 +26,7 @@ def tracer
2626
Sidekiq::Instrumentation.instance.tracer
2727
end
2828

29-
def config
29+
def instrumentation_config
3030
Sidekiq::Instrumentation.instance.config
3131
end
3232
end

instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/patches/poller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ module Patches
1111
# The Poller module contains instrumentation for the enqueue and wait methods
1212
module Poller
1313
def enqueue
14-
if config[:trace_poller_enqueue]
14+
if instrumentation_config[:trace_poller_enqueue]
1515
attributes = {}
16-
attributes['peer.service'] = config[:peer_service] if config[:peer_service]
16+
attributes['peer.service'] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service]
1717
tracer.in_span('Sidekiq::Scheduled::Poller#enqueue', attributes: attributes) { super }
1818
else
1919
OpenTelemetry::Common::Utilities.untraced { super }
@@ -23,7 +23,7 @@ def enqueue
2323
private
2424

2525
def wait
26-
if config[:trace_poller_wait]
26+
if instrumentation_config[:trace_poller_wait]
2727
tracer.in_span('Sidekiq::Scheduled::Poller#wait') { super }
2828
else
2929
OpenTelemetry::Common::Utilities.untraced { super }
@@ -34,7 +34,7 @@ def tracer
3434
Sidekiq::Instrumentation.instance.tracer
3535
end
3636

37-
def config
37+
def instrumentation_config
3838
Sidekiq::Instrumentation.instance.config
3939
end
4040
end

instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/patches/processor.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ module Processor
1313
private
1414

1515
def process_one
16-
if config[:trace_processor_process_one]
16+
if instrumentation_config[:trace_processor_process_one]
1717
attributes = {}
18-
attributes['peer.service'] = config[:peer_service] if config[:peer_service]
18+
attributes['peer.service'] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service]
1919
tracer.in_span('Sidekiq::Processor#process_one', attributes: attributes) { super }
2020
else
2121
OpenTelemetry::Common::Utilities.untraced { super }
@@ -26,7 +26,7 @@ def tracer
2626
Sidekiq::Instrumentation.instance.tracer
2727
end
2828

29-
def config
29+
def instrumentation_config
3030
Sidekiq::Instrumentation.instance.config
3131
end
3232
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
require 'sidekiq/cli'
8+
require 'sidekiq/launcher'
9+
10+
class MockLoader
11+
attr_reader :launcher
12+
13+
def initialize
14+
Sidekiq[:queues] << 'default'
15+
16+
@launcher = Sidekiq::Launcher.new(Sidekiq)
17+
@launcher.fire_event(:startup)
18+
end
19+
20+
def poller
21+
launcher.poller
22+
end
23+
24+
def manager
25+
launcher.manager
26+
end
27+
end

instrumentation/sidekiq/test/test_helper.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212

1313
require 'minitest/autorun'
1414
require 'rspec/mocks/minitest_integration'
15-
require 'helpers/mock_loader'
1615
require 'active_job'
1716
require 'pry'
1817

18+
# Sidekiq changed its loading mechanism in 6.5.0, but we still want to test the
19+
# older versions. We can eliminate the first part of this conditional when we no
20+
# longer support Sidekiq 6.4.x versions.
21+
if Gem::Version.new(Sidekiq::VERSION) < Gem::Version.new('6.5.0')
22+
require 'helpers/mock_loader'
23+
else
24+
require 'helpers/mock_loader_new_launcher'
25+
end
26+
1927
# OpenTelemetry SDK config for testing
2028
EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new
2129
span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER)

0 commit comments

Comments
 (0)