Skip to content

Commit

Permalink
Add ability to have many post initialization callbacks (#1261)
Browse files Browse the repository at this point in the history
* Add ability to have many post initialization callbacks

* Revert version bumping, fix codestyle and rewrite rspec test

* Remove dependenciy bumping from sentry-sidekiq

* Add entries to CHANGELOG
  • Loading branch information
mrexox authored Feb 2, 2021
1 parent a7175ec commit b2359d0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sentry-rails/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Make sentry-rails a Rails engine and provide default job class for async [#1181](https://github.com/getsentry/sentry-ruby/pull/1181)
- Allow users to configure ActiveJob adapters to ignore [#1256](https://github.com/getsentry/sentry-ruby/pull/1256)
- Tag `job_id` and `provider_job_id` on ActiveJob events [#1259](https://github.com/getsentry/sentry-ruby/pull/1259)
- Use another method for post initialization callback [#1261](https://github.com/getsentry/sentry-ruby/pull/1261)

## 4.1.7

Expand Down Expand Up @@ -83,4 +84,3 @@ Release test
## 0.1.0

First version

2 changes: 1 addition & 1 deletion sentry-rails/lib/sentry/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Sentry
class Configuration
attr_reader :rails

def post_initialization_callback
add_post_initialization_callback do
@rails = Sentry::Rails::Configuration.new
@excluded_exceptions = @excluded_exceptions.concat(Sentry::Rails::IGNORE_DEFAULT)
end
Expand Down
2 changes: 1 addition & 1 deletion sentry-ruby/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add ThreadsInterface [#1178](https://github.com/getsentry/sentry-ruby/pull/1178)
- Inspect exception cause by default & don't exclude ActiveJob::DeserializationError [#1180](https://github.com/getsentry/sentry-ruby/pull/1180)
- Fixes [#1071](https://github.com/getsentry/sentry-ruby/issues/1071)
- Add ability to have many post initialization callbacks [#1261](https://github.com/getsentry/sentry-ruby/pull/1261)

## Unreleased

Expand Down Expand Up @@ -145,4 +146,3 @@ Fix require reference
## 0.1.0

First version

24 changes: 21 additions & 3 deletions sentry-ruby/lib/sentry/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ class Configuration

AVAILABLE_BREADCRUMBS_LOGGERS = [:sentry_logger, :active_support_logger].freeze

# Post initialization callbacks are called at the end of initialization process
# allowing extending the configuration of sentry-ruby by multiple extensions
@@post_initialization_callbacks = []

def initialize
self.background_worker_threads = Concurrent.processor_count
self.breadcrumbs_logger = []
Expand All @@ -182,7 +186,7 @@ def initialize

@transport = Transport::Configuration.new
@gem_specs = Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map)
post_initialization_callback
run_post_initialization_callbacks
end

def dsn=(value)
Expand Down Expand Up @@ -386,7 +390,21 @@ def server_name_from_env
end
end

# allow extensions to extend the Configuration class
def post_initialization_callback; end
def run_post_initialization_callbacks
self.class.post_initialization_callbacks.each do |hook|
instance_eval(&hook)
end
end

# allow extensions to add their hooks to the Configuration class
def self.add_post_initialization_callback(&block)
self.post_initialization_callbacks << block
end

protected

def self.post_initialization_callbacks
@@post_initialization_callbacks
end
end
end
23 changes: 23 additions & 0 deletions sentry-ruby/spec/sentry/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,27 @@ class MyTestException < RuntimeError; end
end
end
end

describe '.add_post_initialization_callback' do
class SentryConfigurationSample < Sentry::Configuration
attr_reader :var1, :var2

add_post_initialization_callback do
@var1 = 1
end

add_post_initialization_callback do
@var2 = 2
end
end

subject(:configuration) { SentryConfigurationSample }

it 'calls all hooks and initializes assigned variables' do
instance = configuration.new

expect(instance.var1). to eq 1
expect(instance.var2). to eq 2
end
end
end

0 comments on commit b2359d0

Please sign in to comment.