Skip to content

Commit a4e2010

Browse files
solnicgithub-copilot
andcommitted
feat(active_job): add active_job_propagate_traces config option
Mirrors Sidekiq's propagate_traces flag. When set to false, trace propagation headers are not injected into the serialized job payload and the consumer starts a new unconnected transaction. Defaults to true (existing behaviour is preserved). Co-Authored-By: github-copilot <noreply@example.com>
1 parent 0d4d3c0 commit a4e2010

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

sentry-rails/lib/sentry/rails/active_job.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ def serialize
2727

2828
begin
2929
sentry_data = {}
30-
headers = Sentry.get_trace_propagation_headers
31-
sentry_data["trace_propagation_headers"] = headers if headers && !headers.empty?
30+
if Sentry.configuration.rails.active_job_propagate_traces
31+
headers = Sentry.get_trace_propagation_headers
32+
sentry_data["trace_propagation_headers"] = headers if headers && !headers.empty?
33+
end
3234

3335
if Sentry.configuration.send_default_pii
3436
user = Sentry.get_current_scope.user || {}

sentry-rails/lib/sentry/rails/configuration.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ class Configuration
172172
# Set this option to true if you want Sentry to capture each retry failure
173173
attr_accessor :active_job_report_on_retry_error
174174

175+
# Whether we should inject trace propagation headers into the serialized job
176+
# payload in order to have a connected trace between producer and consumer.
177+
# Defaults to true. Set to false to opt out.
178+
attr_accessor :active_job_propagate_traces
179+
175180
# Configuration for structured logging feature
176181
# @return [StructuredLoggingConfiguration]
177182
attr_reader :structured_logging
@@ -193,6 +198,7 @@ def initialize
193198
@db_query_source_threshold_ms = 100
194199
@active_support_logger_subscription_items = Sentry::Rails::ACTIVE_SUPPORT_LOGGER_SUBSCRIPTION_ITEMS_DEFAULT.dup
195200
@active_job_report_on_retry_error = false
201+
@active_job_propagate_traces = true
196202
@structured_logging = StructuredLoggingConfiguration.new
197203
end
198204
end

sentry-rails/spec/active_job/shared_examples/tracing/trace_propagation.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,37 @@ def perform; end
4747
expect(consumer_transaction).not_to be_nil
4848
expect(consumer_transaction.contexts.dig(:trace, :trace_id)).to eq(parent_trace_id)
4949
end
50+
51+
context "when active_job_propagate_traces is false" do
52+
let(:configure_sentry) do
53+
proc do |config|
54+
config.traces_sample_rate = 1.0
55+
config.rails.active_job_propagate_traces = false
56+
end
57+
end
58+
59+
it "does not inject trace headers into the job payload" do
60+
within_parent_transaction do
61+
successful_job.perform_later
62+
end
63+
64+
payload = queue_adapter.enqueued_jobs.last
65+
sentry_payload = (payload[:_sentry_full_payload] || payload)["_sentry"]
66+
expect(sentry_payload&.dig("trace_propagation_headers")).to be_nil
67+
end
68+
69+
it "starts a new unconnected consumer transaction" do
70+
parent_trace_id = nil
71+
72+
within_parent_transaction do |parent|
73+
parent_trace_id = parent.trace_id
74+
successful_job.perform_later
75+
end
76+
77+
drain
78+
79+
expect(consumer_transaction).not_to be_nil
80+
expect(consumer_transaction.contexts.dig(:trace, :trace_id)).not_to eq(parent_trace_id)
81+
end
82+
end
5083
end

0 commit comments

Comments
 (0)