Skip to content

Commit f1dd08f

Browse files
authored
fix: add support for Sidekiq 8 (#1444)
1 parent 782c046 commit f1dd08f

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

instrumentation/sidekiq/Appraisals

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

3+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2.0')
4+
appraise 'sidekiq-8.0' do
5+
gem 'sidekiq', '~> 8.0'
6+
end
7+
end
8+
39
appraise 'sidekiq-7.0' do
410
gem 'sidekiq', '~> 7.0'
511
end

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def call(_worker_class, job, _queue, _redis_pool)
3131

3232
tracer.in_span(span_name, attributes: attributes, kind: :producer) do |span|
3333
OpenTelemetry.propagation.inject(job)
34-
span.add_event('created_at', timestamp: job['created_at'])
34+
span.add_event('created_at', timestamp: time_from_timestamp(job['created_at']))
3535
yield
3636
end
3737
end
@@ -45,6 +45,15 @@ def instrumentation_config
4545
def tracer
4646
Sidekiq::Instrumentation.instance.tracer
4747
end
48+
49+
def time_from_timestamp(timestamp)
50+
if timestamp.is_a?(Float)
51+
# old format, timestamps were stored as fractional seconds since the epoch
52+
timestamp
53+
else
54+
timestamp/1000r
55+
end
56+
end
4857
end
4958
end
5059
end

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ def call(_worker, msg, _queue)
3131
end
3232

3333
extracted_context = OpenTelemetry.propagation.extract(msg)
34+
created_at = time_from_timestamp(msg['created_at'])
35+
enqueued_at = time_from_timestamp(msg['created_at'])
3436
OpenTelemetry::Context.with_current(extracted_context) do
3537
if instrumentation_config[:propagation_style] == :child
3638
tracer.in_span(span_name, attributes: attributes, kind: :consumer) do |span|
37-
span.add_event('created_at', timestamp: msg['created_at'])
38-
span.add_event('enqueued_at', timestamp: msg['enqueued_at'])
39+
span.add_event('created_at', timestamp: created_at)
40+
span.add_event('enqueued_at', timestamp: enqueued_at)
3941
yield
4042
end
4143
else
@@ -44,8 +46,8 @@ def call(_worker, msg, _queue)
4446
links << OpenTelemetry::Trace::Link.new(span_context) if instrumentation_config[:propagation_style] == :link && span_context.valid?
4547
span = tracer.start_root_span(span_name, attributes: attributes, links: links, kind: :consumer)
4648
OpenTelemetry::Trace.with_span(span) do
47-
span.add_event('created_at', timestamp: msg['created_at'])
48-
span.add_event('enqueued_at', timestamp: msg['enqueued_at'])
49+
span.add_event('created_at', timestamp: created_at)
50+
span.add_event('enqueued_at', timestamp: enqueued_at)
4951
yield
5052
rescue Exception => e # rubocop:disable Lint/RescueException
5153
span.record_exception(e)
@@ -67,6 +69,15 @@ def instrumentation_config
6769
def tracer
6870
Sidekiq::Instrumentation.instance.tracer
6971
end
72+
73+
def time_from_timestamp(timestamp)
74+
if timestamp.is_a?(Float)
75+
# old format, timestamps were stored as fractional seconds since the epoch
76+
timestamp
77+
else
78+
timestamp/1000r
79+
end
80+
end
7081
end
7182
end
7283
end

instrumentation/sidekiq/test/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
_(enqueue_span.attributes['messaging.destination']).must_equal 'default'
4545
_(enqueue_span.attributes['messaging.destination_kind']).must_equal 'queue'
4646
_(enqueue_span.events.size).must_equal(1)
47-
_(enqueue_span.events[0].name).must_equal('created_at')
47+
48+
created_event = enqueue_span.events[0]
49+
_(created_event.name).must_equal('created_at')
50+
_(created_event.timestamp.digits.count).must_equal(19)
4851
end
4952

5053
it 'traces when enqueued with Active Job' do

instrumentation/sidekiq/test/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware_test.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@
4545
_(job_span.attributes['messaging.operation']).must_equal 'process'
4646
_(job_span.attributes['peer.service']).must_be_nil
4747
_(job_span.events.size).must_equal(2)
48-
_(job_span.events[0].name).must_equal('created_at')
49-
_(job_span.events[1].name).must_equal('enqueued_at')
48+
49+
created_event = job_span.events[0]
50+
_(created_event.name).must_equal('created_at')
51+
_(created_event.timestamp.digits.count).must_equal(19)
52+
53+
enqueued_event = job_span.events[1]
54+
_(enqueued_event.name).must_equal('enqueued_at')
55+
_(enqueued_event.timestamp.digits.count).must_equal(19)
5056
end
5157

5258
it 'traces when enqueued with Active Job' do

0 commit comments

Comments
 (0)