# Distributed tracing with sidekiq job

**URL:** <https://discuss.elastic.co/t/distributed-tracing-with-sidekiq-job/355746>\
**Category:** APM\
**Tags:** ruby\
**Created:** [March 19, 2024, 4:48pm UTC](https://discuss.elastic.co/t/distributed-tracing-with-sidekiq-job/355746 "2024-03-19T16:48:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kurigashvili-godaddy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kurigashvili-godaddy/32/132786_2.png) [@kurigashvili-godaddy](https://discuss.elastic.co/u/kurigashvili-godaddy)\
**Post date:** [March 19, 2024, 4:48pm UTC](https://discuss.elastic.co/t/distributed-tracing-with-sidekiq-job/355746/1 "2024-03-19T16:48:00Z")

</div>

I would like an ability to pass trace parent from request to sidekiq job so trace id for the request and the sidekiq job is the same.

Something similar is currently possible with APM for node.js

> **[Message queues | APM Node.js Agent Reference \[4.x\] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/current/message-queues.html)**

I reported a Github issue but no one replied to me yet.

> <https://github.com/elastic/apm-agent-ruby/issues/1444>
>
> \## Is your feature request related to a problem? Please describe.
> 
> I would lik…e 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
> \`\`\`ruby
> 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

Does anyone know how to do it in ruby? It seems like a very useful functionality that seems to be missing.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 16, 2024, 4:48pm UTC](https://discuss.elastic.co/t/distributed-tracing-with-sidekiq-job/355746/2 "2024-04-16T16:48:57Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
