Skip to content

Commit dd2d617

Browse files
authored
Implement proper flushing logic on close for sessions and client reports (#2206)
1 parent 7a6cc87 commit dd2d617

File tree

6 files changed

+56
-17
lines changed

6 files changed

+56
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
If your system serves heavy load, please let us know how this feature works for you!
2323

24+
- Implement proper flushing logic on ``close`` for Client Reports and Sessions [#2206](https://github.com/getsentry/sentry-ruby/pull/2206)
2425
## 5.15.2
2526

2627
### Bug Fixes

sentry-ruby/lib/sentry-ruby.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def exception_locals_tp
6666
end
6767

6868
# @!attribute [rw] background_worker
69-
# @return [BackgroundWorker, nil]
69+
# @return [BackgroundWorker]
7070
attr_accessor :background_worker
7171

7272
# @!attribute [r] session_flusher
@@ -233,10 +233,11 @@ def init(&block)
233233
#
234234
# @return [void]
235235
def close
236-
if @background_worker
237-
@background_worker.shutdown
238-
@background_worker = nil
239-
end
236+
@background_worker.perform { get_current_client&.transport&.flush }
237+
# session_flusher internally queues to the background worker too
238+
@session_flusher&.flush
239+
240+
@background_worker.shutdown
240241

241242
if @session_flusher
242243
@session_flusher.kill

sentry-ruby/lib/sentry/transport.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,20 @@ def record_lost_event(reason, item_type)
168168
@discarded_events[[reason, item_type]] += 1
169169
end
170170

171+
def flush
172+
client_report_headers, client_report_payload = fetch_pending_client_report(force: true)
173+
return unless client_report_headers
174+
175+
envelope = Envelope.new
176+
envelope.add_item(client_report_headers, client_report_payload)
177+
send_envelope(envelope)
178+
end
179+
171180
private
172181

173-
def fetch_pending_client_report
182+
def fetch_pending_client_report(force: false)
174183
return nil unless @send_client_reports
175-
return nil if @last_client_report_sent > Time.now - CLIENT_REPORT_INTERVAL
184+
return nil if !force && @last_client_report_sent > Time.now - CLIENT_REPORT_INTERVAL
176185
return nil if @discarded_events.empty?
177186

178187
discarded_events_hash = @discarded_events.map do |key, val|

sentry-ruby/spec/sentry/background_worker_spec.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,19 @@
101101
expect(worker.full?).to eq(false)
102102
end
103103

104-
it "returns true if thread pool and full" do
105-
configuration.background_worker_threads = 1
106-
configuration.background_worker_max_queue = 1
107-
worker = described_class.new(configuration)
108-
expect(worker.full?).to eq(false)
104+
# skipping this on jruby because the capacity check is flaky
105+
unless RUBY_PLATFORM == "java"
106+
it "returns true if thread pool and full" do
107+
configuration.background_worker_threads = 1
108+
configuration.background_worker_max_queue = 1
109+
worker = described_class.new(configuration)
110+
expect(worker.full?).to eq(false)
109111

110-
2.times { worker.perform { sleep 0.1 } }
111-
expect(worker.full?).to eq(true)
112-
sleep 0.2
113-
expect(worker.full?).to eq(false)
112+
2.times { worker.perform { sleep 0.1 } }
113+
expect(worker.full?).to eq(true)
114+
sleep 0.2
115+
expect(worker.full?).to eq(false)
116+
end
114117
end
115118
end
116119
end

sentry-ruby/spec/sentry/transport_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,20 @@
474474
end
475475
end
476476
end
477+
478+
describe "#flush" do
479+
it "does not do anything without pending client reports" do
480+
expect(subject).not_to receive(:send_envelope)
481+
subject.flush
482+
end
483+
484+
it "sends pending client reports" do
485+
5.times { subject.record_lost_event(:ratelimit_backoff, 'error') }
486+
3.times { subject.record_lost_event(:queue_overflow, 'transaction') }
487+
488+
expect(subject).to receive(:send_data)
489+
subject.flush
490+
expect(io.string).to match(/Sending envelope with items \[client_report\]/)
491+
end
492+
end
477493
end

sentry-ruby/spec/sentry_spec.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,6 @@
10051005
it "calls background worker shutdown" do
10061006
expect(described_class.background_worker).to receive(:shutdown)
10071007
described_class.close
1008-
expect(described_class.background_worker).to eq(nil)
10091008
end
10101009

10111010
it "kills session flusher" do
@@ -1029,6 +1028,16 @@
10291028
described_class.close
10301029
expect(described_class.backpressure_monitor).to eq(nil)
10311030
end
1031+
1032+
it "flushes transport" do
1033+
expect(described_class.get_current_client.transport).to receive(:flush)
1034+
described_class.close
1035+
end
1036+
1037+
it "flushes session flusher" do
1038+
expect(described_class.session_flusher).to receive(:flush)
1039+
described_class.close
1040+
end
10321041
end
10331042

10341043
it "can reinitialize closed SDK" do

0 commit comments

Comments
 (0)