Skip to content

Commit

Permalink
Fix error events missing a DSC when there's an active span
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Sep 19, 2024
1 parent c3bcfa0 commit 57e979b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
- Support human readable intervals in `sidekiq-cron` ([#2387](https://github.com/getsentry/sentry-ruby/pull/2387))
- Set default app dirs pattern ([#2390](https://github.com/getsentry/sentry-ruby/pull/2390))

### Bug Fixes

- Fix error events missing a DSC when there's an active span ([#2408](https://github.com/getsentry/sentry-ruby/pull/2408))

## 5.19.0

### Features
Expand Down
2 changes: 1 addition & 1 deletion sentry-ruby/lib/sentry/propagation_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions sentry-ruby/lib/sentry/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 65 in sentry-ruby/lib/sentry/scope.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/scope.rb#L65

Added line #L65 was not covered by tests
else
event.contexts[:trace] ||= propagation_context.get_trace_context
event.dynamic_sampling_context ||= propagation_context.get_dynamic_sampling_context
Expand Down
6 changes: 6 additions & 0 deletions sentry-ruby/lib/sentry/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 166 in sentry-ruby/lib/sentry/span.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/span.rb#L166

Added line #L166 was not covered by tests
end

# @return [Hash]
def to_hash
hash = {
Expand Down
3 changes: 2 additions & 1 deletion sentry-ruby/spec/sentry/scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,15 @@
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)

subject.apply_to_event(event)

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
Expand Down
29 changes: 29 additions & 0 deletions sentry-ruby/spec/sentry/span_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 57e979b

Please sign in to comment.