Skip to content

Commit ed918ff

Browse files
committed
Add before_send_check_in (#2703)
* Cleanup before_send to only apply to ErrorEvent * remove the Hash deprecation message
1 parent 8bc301e commit ed918ff

File tree

6 files changed

+98
-58
lines changed

6 files changed

+98
-58
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- Remove `config.async` [#1894](https://github.com/getsentry/sentry-ruby/pull/1894)
66
- Migrate from to_hash to to_h ([#2351](https://github.com/getsentry/sentry-ruby/pull/2351))
7+
- Add `before_send_check_in` for applying to `CheckInEvent` ([#2703](https://github.com/getsentry/sentry-ruby/pull/2703))
8+
- Returning a hash from `before_send` and `before_send_transaction` is no longer supported and will drop the event.
79

810
## 5.27.1
911

sentry-ruby/lib/sentry/client.rb

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,10 @@ def send_event(event, hint = nil)
210210
data_category = Envelope::Item.data_category(event.type)
211211
spans_before = event.is_a?(TransactionEvent) ? event.spans.size : 0
212212

213-
if event.type != TransactionEvent::TYPE && configuration.before_send
213+
if event.is_a?(ErrorEvent) && configuration.before_send
214214
event = configuration.before_send.call(event, hint)
215215

216-
case event
217-
when ErrorEvent, CheckInEvent
218-
# do nothing
219-
when Hash
220-
log_debug(<<~MSG)
221-
Returning a Hash from before_send is deprecated and will be removed in the next major version.
222-
Please return a Sentry::ErrorEvent object instead.
223-
MSG
224-
else
216+
if !event.is_a?(ErrorEvent)
225217
# Avoid serializing the event object in this case because we aren't sure what it is and what it contains
226218
log_debug(<<~MSG)
227219
Discarded event because before_send didn't return a Sentry::ErrorEvent object but an instance of #{event.class}
@@ -231,21 +223,10 @@ def send_event(event, hint = nil)
231223
end
232224
end
233225

234-
if event.type == TransactionEvent::TYPE && configuration.before_send_transaction
226+
if event.is_a?(TransactionEvent) && configuration.before_send_transaction
235227
event = configuration.before_send_transaction.call(event, hint)
236228

237-
if event.is_a?(TransactionEvent) || event.is_a?(Hash)
238-
spans_after = event.is_a?(TransactionEvent) ? event.spans.size : 0
239-
spans_delta = spans_before - spans_after
240-
transport.record_lost_event(:before_send, "span", num: spans_delta) if spans_delta > 0
241-
242-
if event.is_a?(Hash)
243-
log_debug(<<~MSG)
244-
Returning a Hash from before_send_transaction is deprecated and will be removed in the next major version.
245-
Please return a Sentry::TransactionEvent object instead.
246-
MSG
247-
end
248-
else
229+
if !event.is_a?(TransactionEvent)
249230
# Avoid serializing the event object in this case because we aren't sure what it is and what it contains
250231
log_debug(<<~MSG)
251232
Discarded event because before_send_transaction didn't return a Sentry::TransactionEvent object but an instance of #{event.class}
@@ -254,6 +235,23 @@ def send_event(event, hint = nil)
254235
transport.record_lost_event(:before_send, "span", num: spans_before + 1)
255236
return
256237
end
238+
239+
spans_after = event.is_a?(TransactionEvent) ? event.spans.size : 0
240+
spans_delta = spans_before - spans_after
241+
transport.record_lost_event(:before_send, "span", num: spans_delta) if spans_delta > 0
242+
end
243+
244+
if event.is_a?(CheckInEvent) && configuration.before_send_check_in
245+
event = configuration.before_send_check_in.call(event, hint)
246+
247+
if !event.is_a?(CheckInEvent)
248+
# Avoid serializing the event object in this case because we aren't sure what it is and what it contains
249+
log_debug(<<~MSG)
250+
Discarded event because before_send_check_in didn't return a Sentry::CheckInEvent object but an instance of #{event.class}
251+
MSG
252+
transport.record_lost_event(:before_send, data_category)
253+
return
254+
end
257255
end
258256

259257
transport.send_event(event) if configuration.sending_to_dsn_allowed?

sentry-ruby/lib/sentry/configuration.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Configuration
6868
# @return [Proc]
6969
attr_reader :before_breadcrumb
7070

71-
# Optional Proc, called before sending an event to the server
71+
# Optional Proc, called before sending an error event to the server
7272
# @example
7373
# config.before_send = lambda do |event, hint|
7474
# # skip ZeroDivisionError exceptions
@@ -81,7 +81,7 @@ class Configuration
8181
# @return [Proc]
8282
attr_reader :before_send
8383

84-
# Optional Proc, called before sending an event to the server
84+
# Optional Proc, called before sending a transaction event to the server
8585
# @example
8686
# config.before_send_transaction = lambda do |event, hint|
8787
# # skip unimportant transactions or strip sensitive data
@@ -94,6 +94,18 @@ class Configuration
9494
# @return [Proc]
9595
attr_reader :before_send_transaction
9696

97+
# Optional Proc, called before sending a check-in event to the server
98+
# @example
99+
# config.before_send_check_in = lambda do |event, hint|
100+
# if event.monitor_slug == "unimportant_job"
101+
# nil
102+
# else
103+
# event
104+
# end
105+
# end
106+
# @return [Proc]
107+
attr_reader :before_send_check_in
108+
97109
# Optional Proc, called before sending an event to the server
98110
# @example
99111
# config.before_send_log = lambda do |log|
@@ -476,6 +488,7 @@ def initialize
476488

477489
self.before_send = nil
478490
self.before_send_transaction = nil
491+
self.before_send_check_in = nil
479492
self.before_send_log = nil
480493
self.rack_env_whitelist = RACK_ENV_WHITELIST_DEFAULT
481494
self.traces_sampler = nil
@@ -550,6 +563,12 @@ def before_send_transaction=(value)
550563
@before_send_transaction = value
551564
end
552565

566+
def before_send_check_in=(value)
567+
check_callable!("before_send_check_in", value)
568+
569+
@before_send_check_in = value
570+
end
571+
553572
def before_breadcrumb=(value)
554573
check_callable!("before_breadcrumb", value)
555574

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

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@
128128
client.event_from_exception(ZeroDivisionError.new("divided by 0"))
129129
end
130130

131+
let(:check_in_event) { client.event_from_check_in("test_slug", :ok) }
132+
131133
shared_examples "Event in send_event" do
132134
context "when there's an exception" do
133135
before do
@@ -140,6 +142,7 @@
140142
end.to raise_error(Sentry::ExternalError, "networking error")
141143
end
142144
end
145+
143146
it "sends data through the transport" do
144147
expect(client.transport).to receive(:send_event).with(event)
145148
client.send_event(event)
@@ -155,18 +158,6 @@
155158
expect(event.tags[:called]).to eq(true)
156159
end
157160

158-
context "for check in events" do
159-
let(:event_object) { client.event_from_check_in("test_slug", :ok) }
160-
161-
it "does not fail due to before_send" do
162-
configuration.before_send = lambda { |e, _h| e }
163-
client.send_event(event)
164-
165-
expect(client.transport).to receive(:send_event).with(event)
166-
client.send_event(event)
167-
end
168-
end
169-
170161
it "doesn't apply before_send_transaction to Event" do
171162
dbl = double("before_send_transaction")
172163
allow(dbl).to receive(:call)
@@ -200,19 +191,6 @@
200191
expect(string_io.string).to include("Discarded event because before_send didn't return a Sentry::ErrorEvent object but an instance of Integer")
201192
expect(return_value).to eq(nil)
202193
end
203-
204-
it "warns about Hash value's deprecation" do
205-
string_io = StringIO.new
206-
logger = Logger.new(string_io, level: :debug)
207-
configuration.sdk_logger = logger
208-
configuration.before_send = lambda do |_event, _hint|
209-
{ foo: "bar" }
210-
end
211-
212-
return_value = client.send_event(event)
213-
expect(string_io.string).to include("Returning a Hash from before_send is deprecated and will be removed in the next major version.")
214-
expect(return_value).to eq({ foo: "bar" })
215-
end
216194
end
217195

218196
it_behaves_like "Event in send_event" do
@@ -254,23 +232,60 @@
254232
expect(string_io.string).to include("Discarded event because before_send_transaction didn't return a Sentry::TransactionEvent object but an instance of NilClass")
255233
expect(return_value).to be_nil
256234
end
235+
end
236+
237+
it_behaves_like "TransactionEvent in send_event" do
238+
let(:event) { transaction_event }
239+
end
240+
241+
shared_examples "CheckInEvent in send_event" do
242+
it "sends data through the transport" do
243+
client.send_event(event)
244+
end
245+
246+
it "doesn't apply before_send to CheckInEvent" do
247+
configuration.before_send = lambda do |event, _hint|
248+
raise "shouldn't trigger me"
249+
end
250+
251+
client.send_event(event)
252+
end
253+
254+
it "doesn't apply before_send_transaction to CheckInEvent" do
255+
configuration.before_send_transaction = lambda do |event, _hint|
256+
raise "shouldn't trigger me"
257+
end
258+
259+
client.send_event(event)
260+
end
257261

258-
it "warns about Hash value's deprecation" do
262+
it "applies before_send_check_in callback before sending the event" do
263+
called = false
264+
configuration.before_send_check_in = lambda do |event, _hint|
265+
called = true
266+
event
267+
end
268+
269+
client.send_event(event)
270+
expect(called).to eq(true)
271+
end
272+
273+
it "warns if before_send_check_in returns nil" do
259274
string_io = StringIO.new
260275
logger = Logger.new(string_io, level: :debug)
261276
configuration.sdk_logger = logger
262-
configuration.before_send_transaction = lambda do |_event, _hint|
263-
{ foo: "bar" }
277+
configuration.before_send_check_in = lambda do |_event, _hint|
278+
nil
264279
end
265280

266281
return_value = client.send_event(event)
267-
expect(string_io.string).to include("Returning a Hash from before_send_transaction is deprecated and will be removed in the next major version.")
268-
expect(return_value).to eq({ foo: "bar" })
282+
expect(string_io.string).to include("Discarded event because before_send_check_in didn't return a Sentry::CheckInEvent object but an instance of NilClass")
283+
expect(return_value).to be_nil
269284
end
270285
end
271286

272-
it_behaves_like "TransactionEvent in send_event" do
273-
let(:event) { transaction_event }
287+
it_behaves_like "CheckInEvent in send_event" do
288+
let(:event) { check_in_event }
274289
end
275290
end
276291

sentry-ruby/spec/sentry/configuration_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,12 @@
389389
expect { subject.before_send_transaction = true }.to raise_error(ArgumentError, "before_send_transaction must be callable (or nil to disable)")
390390
end
391391

392+
it 'raises error when setting before_send_check_in to anything other than callable or nil' do
393+
subject.before_send_check_in = -> { }
394+
subject.before_send_check_in = nil
395+
expect { subject.before_send_check_in = true }.to raise_error(ArgumentError, "before_send_check_in must be callable (or nil to disable)")
396+
end
397+
392398
it 'raises error when setting before_breadcrumb to anything other than callable or nil' do
393399
subject.before_breadcrumb = -> { }
394400
subject.before_breadcrumb = nil

sentry-ruby/spec/sentry/integrable_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module AnotherIntegration; end
8282
it "generates Sentry::FakeIntegration.capture_check_in" do
8383
hint = nil
8484

85-
Sentry.configuration.before_send = lambda do |event, h|
85+
Sentry.configuration.before_send_check_in = lambda do |event, h|
8686
hint = h
8787
event
8888
end

0 commit comments

Comments
 (0)