Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ 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: created_at)
span.add_event('enqueued_at', timestamp: enqueued_at)
add_span_timestamps(span, msg)
yield
end
else
Expand All @@ -46,8 +43,7 @@ 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: created_at)
span.add_event('enqueued_at', timestamp: enqueued_at)
add_span_timestamps(span, msg)
yield
rescue Exception => e # rubocop:disable Lint/RescueException
span.record_exception(e)
Expand All @@ -70,6 +66,13 @@ def tracer
Sidekiq::Instrumentation.instance.tracer
end

def add_span_timestamps(span, msg)
created_at = msg['created_at']
enqueued_at = msg['enqueued_at']
span.add_event('created_at', timestamp: time_from_timestamp(created_at)) if created_at
span.add_event('enqueued_at', timestamp: time_from_timestamp(enqueued_at)) if enqueued_at
end

def time_from_timestamp(timestamp)
if timestamp.is_a?(Float)
# old format, timestamps were stored as fractional seconds since the epoch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@
_(job_span.attributes['messaging.operation']).must_equal 'process'
end

it 'traces when enqueued through another system' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name this as "when enqueued with minimal data"

payload = { 'queue' => 'default', 'args' => [], 'class' => SimpleJob, 'enqueued_at' => Time.current, 'jid' => '4' }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't enqueued_at be removed here too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exq supplies this one, but not created_at from what I can tell. In addition, Sidekiq::Client#raw_push adds enqueued_at on its own. I could try doing some shenanigans to push the job directly to Redis, but I'm not sure that effort would be worth the slight test improvement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, it's not about Exq. It's about what any custom library could do. If it's possible to enqueue a job and have it processed by sidekiq without an `enqueued_at, then we should handle it.
If sidekiq would auto-add it, or reject processing it then, we're good.

Sidekiq::Client.new.send(:raw_push, [payload])
Sidekiq::Worker.drain_all

_(job_span.name).must_equal 'default process'
_(job_span.kind).must_equal :consumer
_(job_span.attributes['messaging.system']).must_equal 'sidekiq'
_(job_span.attributes['messaging.sidekiq.job_class']).must_equal 'SimpleJob'
_(job_span.attributes['messaging.message_id']).must_equal '4'
_(job_span.attributes['messaging.destination']).must_equal 'default'
_(job_span.attributes['messaging.destination_kind']).must_equal 'queue'
_(job_span.attributes['messaging.operation']).must_equal 'process'
_(job_span.attributes['peer.service']).must_be_nil
_(job_span.events.size).must_equal(1)

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

it 'defaults to using links to the enqueing span but does not continue the trace' do
SimpleJob.perform_async
SimpleJob.drain
Expand Down