bugsnag-ruby icon indicating copy to clipboard operation
bugsnag-ruby copied to clipboard

Ability to suppress "No valid API key" warning

Open kolen opened this issue 6 years ago • 9 comments
trafficstars

Description

When doing Bugsnag.configure, if there's no valid API key (and usually API key is configured only in production environment), the following warning appears:

** [Bugsnag] 2019-07-22 22:36:45 +0300: No valid API key has been set, notifications will not be sent

Also, when triggering exception, another warning appears:

Not notifying due to an invalid api_key

(this one is without logging prefix, and happens in at_exit, when pressing ctrl+c to shut down rails dev server, and that might be separate problem due to general at_exit weirdness)

These messages are annoying: I know for sure that I don't have Bugsnag API key in dev and test environments, and I don't want to be warned about that.

One way to suppress that is to modify Bugsnag::Configuration's logger before calling Bugsnag.configure:

require "bugsnag"

unless Rails.env.production?
  Bugsnag.configuration.logger.sev_threshold = Logger::ERROR
end

Bugsnag.configure do |config|
# ...

But that's intrusion into private API (despite configuration and logger methods are public, it's undocumented). And it does not cause the second warning (Not notifying due to an invalid api_key) to disappear, as it's sent through some separate logger that does not have time prefixes.

Environment

Library versions:

  * bugsnag (6.11.1)
  * bugsnag-capistrano (1.1.2)
  * rails (5.2.3)
  • OS / OS version: MacOS 10.13.6
  • debug mode or production?: I don't know what it's about

Example code snippet

require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "bugsnag"
  gem "rails"
  gem "rack"
end

require "bugsnag"
require "rails"
require "rack"

class MyApp < Rails::Application
end
MyApp.initialize!

# here happens warning:
Bugsnag.configure do |config|
end

# I don't see 'Not notifying due to an invalid api_key' error message 
# neither for this notification, nor for at_exit
begin
  raise 'Something went wrong!'
rescue => e
  Bugsnag.notify(e)
end

Rack::Handler::WEBrick.run MyApp
% ruby test.rb
config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:

  * development - set it to false
  * test - set it to false (unless you use a tool that preloads your test environment)
  * production - set it to true

** [Bugsnag] 2019-07-22 23:25:00 +0300: No valid API key has been set, notifications will not be sent

kolen avatar Jul 22 '19 20:07 kolen

Thanks for the report, @kolen - the logger option is documented in configuration options despite not being listed in the code-generated documentation. So its expected and safe to change it or override it entirely.

The easiest way to configure this how you want is probably something like:

Bugsnag.configure do |config|
  config.logger.level = Logger::ERROR
end

when the key will be missing.

kattrali avatar Jul 22 '19 20:07 kattrali

@kattrali I tried that code, but it also caused the Rails logger to only output error-level log entries. Is there a way to set the Bugsnag log level without affecting the Rails log level?

Does config.logger map to the Rails logger by default or something? I'm surprised that it would do this

xtagon avatar Apr 08 '20 19:04 xtagon

Hey @xtagon, sorry for the late response on this one. If you try the following instead:

Bugsnag.configure do |config|
  config.logger = Logger.new(STDOUT)
  config.logger.level = Logger::ERROR
end

You should see the Bugsnag logger level be set to ERROR, but the Rails.logger will remain at whatever level you've set this to in your config environment.

xljones avatar May 28 '20 11:05 xljones

@xander-jones Thank you, I will try it!

xtagon avatar May 29 '20 00:05 xtagon

@kattrali While that workaround works, could this issue be reopened?

Personally, I think this warning should just never be emitted unless config.notify_release_stages.include?(config.release_stage). At Shopify we have countless repositories that use the Bugsnag gem, and almost all of them get spammed with this warning. Fixing them would involve repeating the logger trick in each repo, which seems avoidable.

amomchilov avatar Dec 03 '20 16:12 amomchilov

Thanks for the comment @amomchilov . We will take a look into this.

abigailbramble avatar Dec 03 '20 18:12 abigailbramble

No solution yet?

alxmagro avatar Jun 23 '21 18:06 alxmagro

Hi @alexandremagro - currently we don't have a way of addressing this without breaking changes. We will continue to keep this under review, but in the meantime we believe configuring the logger option as described here should suppress these warnings.

yousif-bugsnag avatar Jun 28 '21 16:06 yousif-bugsnag

This issue of generating erroneous warnings in development is something that affects most users.

Can I suggest that the installation generator is updated to add the recommended workaround above.

Like this:

  def create_initializer_file
    unless /^[a-f0-9]{32}$/ =~ api_key
      raise Thor::Error, "Invalid bugsnag notifier api key #{api_key.inspect}\nYou can find the api key on the Settings tab of https://bugsnag.com/"
    end

    initializer "bugsnag.rb" do
      <<-EOF
Bugsnag.configure do |config|
  config.api_key = #{api_key.inspect}
  config.logger = Logger.new(STDOUT)
  config.logger.level = Logger::ERROR
end
      EOF
    end
  end

Would everyone be happy with this change?

evolve2k avatar Jul 25 '22 06:07 evolve2k

Hi all,

After consideration and internal discussion we would not want to ignore this warning by default as doing so would mean users are not alerted to a misconfiguration when this occurs in the genuine case.

We have updated the documentation with the following suggestion in order to work around the issue.

Within the Bugsnag configuration you can do the following:

config.logger = Logger.new(STDOUT) # In Rails apps, create a new logger to avoid
                                   # changing the Rails log level
config.logger.level = Logger::ERROR

https://docs.bugsnag.com/platforms/ruby/rails/configuration-options/#logger which will then ignore the lower level DEBUG messages.

Generally we would expect a valid API key to be set across all release stages. There are alternative ways to stop Bugsnag notifying in specific release stages, for example you could leave the api key in place across all release stages but configure something like

config.release_stage = 'development' https://docs.bugsnag.com/platforms/ruby/rails/configuration-options/#release_stage

and

config.enabled_release_stages = ['production'] https://docs.bugsnag.com/platforms/ruby/rails/configuration-options/#enabled_release_stages

which will only notify when release stage is set to Production and would have the effect of ignoring errors that occur in development.

johnkiely1 avatar Aug 22 '22 09:08 johnkiely1