Skip to content

Commit a064512

Browse files
authored
Accept Hash in before_send* callbacks again (#2529)
* Allow before_send* callbacks to return Hash * Print deprecation message when users pass a Hash to before_send* callbacks * Update changelog
1 parent 1ee671c commit a064512

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## Unreleased
2+
3+
### Bug Fixes
4+
5+
- Accept Hash in `before_send*` callbacks again ([#2529](https://github.com/getsentry/sentry-ruby/pull/2529))
6+
- Fixes [#2526](https://github.com/getsentry/sentry-ruby/issues/2526)
7+
18
## 5.22.2
29

310
### Features
@@ -6,7 +13,7 @@
613
- Use attempt_threshold to skip reporting on first N attempts ([#2503](https://github.com/getsentry/sentry-ruby/pull/2503))
714
- Support `code.namespace` for Ruby 3.4+ stacktraces ([#2506](https://github.com/getsentry/sentry-ruby/pull/2506))
815

9-
### Bug fixes
16+
### Bug Fixes
1017

1118
- Default to `internal_error` error type for OpenTelemetry spans [#2473](https://github.com/getsentry/sentry-ruby/pull/2473)
1219
- Improve `before_send` and `before_send_transaction`'s return value handling ([#2504](https://github.com/getsentry/sentry-ruby/pull/2504))

sentry-ruby/lib/sentry/client.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,15 @@ def send_event(event, hint = nil)
183183
if event_type != TransactionEvent::TYPE && configuration.before_send
184184
event = configuration.before_send.call(event, hint)
185185

186-
unless event.is_a?(ErrorEvent)
186+
case event
187+
when ErrorEvent
188+
# do nothing
189+
when Hash
190+
log_debug(<<~MSG)
191+
Returning a Hash from before_send is deprecated and will be removed in the next major version.
192+
Please return a Sentry::ErrorEvent object instead.
193+
MSG
194+
else
187195
# Avoid serializing the event object in this case because we aren't sure what it is and what it contains
188196
log_debug(<<~MSG)
189197
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)
196204
if event_type == TransactionEvent::TYPE && configuration.before_send_transaction
197205
event = configuration.before_send_transaction.call(event, hint)
198206

199-
if event.is_a?(TransactionEvent)
207+
if event.is_a?(TransactionEvent) || event.is_a?(Hash)
200208
spans_after = event.is_a?(TransactionEvent) ? event.spans.size : 0
201209
spans_delta = spans_before - spans_after
202210
transport.record_lost_event(:before_send, "span", num: spans_delta) if spans_delta > 0
211+
212+
if event.is_a?(Hash)
213+
log_debug(<<~MSG)
214+
Returning a Hash from before_send_transaction is deprecated and will be removed in the next major version.
215+
Please return a Sentry::TransactionEvent object instead.
216+
MSG
217+
end
203218
else
204219
# Avoid serializing the event object in this case because we aren't sure what it is and what it contains
205220
log_debug(<<~MSG)

sentry-ruby/spec/sentry/client/event_sending_spec.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,22 @@
269269
123
270270
end
271271

272-
client.send_event(event)
272+
return_value = client.send_event(event)
273273
expect(string_io.string).to include("Discarded event because before_send didn't return a Sentry::ErrorEvent object but an instance of Integer")
274+
expect(return_value).to eq(nil)
275+
end
276+
277+
it "warns about Hash value's deprecation" do
278+
string_io = StringIO.new
279+
logger = Logger.new(string_io, level: :debug)
280+
configuration.logger = logger
281+
configuration.before_send = lambda do |_event, _hint|
282+
{ foo: "bar" }
283+
end
284+
285+
return_value = client.send_event(event)
286+
expect(string_io.string).to include("Returning a Hash from before_send is deprecated and will be removed in the next major version.")
287+
expect(return_value).to eq({ foo: "bar" })
274288
end
275289
end
276290

@@ -323,8 +337,22 @@
323337
nil
324338
end
325339

326-
client.send_event(event)
340+
return_value = client.send_event(event)
327341
expect(string_io.string).to include("Discarded event because before_send_transaction didn't return a Sentry::TransactionEvent object but an instance of NilClass")
342+
expect(return_value).to be_nil
343+
end
344+
345+
it "warns about Hash value's deprecation" do
346+
string_io = StringIO.new
347+
logger = Logger.new(string_io, level: :debug)
348+
configuration.logger = logger
349+
configuration.before_send_transaction = lambda do |_event, _hint|
350+
{ foo: "bar" }
351+
end
352+
353+
return_value = client.send_event(event)
354+
expect(string_io.string).to include("Returning a Hash from before_send_transaction is deprecated and will be removed in the next major version.")
355+
expect(return_value).to eq({ foo: "bar" })
328356
end
329357
end
330358

0 commit comments

Comments
 (0)