Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Internal

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

## 5.27.1

Expand Down
3 changes: 1 addition & 2 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ def add_attachment(**opts)
# @yieldparam config [Configuration]
# @return [void]
def init(&block)
config = Configuration.new
yield(config) if block_given?
config = Configuration.new(&block)

config.detect_release
apply_patches(config)
Expand Down
32 changes: 27 additions & 5 deletions sentry-ruby/lib/sentry/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,23 @@ def post_initialization_callbacks

# allow extensions to add their hooks to the Configuration class
def add_post_initialization_callback(&block)
post_initialization_callbacks << block
callbacks[:initialize][:after] << block
end

def before(event, &block)
callbacks[event.to_sym][:before] << block
end

def after(event, &block)
callbacks[event.to_sym][:after] << block
end

# @!visibility private
def callbacks
@callbacks ||= {
initialize: { before: [], after: [] },
configured: { before: [], after: [] }
}
end

def validations
Expand Down Expand Up @@ -444,6 +460,8 @@ def build_validation_proc(optional, type)
validate :profiles_sample_rate, optional: true, type: :numeric

def initialize
run_callbacks(:before, :initialize)

self.app_dirs_pattern = APP_DIRS_PATTERN
self.debug = Sentry::Utils::EnvHelper.env_to_bool(ENV["SENTRY_DEBUG"])
self.background_worker_threads = (processor_count / 2.0).ceil
Expand Down Expand Up @@ -498,9 +516,13 @@ def initialize
@structured_logging = StructuredLoggingConfiguration.new
@gem_specs = Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map)

run_post_initialization_callbacks

self.max_log_events = LogEventBuffer::DEFAULT_MAX_EVENTS

run_callbacks(:after, :initialize)

yield(self) if block_given?

run_callbacks(:after, :configured)
end

def validate
Expand Down Expand Up @@ -784,8 +806,8 @@ def running_on_heroku?
File.directory?("/etc/heroku") && !ENV["CI"]
end

def run_post_initialization_callbacks
self.class.post_initialization_callbacks.each do |hook|
def run_callbacks(hook, event)
self.class.callbacks[event][hook].each do |hook|
instance_eval(&hook)
end
end
Expand Down
33 changes: 33 additions & 0 deletions sentry-ruby/spec/sentry/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,39 @@ class SentryConfigurationSample < Sentry::Configuration
end
end

describe '.before' do
it 'calls a hook before given event' do
config = Class.new(Sentry::Configuration) do
attr_reader :info

before(:initialize) do
@info = "debug is #{debug.inspect}"
end
end.new do |config|
config.debug = true
end

expect(config.info).to eq("debug is nil")
expect(config.debug).to be(true)
end
end

describe '.after' do
it 'calls a hook after given event' do
config = Class.new(Sentry::Configuration) do
attr_reader :info

after(:configured) do
@info = "debug was set to #{debug}"
end
end.new do |config|
config.debug = true
end

expect(config.info).to eq("debug was set to true")
end
end

describe "#skip_rake_integration" do
it "returns false by default" do
expect(subject.skip_rake_integration).to eq(false)
Expand Down
Loading