Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use Sentry.init properly with sentry-ruby appender #218

Closed
oNaiPs opened this issue May 30, 2022 · 2 comments
Closed

Cannot use Sentry.init properly with sentry-ruby appender #218

oNaiPs opened this issue May 30, 2022 · 2 comments

Comments

@oNaiPs
Copy link

oNaiPs commented May 30, 2022

Environment

Provide at least:

  • Ruby Version. - any
  • Semantic Logger Version. - v4.9.0
  • Application/framework names and versions (e.g. Rails, Sinatra, Puma, etc.). - Rails 6

Expected Behavior

It's expected for the developer to call Sentry.init with custom options (see), to set e.g. environment or DSN information.
However, the sentry_ruby appender already calls Sentry.init here to set a custom logger. Every call to Sentry.init deletes the previous set configuration. Current state of implementation only gives us two options:

  • Don't use Sentry.init - not doable since some config options are only available through that method
  • Re-initialize Sentry adding the customer logger again (since previous config is deleted). For this case, since Sentry.init is called after initializing the appender, the logger configuration is reset:
pry(main)> SemanticLogger.add_appender(appender: :sentry_ruby)
pry(main)> Sentry.configuration.logger
=> #<SemanticLogger::Logger:0x00007f5edc9fc850 @filter=nil, @level=nil, @level_index=nil, @name="Sentry">
pry(main)> Sentry.init do |config| config.environment = 'dev' end
pry(main)> Sentry.configuration.environment
=> "dev"
pry(main)> Sentry.configuration.logger
=> #<SemanticLogger::Logger:0x00007f5ee4a8d550 @filter=nil, @level=nil, @level_index=nil, @name="Rails">

Actual Behavior

There should be a way to initialize Sentry outside the appender initialization. I would recommend against passing additional Sentry config options to the appender since e.g. sentry-rails also assumes Sentry.init initialization to be called on a specific file config/initializers/sentry.rb.

Pull Request

I could write a PR but would like to discuss options first.

@reidmorrison
Copy link
Owner

Definitely open to pull requests. I have not used Sentry myself so have no specific solutions here.

@aka-nez
Copy link

aka-nez commented Sep 2, 2022

Hi, @reidmorrison ! First of all, I want to say thank you for this gem.

I faced similar behavior a few days ago and tried to find solutions.

Now sentry-ruby appender calls Sentry.init. If developers call Sentry.init too, and it rewrites the configuration.

I suggest the following solution:

Replace this line

::Sentry.init { |config| config.logger = SemanticLogger[::Sentry] } 

to something like this

::Sentry.init { |config| config.logger = SemanticLogger[::Sentry] } unless ::Sentry.initialized?

This change allows use of the appender as usual but adds the opportunity to add a custom sentry initializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants