Skip to content

Commit eae4e97

Browse files
solnicCodex
andcommitted
fix(queues): skip unsampled optional telemetry
Co-Authored-By: Codex <noreply@example.com>
1 parent 97bbe9a commit eae4e97

4 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎sentry-rails/spec/active_job/test_adapter_spec.rb‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@
3232
end
3333
end
3434

35+
context "when the transaction is unsampled" do
36+
let(:configure_sentry) { proc { |config| config.traces_sample_rate = 0.0 } }
37+
38+
it "enqueues and performs without encoding optional body size" do
39+
performed_arguments = []
40+
recording_job = job_fixture do
41+
define_method(:perform) { |argument| performed_arguments << argument }
42+
end
43+
serializations = 0
44+
serializer = TracePoint.new(:call) do |call|
45+
serializations += 1 if call.self.equal?(JSON) && call.method_id == :generate
46+
end
47+
48+
serializer.enable do
49+
within_parent_transaction { recording_job.perform_later("hello") }
50+
drain
51+
end
52+
53+
expect(performed_arguments).to eq(["hello"])
54+
expect(serializations).to eq(0)
55+
expect(transactions).to be_empty
56+
end
57+
end
58+
3559
context "when tracing is disabled" do
3660
it "reports job errors without messaging.message.body.size" do
3761
expect do

‎sentry-ruby/lib/sentry/job_extensions/consumer.rb‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ def start_transaction(scope, job, trace_headers: nil, payload: nil, always_conti
5151
end
5252
return unless transaction
5353

54-
set_messaging_data(transaction, resolve(messaging_data).reject { |_key, value| value.nil? })
55-
set_messaging_data(transaction, registry.call(extension, :messaging_data, job))
56-
set_retry_count(transaction, extension, job, default: default_retry_count)
54+
if transaction.sampled
55+
set_messaging_data(transaction, resolve(messaging_data).reject { |_key, value| value.nil? })
56+
set_messaging_data(transaction, registry.call(extension, :messaging_data, job))
57+
set_retry_count(transaction, extension, job, default: default_retry_count)
58+
end
5759

5860
scope.set_span(transaction)
5961
transaction

‎sentry-ruby/lib/sentry/job_extensions/producer.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def with_span(job, description:, messaging_data: {}, default_messaging_data: {},
4747
span_description = registry.call(extension, :publish_description, job) || description
4848

4949
Sentry.with_child_span(op: OP_NAME, description: span_description) do |span|
50-
if span
50+
if span && span.sampled
5151
span_origin = registry.call(extension, :span_origin, job) || origin
5252
span.set_origin(span_origin) if span_origin
5353

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@
5050
expect(transport.events.last.contexts.dig(:trace, :trace_id)).to eq(trace_id)
5151
end
5252

53+
it "propagates unsampled jobs without serializing optional telemetry" do
54+
perform_basic_setup { |config| config.traces_sample_rate = 0.0 }
55+
parent = Sentry.start_transaction(name: "enqueue", op: "test")
56+
Sentry.get_current_scope.set_span(parent)
57+
job = { "jid" => "unsampled", "class" => "HappyWorker", "queue" => "default", "args" => [] }
58+
serializations = 0
59+
serializer = TracePoint.new(:call) do |call|
60+
serializations += 1 if call.self.equal?(Sidekiq) && call.method_id == :dump_json
61+
end
62+
performed = false
63+
64+
serializer.enable do
65+
Sentry::Sidekiq::SentryContextClientMiddleware.new.call(HappyWorker, job, "default", nil) { true }
66+
described_class.new.call(HappyWorker.new, job, "default") do
67+
performed = true
68+
Sentry.capture_message("unsampled job")
69+
end
70+
end
71+
72+
expect(performed).to be(true)
73+
expect(serializations).to eq(0)
74+
expect(job["trace_propagation_headers"].keys).to include("sentry-trace", "baggage")
75+
expect(transport.events.last.contexts.dig(:trace, :trace_id)).to eq(parent.trace_id)
76+
end
77+
5378
context "with tracing enabled" do
5479
before do
5580
perform_basic_setup { |config| config.traces_sample_rate = 1.0 }

0 commit comments

Comments
 (0)