diff --git a/CHANGELOG.md b/CHANGELOG.md index bc1709391..5fe10cd9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## Unreleased + +### Bug Fixes + +- Accept Hash in `before_send*` callbacks again ([#2529](https://github.com/getsentry/sentry-ruby/pull/2529)) + - Fixes [#2526](https://github.com/getsentry/sentry-ruby/issues/2526) + ## 5.22.2 ### Features @@ -6,7 +13,7 @@ - Use attempt_threshold to skip reporting on first N attempts ([#2503](https://github.com/getsentry/sentry-ruby/pull/2503)) - Support `code.namespace` for Ruby 3.4+ stacktraces ([#2506](https://github.com/getsentry/sentry-ruby/pull/2506)) -### Bug fixes +### Bug Fixes - Default to `internal_error` error type for OpenTelemetry spans [#2473](https://github.com/getsentry/sentry-ruby/pull/2473) - Improve `before_send` and `before_send_transaction`'s return value handling ([#2504](https://github.com/getsentry/sentry-ruby/pull/2504)) diff --git a/sentry-ruby/lib/sentry/client.rb b/sentry-ruby/lib/sentry/client.rb index f1db4a295..9f8c02899 100644 --- a/sentry-ruby/lib/sentry/client.rb +++ b/sentry-ruby/lib/sentry/client.rb @@ -183,7 +183,15 @@ def send_event(event, hint = nil) if event_type != TransactionEvent::TYPE && configuration.before_send event = configuration.before_send.call(event, hint) - unless event.is_a?(ErrorEvent) + case event + when ErrorEvent + # do nothing + when Hash + log_debug(<<~MSG) + Returning a Hash from before_send is deprecated and will be removed in the next major version. + Please return a Sentry::ErrorEvent object instead. + MSG + else # Avoid serializing the event object in this case because we aren't sure what it is and what it contains log_debug(<<~MSG) Discarded event because before_send didn't return a Sentry::ErrorEvent object but an instance of #{event.class} @@ -196,10 +204,17 @@ def send_event(event, hint = nil) if event_type == TransactionEvent::TYPE && configuration.before_send_transaction event = configuration.before_send_transaction.call(event, hint) - if event.is_a?(TransactionEvent) + if event.is_a?(TransactionEvent) || event.is_a?(Hash) spans_after = event.is_a?(TransactionEvent) ? event.spans.size : 0 spans_delta = spans_before - spans_after transport.record_lost_event(:before_send, "span", num: spans_delta) if spans_delta > 0 + + if event.is_a?(Hash) + log_debug(<<~MSG) + Returning a Hash from before_send_transaction is deprecated and will be removed in the next major version. + Please return a Sentry::TransactionEvent object instead. + MSG + end else # Avoid serializing the event object in this case because we aren't sure what it is and what it contains log_debug(<<~MSG) diff --git a/sentry-ruby/spec/sentry/client/event_sending_spec.rb b/sentry-ruby/spec/sentry/client/event_sending_spec.rb index 0fc9105c1..5e0bcac4e 100644 --- a/sentry-ruby/spec/sentry/client/event_sending_spec.rb +++ b/sentry-ruby/spec/sentry/client/event_sending_spec.rb @@ -269,8 +269,22 @@ 123 end - client.send_event(event) + return_value = client.send_event(event) expect(string_io.string).to include("Discarded event because before_send didn't return a Sentry::ErrorEvent object but an instance of Integer") + expect(return_value).to eq(nil) + end + + it "warns about Hash value's deprecation" do + string_io = StringIO.new + logger = Logger.new(string_io, level: :debug) + configuration.logger = logger + configuration.before_send = lambda do |_event, _hint| + { foo: "bar" } + end + + return_value = client.send_event(event) + expect(string_io.string).to include("Returning a Hash from before_send is deprecated and will be removed in the next major version.") + expect(return_value).to eq({ foo: "bar" }) end end @@ -323,8 +337,22 @@ nil end - client.send_event(event) + return_value = client.send_event(event) expect(string_io.string).to include("Discarded event because before_send_transaction didn't return a Sentry::TransactionEvent object but an instance of NilClass") + expect(return_value).to be_nil + end + + it "warns about Hash value's deprecation" do + string_io = StringIO.new + logger = Logger.new(string_io, level: :debug) + configuration.logger = logger + configuration.before_send_transaction = lambda do |_event, _hint| + { foo: "bar" } + end + + return_value = client.send_event(event) + expect(string_io.string).to include("Returning a Hash from before_send_transaction is deprecated and will be removed in the next major version.") + expect(return_value).to eq({ foo: "bar" }) end end