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

Configuring Tracing for Sidekiq #1586

Closed
choxi opened this issue Oct 1, 2021 · 5 comments · Fixed by #1590
Closed

Configuring Tracing for Sidekiq #1586

choxi opened this issue Oct 1, 2021 · 5 comments · Fixed by #1590

Comments

@choxi
Copy link

choxi commented Oct 1, 2021

What is the recommended way to pass a trace ID from Rails to a Sidekiq worker? I have this configuration:

# config/initializers/sentry.rb

Sentry.init do |config|
  config.breadcrumbs_logger = [:active_support_logger, :http_logger]

  # Ignore 404s
  config.excluded_exceptions += ['ActionController::RoutingError']

  config.traces_sample_rate = 1.0
end

and

# Gemfile
gem "sentry-rails"
gem "sentry-sidekiq"

When I queue a Sidekiq job:

class PostsController < ApplicationController
  def create
    # How do I pass the trace_id to the worker?
    PostsWorker.perform_async(post_id, trace_id)
  end
end

class PostsWorker
  include Sidekiq::Worker
  
  def perform(post_id, trace_id)
    # How do I set the trace_id in the worker?
    Sentry.set_trace_id(trace_id)
  end
end

It currently starts a separate trace for the controller and the worker, but I'd like the worker to belong to the same trace as the request. Is there a way to do this?

@choxi choxi added the question label Oct 1, 2021
@st0012 st0012 added this to To do in 4.x via automation Oct 1, 2021
@st0012 st0012 added this to the 4.8.0 milestone Oct 1, 2021
@st0012
Copy link
Collaborator

st0012 commented Oct 1, 2021

@choxi currently it's not possible without adding your own Sidekiq client middleware. but I think this is something we should support natively. so I'll implement it in a future release 🙂

@st0012
Copy link
Collaborator

st0012 commented Oct 8, 2021

@choxi I've added #1590 to address this issue. can you give it a try?

4.x automation moved this from To do to Done Oct 9, 2021
@choxi
Copy link
Author

choxi commented Feb 4, 2022

@st0012 it's working, thank you!

@choxi
Copy link
Author

choxi commented Feb 4, 2022

One related question: if our frontend is sending sentry-trace headers, will the backend always capture those even if it goes over the sample rate? Is there a way to make sure we always capture the full trace when the sample rate is < 1?

@st0012
Copy link
Collaborator

st0012 commented Feb 10, 2022

@choxi if you use traces_sample_rate, the inherited sampling decision will be prioritized over it.
if you use traces_sampler, you need to prioritize it yourself.

config.traces_sampler = lambda do |sampling_context|
  parent_sampled = sampling_context[:parent_sampled]

  if !parent_sampled.nil?
    parent_sampled
  else
    # the rest of sampling logic
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
4.x
  
Done
Development

Successfully merging a pull request may close this issue.

2 participants