Skip to content

Commit

Permalink
Accept Hash in before_send* callbacks again (#2529)
Browse files Browse the repository at this point in the history
* Allow before_send* callbacks to return Hash

* Print deprecation message when users pass a Hash to before_send* callbacks

* Update changelog
  • Loading branch information
st0012 authored Jan 29, 2025
1 parent 1ee671c commit a064512
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
Expand Down
19 changes: 17 additions & 2 deletions sentry-ruby/lib/sentry/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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)
Expand Down
32 changes: 30 additions & 2 deletions sentry-ruby/spec/sentry/client/event_sending_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a064512

Please sign in to comment.