Skip to content

Commit 773f8e7

Browse files
committed
fix: handle time now being in milliseconds rather than float in seconds
1 parent 4612bf3 commit 773f8e7

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

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 / 1000
79+
end
80+
end
7081
end
7182
end
7283
end

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)