Skip to content

Commit 43e5788

Browse files
authored
Add new configuration callbacks (#2724)
* Yield config from its constructor * feat(configuration): add before/after callbacks * Update CHANGELOG
1 parent 076adb7 commit 43e5788

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Internal
44

55
- Fix leftover `config.logger` call in `graphql` patch ([#2722](https://github.com/getsentry/sentry-ruby/2722)
6+
- Add `Configuration.before` and `Configuration.after` to run hooks before and after given event ([#2724](https://github.com/getsentry/sentry-ruby/pull/2724))
67

78
## 5.27.1
89

sentry-ruby/lib/sentry-ruby.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ def add_attachment(**opts)
239239
# @yieldparam config [Configuration]
240240
# @return [void]
241241
def init(&block)
242-
config = Configuration.new
243-
yield(config) if block_given?
242+
config = Configuration.new(&block)
244243

245244
config.detect_release
246245
apply_patches(config)

sentry-ruby/lib/sentry/configuration.rb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,23 @@ def post_initialization_callbacks
400400

401401
# allow extensions to add their hooks to the Configuration class
402402
def add_post_initialization_callback(&block)
403-
post_initialization_callbacks << block
403+
callbacks[:initialize][:after] << block
404+
end
405+
406+
def before(event, &block)
407+
callbacks[event.to_sym][:before] << block
408+
end
409+
410+
def after(event, &block)
411+
callbacks[event.to_sym][:after] << block
412+
end
413+
414+
# @!visibility private
415+
def callbacks
416+
@callbacks ||= {
417+
initialize: { before: [], after: [] },
418+
configured: { before: [], after: [] }
419+
}
404420
end
405421

406422
def validations
@@ -444,6 +460,8 @@ def build_validation_proc(optional, type)
444460
validate :profiles_sample_rate, optional: true, type: :numeric
445461

446462
def initialize
463+
run_callbacks(:before, :initialize)
464+
447465
self.app_dirs_pattern = APP_DIRS_PATTERN
448466
self.debug = Sentry::Utils::EnvHelper.env_to_bool(ENV["SENTRY_DEBUG"])
449467
self.background_worker_threads = (processor_count / 2.0).ceil
@@ -498,9 +516,13 @@ def initialize
498516
@structured_logging = StructuredLoggingConfiguration.new
499517
@gem_specs = Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map)
500518

501-
run_post_initialization_callbacks
502-
503519
self.max_log_events = LogEventBuffer::DEFAULT_MAX_EVENTS
520+
521+
run_callbacks(:after, :initialize)
522+
523+
yield(self) if block_given?
524+
525+
run_callbacks(:after, :configured)
504526
end
505527

506528
def validate
@@ -784,8 +806,8 @@ def running_on_heroku?
784806
File.directory?("/etc/heroku") && !ENV["CI"]
785807
end
786808

787-
def run_post_initialization_callbacks
788-
self.class.post_initialization_callbacks.each do |hook|
809+
def run_callbacks(hook, event)
810+
self.class.callbacks[event][hook].each do |hook|
789811
instance_eval(&hook)
790812
end
791813
end

sentry-ruby/spec/sentry/configuration_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,39 @@ class SentryConfigurationSample < Sentry::Configuration
605605
end
606606
end
607607

608+
describe '.before' do
609+
it 'calls a hook before given event' do
610+
config = Class.new(Sentry::Configuration) do
611+
attr_reader :info
612+
613+
before(:initialize) do
614+
@info = "debug is #{debug.inspect}"
615+
end
616+
end.new do |config|
617+
config.debug = true
618+
end
619+
620+
expect(config.info).to eq("debug is nil")
621+
expect(config.debug).to be(true)
622+
end
623+
end
624+
625+
describe '.after' do
626+
it 'calls a hook after given event' do
627+
config = Class.new(Sentry::Configuration) do
628+
attr_reader :info
629+
630+
after(:configured) do
631+
@info = "debug was set to #{debug}"
632+
end
633+
end.new do |config|
634+
config.debug = true
635+
end
636+
637+
expect(config.info).to eq("debug was set to true")
638+
end
639+
end
640+
608641
describe "#skip_rake_integration" do
609642
it "returns false by default" do
610643
expect(subject.skip_rake_integration).to eq(false)

0 commit comments

Comments
 (0)