semantic_logger icon indicating copy to clipboard operation
semantic_logger copied to clipboard

Cannot use Sentry.init properly with sentry-ruby appender

Open oNaiPs opened this issue 2 years ago • 2 comments

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.

oNaiPs avatar May 30 '22 16:05 oNaiPs

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

reidmorrison avatar Jul 05 '22 22:07 reidmorrison

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.

aka-nez avatar Sep 02 '22 06:09 aka-nez