apm-agent-ruby icon indicating copy to clipboard operation
apm-agent-ruby copied to clipboard

Pass trace parent to sidekiq job

Open kurigashvili-godaddy opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe.

I would like an ability to pass trace parent from request to sidekiq job so trace id for request and sidekiq is the same. This is currently possible in APM for node.js https://www.elastic.co/guide/en/apm/agent/nodejs/current/message-queues.html

I tried creating transaction in sidekiq middleware like this

ElasticAPM.start_transaction('some-job', 'sidekiq', trace_context: ElasticAPM::TraceContext.new(
    traceparent: ElasticAPM::TraceContext::Traceparent.new(trace_id: trace_parent[:trace_id])
 ))

But APM Sidekiq middleware always runs after and it fails with ExistingTransactionError. If it runs before, it still would have this issue. https://github.com/elastic/apm-agent-ruby/blob/main/lib/elastic_apm/spies/sidekiq.rb#L32 https://github.com/elastic/apm-agent-ruby/blob/main/lib/elastic_apm/instrumenter.rb#L116

Possible solution

May be you can look for certain parameter on job object and use it to set trace_context parameter. https://github.com/elastic/apm-agent-ruby/blob/main/lib/elastic_apm/spies/sidekiq.rb#L32

Client middleware can pass it similar to the example here https://github.com/sidekiq/sidekiq/wiki/Middleware#client-middleware

kurigashvili-godaddy avatar Mar 15 '24 06:03 kurigashvili-godaddy

Hi @kurigashvili-godaddy thanks for opening this issue. It seems reasonable to support passing the trace context to the sidekiq job. Since you've already identified all the relevant parts of the code, do you want to open a PR to add support?

estolfo avatar Mar 28 '24 12:03 estolfo