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

undefined method `exclude_loggers' for nil:NilClass during Rails boot on production #1858

Closed
tvdeyen opened this issue Jul 26, 2022 · 13 comments · Fixed by #1860
Closed

undefined method `exclude_loggers' for nil:NilClass during Rails boot on production #1858

tvdeyen opened this issue Jul 26, 2022 · 13 comments · Fixed by #1860
Assignees

Comments

@tvdeyen
Copy link

tvdeyen commented Jul 26, 2022

Issue Description

Booting a Rails app during deployment fails with

undefined method `exclude_loggers' for nil:NilClass (NoMethodError)

in

/opt/render/project/.gems/ruby/2.7.0/gems/sentry-ruby-5.4.1/lib/sentry/breadcrumb/sentry_logger.rb:80:in `ignored_logger?': undefined method `exclude_loggers' for nil:NilClass (NoMethodError)
  from /opt/render/project/.gems/ruby/2.7.0/gems/sentry-ruby-5.4.1/lib/sentry/breadcrumb/sentry_logger.rb:55:in `add_breadcrumb'
  from /opt/render/project/.gems/ruby/2.7.0/gems/sentry-ruby-5.4.1/lib/sentry/breadcrumb/sentry_logger.rb:18:in `add'
  from /opt/render/project/rubies/ruby-2.7.6/lib/ruby/2.7.0/logger.rb:494:in `debug'
  from /opt/render/project/.gems/ruby/2.7.0/gems/newrelic_rpm-8.9.0/lib/new_relic/agent/agent_logger.rb:94:in `block in format_and_send'
  from /opt/render/project/.gems/ruby/2.7.0/gems/newrelic_rpm-8.9.0/lib/new_relic/agent/agent_logger.rb:91:in `each'
  from /opt/render/project/.gems/ruby/2.7.0/gems/newrelic_rpm-8.9.0/lib/new_relic/agent/agent_logger.rb:91:in `format_and_send'
  from /opt/render/project/.gems/ruby/2.7.0/gems/newrelic_rpm-8.9.0/lib/new_relic/agent/agent_logger.rb:44:in `debug'
  from /opt/render/project/.gems/ruby/2.7.0/gems/newrelic_rpm-8.9.0/lib/new_relic/agent/threading/agent_thread.rb:20:in `block in create'

Reproduction Steps

  1. Update to sentry-ruby 5.4.1
  2. Deploy to production
  3. Run rake db:migrate

Expected Behavior

It should not fail

Actual Behavior

It should work

Ruby Version

2.7.6

SDK Version

5.4.1

Integration and Its Version

Rails 6.0.5.1

Sentry Config

Sentry.init do |config|
  config.release = "<snip>"
  config.dsn = ENV["SENTRY_DSN"]
  config.breadcrumbs_logger = [:sentry_logger, :active_support_logger, :http_logger]
end
@tvdeyen
Copy link
Author

tvdeyen commented Jul 26, 2022

Downgrading to sentry-ruby 5.3.1 fixes the issue.

@st0012
Copy link
Collaborator

st0012 commented Jul 26, 2022

This is weird because if it reaches there, the SDK should already be initialized so the configuration should present 🤔

Sentry.configuration.exclude_loggers.include?(progname)

@sl0thentr0py any thoughts?

@sl0thentr0py
Copy link
Member

hmm I guess what is happening is that since this patches Logger, some rails logic seems to happen before Sentry.init and this patch tries to add breadcrumbs for those logs.
But I don't know why this would only show up in the new release.

@sl0thentr0py
Copy link
Member

sl0thentr0py commented Jul 26, 2022

and we did add the close to at_exit which also sets initialized? to false and causes Sentry.configuration to be nil.

@sl0thentr0py sl0thentr0py assigned sl0thentr0py and unassigned st0012 Jul 26, 2022
@st0012
Copy link
Collaborator

st0012 commented Jul 26, 2022

But at the beginning of add_breadcrumb we already check Sentry.get_current_hub is present. So Sentry.configuration should also present I think? Ofc it's possible to have the SDK closed between the 2 calls but I think it's unlikely.

@sl0thentr0py
Copy link
Member

yeah you're right. No clue, but I also see some newrelic in the trace above. I'll try to repro this tomorrow.

@anteo
Copy link

anteo commented Aug 2, 2022

I got the same error with newrelic_rpm (8.9.0) + sentry-ruby (5.4.1)

Sentry calls close in at_exit handler which makes Sentry.configuration to be nil in the main thread.

At the same time NewRelic's AgentThread finalizes its job by sending events and breadcrumbs and also calling SentryLogger#add_breadcrumb. This method checks Sentry.get_current_hub to be present and it is present, because Sentry.get_current_hub returns thread's local hub, which was not yet closed.

But Sentry.initialized? returns false, because it checks presence of @main_hub which was unset already in the main thread. It causes Sentry.configuration to return nil and #ignored_logger? method fail.

Not sure, but maybe it makes sense to get the configuration of the local thread instead of the main thread (get_main_hub) inside initialized? check?:

    def initialized?
      !!Thread.current.thread_variable_get(THREAD_LOCAL)
    end

It solved the issue for me.

@sl0thentr0py
Copy link
Member

sl0thentr0py commented Aug 2, 2022

hmm that's a great explanation @anteo, thanks!
I believe we just need a better check in add_breadcrumb. Do you agree @st0012?

        return unless Sentry.initialized? && Sentry.get_current_hub

see an equivalent check here

@tvdeyen
Copy link
Author

tvdeyen commented Aug 16, 2022

When will this be released as gem?

@sl0thentr0py
Copy link
Member

oh, I can do a patch release tomorrow

@tvdeyen
Copy link
Author

tvdeyen commented Aug 16, 2022

Nice

@sl0thentr0py
Copy link
Member

@tvdeyen 5.4.2 released

@tvdeyen
Copy link
Author

tvdeyen commented Aug 17, 2022

@sl0thentr0py thank you 🙏🏻

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

Successfully merging a pull request may close this issue.

4 participants