Skip to content

Commit 39ca600

Browse files
dingsdaxclaude
andcommitted
feat(yabeda): Fire :closed callbacks on Sentry.close for final flush
Add a :closed callback event to Sentry::Configuration, fired by Sentry.close before client.flush. sentry-yabeda registers an after(:closed) hook that performs one final Yabeda.collect! and then kills the collector, ensuring any gauge values set by collection blocks are captured in the last envelope. The existing after(:configured) guard (collector&.kill) is kept to handle re-initialisation without an explicit close call. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 855c165 commit 39ca600

5 files changed

Lines changed: 60 additions & 3 deletions

File tree

sentry-ruby/lib/sentry-ruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ def close
268268
end
269269

270270
if client = get_current_client
271+
client.configuration.run_after_close_callbacks
271272
client.flush
272273

273274
if client.configuration.include_local_variables

sentry-ruby/lib/sentry/configuration.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ def after(event, &block)
461461
def callbacks
462462
@callbacks ||= {
463463
initialize: { before: [], after: [] },
464-
configured: { before: [], after: [] }
464+
configured: { before: [], after: [] },
465+
closed: { before: [], after: [] }
465466
}
466467
end
467468

@@ -798,6 +799,11 @@ def error_messages
798799
@errors.join(", ")
799800
end
800801

802+
# @api private
803+
def run_after_close_callbacks
804+
run_callbacks(:after, :closed)
805+
end
806+
801807
private
802808

803809
def init_dsn(dsn_string)

sentry-ruby/spec/sentry/configuration_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,20 @@ class SentryConfigurationSample < Sentry::Configuration
569569
end
570570
end
571571

572+
describe '#run_after_close_callbacks' do
573+
it 'calls hooks registered with after(:closed)' do
574+
called = false
575+
576+
config = Class.new(Sentry::Configuration) do
577+
after(:closed) do
578+
called = true
579+
end
580+
end.new
581+
582+
expect { config.run_after_close_callbacks }.to change { called }.from(false).to(true)
583+
end
584+
end
585+
572586
describe "#skip_rake_integration" do
573587
it "returns false by default" do
574588
expect(subject.skip_rake_integration).to eq(false)

sentry-yabeda/lib/sentry/yabeda/configuration.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,13 @@ class Configuration
88
Sentry::Yabeda.collector = Sentry::Yabeda::Collector.new(self)
99
end
1010
end
11+
12+
after(:closed) do
13+
if (collector = Sentry::Yabeda.collector)
14+
collector.run
15+
collector.kill
16+
Sentry::Yabeda.collector = nil
17+
end
18+
end
1119
end
1220
end

sentry-yabeda/spec/sentry/yabeda/collector_spec.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,32 @@
2121
end
2222
end
2323

24+
describe "on close" do
25+
it "performs a final collect before shutting down" do
26+
expect(::Yabeda).to receive(:collect!)
27+
Sentry.close
28+
end
29+
30+
it "kills the collector and sets it to nil" do
31+
collector = Sentry::Yabeda.collector
32+
expect(collector).to receive(:kill).and_call_original
33+
Sentry.close
34+
expect(Sentry::Yabeda.collector).to be_nil
35+
end
36+
37+
it "does nothing when no collector is running" do
38+
Sentry::Yabeda.collector = nil
39+
expect { Sentry.close }.not_to raise_error
40+
end
41+
end
42+
2443
describe "auto-start" do
2544
it "starts automatically when Sentry is initialized with enable_metrics" do
2645
expect(Sentry::Yabeda.collector).to be_a(described_class)
2746
end
2847

2948
it "does not start when enable_metrics is false" do
3049
Sentry.close
31-
Sentry::Yabeda.collector = nil
3250

3351
Sentry.init do |config|
3452
config.dsn = DUMMY_DSN
@@ -40,7 +58,7 @@
4058
expect(Sentry::Yabeda.collector).to be_nil
4159
end
4260

43-
it "replaces an existing collector on re-initialization" do
61+
it "replaces an existing collector on re-initialization via close" do
4462
first = Sentry::Yabeda.collector
4563

4664
Sentry.close
@@ -49,5 +67,15 @@
4967
expect(Sentry::Yabeda.collector).to be_a(described_class)
5068
expect(Sentry::Yabeda.collector).not_to equal(first)
5169
end
70+
71+
it "replaces an existing collector on re-initialization without close" do
72+
first = Sentry::Yabeda.collector
73+
expect(first).to receive(:kill).and_call_original
74+
75+
perform_basic_setup
76+
77+
expect(Sentry::Yabeda.collector).to be_a(described_class)
78+
expect(Sentry::Yabeda.collector).not_to equal(first)
79+
end
5280
end
5381
end

0 commit comments

Comments
 (0)