Skip to content

Commit d9b815c

Browse files
committed
Lazily start TelemetryEventBuffers
1 parent 792ffba commit d9b815c

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

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: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,6 +50,8 @@ def flush
5550
alias_method :run, :flush
5651

5752
def add_item(item)
53+
return unless ensure_thread
54+
5855
unless item.is_a?(@event_class)
5956
log_debug("[#{self.class}] expected a #{@event_class}, got #{item.class}")
6057
return

sentry-ruby/spec/support/shared_examples_for_telemetry_event_buffers.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@
2424
describe "#add_item" do
2525
let(:max_items) { 3 }
2626

27+
it "spawns new thread" do
28+
expect do
29+
subject.add_item(event)
30+
end.to change { Thread.list.count }.by(1)
31+
32+
expect(subject.instance_variable_get(:@thread)).to be_a(Thread)
33+
end
34+
35+
it "spawns only one thread" do
36+
expect do
37+
subject.add_item(event)
38+
end.to change { Thread.list.count }.by(1)
39+
40+
thread = subject.instance_variable_get(:@thread)
41+
expect(thread).to receive(:alive?).and_return(true)
42+
43+
expect do
44+
subject.add_item(event)
45+
end.to change { Thread.list.count }.by(0)
46+
end
47+
2748
it "does nothing when there are no pending items" do
2849
expect(client).not_to receive(:capture_envelope)
2950

@@ -156,7 +177,6 @@
156177

157178
it "keeps the background thread alive after an error" do
158179
subject.add_item(event)
159-
subject.start
160180

161181
thread = subject.instance_variable_get(:@thread)
162182

0 commit comments

Comments
 (0)