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

fix: add support for Sidekiq 8 #1444

Merged
merged 10 commits into from
Mar 26, 2025
6 changes: 6 additions & 0 deletions instrumentation/sidekiq/Appraisals
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# frozen_string_literal: true

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2.0')
appraise 'sidekiq-8.0' do
gem 'sidekiq', '~> 8.0'
end
end

appraise 'sidekiq-7.0' do
gem 'sidekiq', '~> 7.0'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def call(_worker_class, job, _queue, _redis_pool)

tracer.in_span(span_name, attributes: attributes, kind: :producer) do |span|
OpenTelemetry.propagation.inject(job)
span.add_event('created_at', timestamp: job['created_at'])
span.add_event('created_at', timestamp: time_from_timestamp(job['created_at']))
yield
end
end
Expand All @@ -45,6 +45,15 @@ def instrumentation_config
def tracer
Sidekiq::Instrumentation.instance.tracer
end

def time_from_timestamp(timestamp)
if timestamp.is_a?(Float)
# old format, timestamps were stored as fractional seconds since the epoch
timestamp
else
timestamp/1000r
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ def call(_worker, msg, _queue)
end

extracted_context = OpenTelemetry.propagation.extract(msg)
created_at = time_from_timestamp(msg['created_at'])
enqueued_at = time_from_timestamp(msg['created_at'])
OpenTelemetry::Context.with_current(extracted_context) do
if instrumentation_config[:propagation_style] == :child
tracer.in_span(span_name, attributes: attributes, kind: :consumer) do |span|
span.add_event('created_at', timestamp: msg['created_at'])
span.add_event('enqueued_at', timestamp: msg['enqueued_at'])
span.add_event('created_at', timestamp: created_at)
span.add_event('enqueued_at', timestamp: enqueued_at)
yield
end
else
Expand All @@ -44,8 +46,8 @@ def call(_worker, msg, _queue)
links << OpenTelemetry::Trace::Link.new(span_context) if instrumentation_config[:propagation_style] == :link && span_context.valid?
span = tracer.start_root_span(span_name, attributes: attributes, links: links, kind: :consumer)
OpenTelemetry::Trace.with_span(span) do
span.add_event('created_at', timestamp: msg['created_at'])
span.add_event('enqueued_at', timestamp: msg['enqueued_at'])
span.add_event('created_at', timestamp: created_at)
span.add_event('enqueued_at', timestamp: enqueued_at)
yield
rescue Exception => e # rubocop:disable Lint/RescueException
span.record_exception(e)
Expand All @@ -67,6 +69,15 @@ def instrumentation_config
def tracer
Sidekiq::Instrumentation.instance.tracer
end

def time_from_timestamp(timestamp)
if timestamp.is_a?(Float)
# old format, timestamps were stored as fractional seconds since the epoch
timestamp
else
timestamp/1000r
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
_(enqueue_span.attributes['messaging.destination']).must_equal 'default'
_(enqueue_span.attributes['messaging.destination_kind']).must_equal 'queue'
_(enqueue_span.events.size).must_equal(1)
_(enqueue_span.events[0].name).must_equal('created_at')

created_event = enqueue_span.events[0]
_(created_event.name).must_equal('created_at')
_(created_event.timestamp.digits.count).must_equal(19)
end

it 'traces when enqueued with Active Job' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@
_(job_span.attributes['messaging.operation']).must_equal 'process'
_(job_span.attributes['peer.service']).must_be_nil
_(job_span.events.size).must_equal(2)
_(job_span.events[0].name).must_equal('created_at')
_(job_span.events[1].name).must_equal('enqueued_at')

created_event = job_span.events[0]
_(created_event.name).must_equal('created_at')
_(created_event.timestamp.digits.count).must_equal(19)

enqueued_event = job_span.events[1]
_(enqueued_event.name).must_equal('enqueued_at')
_(enqueued_event.timestamp.digits.count).must_equal(19)
end

it 'traces when enqueued with Active Job' do
Expand Down
Loading