Skip to content

Commit 02a5e26

Browse files
committed
Add sidekiq config propagate_traces to control trace header injection
1 parent 8108ddb commit 02a5e26

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
- Add new sidekiq config `report_only_dead_jobs` ([#2581](https://github.com/getsentry/sentry-ruby/pull/2581))
66
- Add `max_nesting` of 10 to breadcrumbs data serialization ([#2583](https://github.com/getsentry/sentry-ruby/pull/2583))
7+
- Add sidekiq config `propagate_traces` to control trace header injection ([#2588](https://github.com/getsentry/sentry-ruby/pull/2588))
8+
9+
If you use schedulers you can get one large trace with all your jobs which is undesirable.
10+
We recommend using the following to propagate traces only from the Rails server and not elsewhere.
11+
12+
```ruby
13+
config.sidekiq.propagate_traces = false unless Rails.const_defined?('Server')
14+
```
715

816
### Bug Fixes
917

sentry-sidekiq/lib/sentry/sidekiq/configuration.rb

+4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ class Configuration
2424
# Only report jobs that don't have `dead: false` set in the job's `sidekiq_options`
2525
attr_accessor :report_only_dead_jobs
2626

27+
# Whether we should inject headers while enqueuing the job in order to have a connected trace
28+
attr_accessor :propagate_traces
29+
2730
def initialize
2831
@report_after_job_retries = false
2932
@report_only_dead_jobs = false
33+
@propagate_traces = true
3034
end
3135
end
3236
end

sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ def call(worker_class, job, queue, _redis_pool)
9595

9696
user = Sentry.get_current_scope.user
9797
job["sentry_user"] = user unless user.empty?
98-
job["trace_propagation_headers"] ||= Sentry.get_trace_propagation_headers
98+
99+
if Sentry.configuration.sidekiq.propagate_traces
100+
job["trace_propagation_headers"] ||= Sentry.get_trace_propagation_headers
101+
end
99102

100103
Sentry.with_child_span(op: "queue.publish", description: worker_class.to_s) do |span|
101104
set_span_data(span, id: job["jid"], queue: queue)

sentry-sidekiq/spec/sentry/sidekiq/configuration_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,16 @@
2626
expect(subject.report_after_job_retries).to eq(false)
2727
end
2828
end
29+
30+
describe "#report_only_dead_jobs" do
31+
it "has correct default value" do
32+
expect(subject.report_only_dead_jobs).to eq(false)
33+
end
34+
end
35+
36+
describe "#propagate_traces" do
37+
it "has correct default value" do
38+
expect(subject.propagate_traces).to eq(true)
39+
end
40+
end
2941
end

sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,19 @@
222222
expect(event.spans[0][:data]['messaging.message.id']).to eq(message_id)
223223
expect(event.spans[0][:data]['messaging.destination.name']).to eq('default')
224224
end
225+
226+
it "does not propagate headers with propagate_traces = false" do
227+
perform_basic_setup do |config|
228+
config.traces_sample_rate = 1.0
229+
config.sidekiq.propagate_traces = false
230+
end
231+
232+
Sentry.get_current_scope.set_span(transaction)
233+
234+
client.push('queue' => 'default', 'class' => HappyWorker, 'args' => [])
235+
236+
expect(queue.size).to be(1)
237+
expect(queue.first["trace_propagation_headers"]).to be_nil
238+
end
225239
end
226240
end

0 commit comments

Comments
 (0)