diff --git a/sentry-ruby/lib/sentry/propagation_context.rb b/sentry-ruby/lib/sentry/propagation_context.rb index 12ce55540..db2f58b8f 100644 --- a/sentry-ruby/lib/sentry/propagation_context.rb +++ b/sentry-ruby/lib/sentry/propagation_context.rb @@ -108,7 +108,7 @@ def get_baggage end # Returns the Dynamic Sampling Context from the baggage. - # @return [String, nil] + # @return [Hash, nil] def get_dynamic_sampling_context get_baggage&.dynamic_sampling_context end diff --git a/sentry-ruby/lib/sentry/scope.rb b/sentry-ruby/lib/sentry/scope.rb index 73e873f43..4feb6ecdd 100644 --- a/sentry-ruby/lib/sentry/scope.rb +++ b/sentry-ruby/lib/sentry/scope.rb @@ -62,6 +62,7 @@ def apply_to_event(event, hint = nil) if span event.contexts[:trace] ||= span.get_trace_context + event.dynamic_sampling_context ||= span.get_dynamic_sampling_context else event.contexts[:trace] ||= propagation_context.get_trace_context event.dynamic_sampling_context ||= propagation_context.get_dynamic_sampling_context diff --git a/sentry-ruby/lib/sentry/span.rb b/sentry-ruby/lib/sentry/span.rb index da7232d9a..3dd2158ea 100644 --- a/sentry-ruby/lib/sentry/span.rb +++ b/sentry-ruby/lib/sentry/span.rb @@ -160,6 +160,12 @@ def to_baggage transaction.get_baggage&.serialize end + # Returns the Dynamic Sampling Context from the transaction baggage. + # @return [Hash, nil] + def get_dynamic_sampling_context + transaction.get_baggage&.dynamic_sampling_context + end + # @return [Hash] def to_hash hash = { diff --git a/sentry-ruby/spec/sentry/scope_spec.rb b/sentry-ruby/spec/sentry/scope_spec.rb index 9b52244a8..6b4b4c168 100644 --- a/sentry-ruby/spec/sentry/scope_spec.rb +++ b/sentry-ruby/spec/sentry/scope_spec.rb @@ -293,7 +293,7 @@ end end - it "sets trace context from span if there's a span" do + it "sets trace context and dynamic_sampling_context from span if there's a span" do transaction = Sentry::Transaction.new(op: "foo", hub: hub) subject.set_span(transaction) @@ -301,6 +301,7 @@ expect(event.contexts[:trace]).to eq(transaction.get_trace_context) expect(event.contexts.dig(:trace, :op)).to eq("foo") + expect(event.dynamic_sampling_context).to eq(transaction.get_dynamic_sampling_context) end it "sets trace context and dynamic_sampling_context from propagation context if there's no span" do diff --git a/sentry-ruby/spec/sentry/span_spec.rb b/sentry-ruby/spec/sentry/span_spec.rb index d890dfa25..f24876d7f 100644 --- a/sentry-ruby/spec/sentry/span_spec.rb +++ b/sentry-ruby/spec/sentry/span_spec.rb @@ -136,6 +136,35 @@ end end + describe "#get_dynamic_sampling_context" do + before do + # because initializing transactions requires an active hub + perform_basic_setup + end + + subject do + baggage = Sentry::Baggage.from_incoming_header( + "other-vendor-value-1=foo;bar;baz, "\ + "sentry-trace_id=771a43a4192642f0b136d5159a501700, "\ + "sentry-public_key=49d0f7386ad645858ae85020e393bef3, "\ + "sentry-sample_rate=0.01337, "\ + "sentry-user_id=Am%C3%A9lie, "\ + "other-vendor-value-2=foo;bar;" + ) + + Sentry::Transaction.new(hub: Sentry.get_current_hub, baggage: baggage).start_child + end + + it "propagates sentry dynamic_sampling_context" do + expect(subject.get_dynamic_sampling_context).to eq({ + "sample_rate" => "0.01337", + "public_key" => "49d0f7386ad645858ae85020e393bef3", + "trace_id" => "771a43a4192642f0b136d5159a501700", + "user_id" => "Amélie" + }) + end + end + describe "#start_child" do before do # because initializing transactions requires an active hub