Skip to content

Commit 9cd6a06

Browse files
authored
Lazily start TelemetryEventBuffer threads (#2832)
* Lazily start `TelemetryEventBuffer`s * move ensure_thread inside mutex
1 parent 8db45ed commit 9cd6a06

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
- Unify Logs and Metrics implementations ([#2826](https://github.com/getsentry/sentry-ruby/pull/2826))
6262
- Unify LogEventBuffer and MetricEventBuffer logic ([#2830](https://github.com/getsentry/sentry-ruby/pull/2830))
6363
- Add maximum limits on LogEventBuffer (1k) and MetricEventBuffer (10k) for protection from memory blowup ([#2831](https://github.com/getsentry/sentry-ruby/pull/2831))
64+
- Lazily start LogEventBuffer and MetricEventBuffer threads ([#2832](https://github.com/getsentry/sentry-ruby/pull/2832))
6465

6566
## 6.2.0
6667

sentry-ruby/lib/sentry/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ def initialize(configuration)
4949
@spotlight_transport = SpotlightTransport.new(configuration) if configuration.spotlight
5050

5151
if configuration.enable_logs
52-
@log_event_buffer = LogEventBuffer.new(configuration, self).start
52+
@log_event_buffer = LogEventBuffer.new(configuration, self)
5353
end
5454

5555
if configuration.enable_metrics
56-
@metric_event_buffer = MetricEventBuffer.new(configuration, self).start
56+
@metric_event_buffer = MetricEventBuffer.new(configuration, self)
5757
end
5858
end
5959

sentry-ruby/lib/sentry/telemetry_event_buffer.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class TelemetryEventBuffer < ThreadedPeriodicWorker
1414
FLUSH_INTERVAL = 5 # seconds
1515

1616
# @!visibility private
17-
attr_reader :pending_items, :envelope_type, :data_category
17+
attr_reader :pending_items, :envelope_type, :data_category, :thread
1818

1919
def initialize(configuration, client, event_class:, max_items:, max_items_before_drop:, envelope_type:, envelope_content_type:, before_send:)
2020
super(configuration.sdk_logger, FLUSH_INTERVAL)
@@ -36,11 +36,6 @@ def initialize(configuration, client, event_class:, max_items:, max_items_before
3636
log_debug("[#{self.class}] Initialized buffer with max_items=#{@max_items}, flush_interval=#{FLUSH_INTERVAL}s")
3737
end
3838

39-
def start
40-
ensure_thread
41-
self
42-
end
43-
4439
def flush
4540
@mutex.synchronize do
4641
return if empty?
@@ -55,12 +50,9 @@ def flush
5550
alias_method :run, :flush
5651

5752
def add_item(item)
58-
unless item.is_a?(@event_class)
59-
log_debug("[#{self.class}] expected a #{@event_class}, got #{item.class}")
60-
return
61-
end
62-
6353
@mutex.synchronize do
54+
return unless ensure_thread
55+
6456
if size >= @max_items_before_drop
6557
log_debug("[#{self.class}] exceeded max capacity, dropping event")
6658
@client.transport.record_lost_event(:queue_overflow, @data_category)

sentry-ruby/spec/support/shared_examples_for_telemetry_event_buffers.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
describe "#add_item" do
2525
let(:max_items) { 3 }
2626

27+
it "spawns only one thread" do
28+
expect do
29+
subject.add_item(event)
30+
end.to change { Thread.list.count }.by(1)
31+
32+
expect(subject.thread).to receive(:alive?).and_return(true)
33+
34+
expect do
35+
subject.add_item(event)
36+
end.to change { Thread.list.count }.by(0)
37+
end
38+
2739
it "does nothing when there are no pending items" do
2840
expect(client).not_to receive(:capture_envelope)
2941

@@ -156,9 +168,8 @@
156168

157169
it "keeps the background thread alive after an error" do
158170
subject.add_item(event)
159-
subject.start
160171

161-
thread = subject.instance_variable_get(:@thread)
172+
thread = subject.thread
162173

163174
expect(thread).to be_alive
164175
expect { subject.flush }.not_to raise_error

0 commit comments

Comments
 (0)