Skip to content

Commit

Permalink
Always send envelope trace header from dynamic sampling context
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Sep 21, 2023
1 parent 076a4e5 commit f232974
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Increase `Envelope::Item::MAX_SERIALIZED_PAYLOAD_SIZE` to 1MB [#2108](https://github.com/getsentry/sentry-ruby/pull/2108)
- Fix `db_config` begin `nil` in `ActiveRecordSubscriber` [#2111](https://github.com/getsentry/sentry-ruby/pull/2111)
- Fixes [#2109](https://github.com/getsentry/sentry-ruby/issues/2109)
- Always send envelope trace header from dynamic sampling context [#2113](https://github.com/getsentry/sentry-ruby/pull/2113)

## 5.11.0

Expand Down
2 changes: 1 addition & 1 deletion sentry-ruby/lib/sentry/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def envelope_from_event(event)
sent_at: Sentry.utc_now.iso8601
}

if event.is_a?(TransactionEvent) && event.dynamic_sampling_context
if event.is_a?(Event) && event.dynamic_sampling_context

Check warning on line 147 in sentry-ruby/lib/sentry/transport.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/transport.rb#L147

Added line #L147 was not covered by tests
envelope_headers[:trace] = event.dynamic_sampling_context
end

Expand Down
38 changes: 23 additions & 15 deletions sentry-ruby/spec/sentry/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,40 @@
Sentry::Hub.new(client, Sentry::Scope.new)
end

let(:dynamic_sampling_context) do
{
"sample_rate" => "0.01337",
"public_key" => "49d0f7386ad645858ae85020e393bef3",
"trace_id" => "771a43a4192642f0b136d5159a501700",
"user_id" => "Amélie"
}
end

subject { client.transport }

describe "#serialize_envelope" do
context "normal event" do
let(:event) { client.event_from_exception(ZeroDivisionError.new("divided by 0")) }
let(:event) do
event = client.event_from_exception(ZeroDivisionError.new("divided by 0"))
event.dynamic_sampling_context = dynamic_sampling_context
event
end

let(:envelope) { subject.envelope_from_event(event) }

it "generates correct envelope content" do
result, _ = subject.serialize_envelope(envelope)

envelope_header, item_header, item = result.split("\n")
envelope_header_parsed = JSON.parse(envelope_header)

expect(envelope_header).to eq(
<<~ENVELOPE_HEADER.chomp
{"event_id":"#{event.event_id}","dsn":"#{Sentry::TestHelper::DUMMY_DSN}","sdk":#{Sentry.sdk_meta.to_json},"sent_at":"#{Time.now.utc.iso8601}"}
ENVELOPE_HEADER
)
expect(envelope_header_parsed).to eq({
"event_id" => event.event_id,
"dsn" => Sentry::TestHelper::DUMMY_DSN,
"sdk" => Sentry.sdk_meta,
"sent_at" => Time.now.utc.iso8601,
"trace" => dynamic_sampling_context
})

expect(item_header).to eq(
'{"type":"event","content_type":"application/json"}'
Expand All @@ -47,15 +64,6 @@
Sentry::Transaction.new(name: "test transaction", op: "rack.request", hub: hub)
end

let(:dynamic_sampling_context) do
{
"sample_rate" => "0.01337",
"public_key" => "49d0f7386ad645858ae85020e393bef3",
"trace_id" => "771a43a4192642f0b136d5159a501700",
"user_id" => "Amélie"
}
end

let(:event) do
event = client.event_from_transaction(transaction)
event.dynamic_sampling_context = dynamic_sampling_context
Expand Down

0 comments on commit f232974

Please sign in to comment.