honeybadger-ruby: can't get error reporting from rake tasks

No matter what I try, I can’t seem to get error reporting from rake tasks.

At first I didn’t realize this was even a feature in the honeybadger gem, but noticed the rake hooks, and the configuration option exceptions.rescue_rake, Default: true when run in background; false when run in terminal.

The rake task I was originally trying to get reporting from was run from a cronjob, so I’d think that would be not in a terminal, but this gets complicated, and I’m not totally sure if it ends up STDOUT.tty? or not.

So I figure, okay, I’ll just set it explictly then. But I can’t get this to work either. Not by setting an ENV variable HONEYBADGER_EXCEPTIONS_RESCUE_RAKE, not even by putting the key explicitly in my config/honeybadger.yml.

From trying to set breakpoints and debug the code, I have determined that the rake wrapper method display_error_message_with_honeybadger is getting called. Honeybadger is in there.

But I’m not sure my config/honeybadger.yml is ever actually getting read, even to get api keys, and I’m not sure the ENV is ever being looked up at to load honeybadger config either. This gets really confusing to debug, in various different contexts, both from cron job and just manually on command line, both on my dev workstation and my staging server. Things seem to behave differently in different contexts for reasons I can’t figure out. But in none of them can I get an error to actually be reported to honeybadger.

Are you certain error reporting from rake tasks is working at all? Could you possibly supply a demonstration example, ideally a Rails app (since that’s what I have), where an error in a rake task is reported to honeybadger? With that I could try tracing the code in a working demonstration and seeing how my actual app differs. I’m not really sure what’s going on, but I think honeybadger reporting from rake tasks may not work at all?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 28 (28 by maintainers)

Commits related to this issue

Most upvoted comments

if $0 =~ /rake/
  require 'honeybadger/init/ruby'
elsif defined?(::Rails::Railtie)
  require 'honeybadger/init/rails'

Hypothetical problem, if the rake task in a Rails app does have an :environment dependency, it’s the init/rails code that should probably be run. Now the difference between effect of init/rails and init/ruby are actually fairly minor, so it may not matter. One potential difference, a slight difference in paths that the honeybadger.yml is looked for. But it might wind up being awfully confusing on the edge case if honeybadger in your rails app normally finds your honeybadger.yml , but not when running rake tasks.

One solution is maybe concurrency-safe flags for “exit_handler_installed?” and/or “honeybadger_init’d?”. And Agent.notify has to check for each of these at the beginning, and do em if they weren’t done already. On every call, which is sad, so maybe that’s no good. But I can’t think of any other safe way to do things at present.

If you did do that, then you might not even need the separate init paths for rails/sinatra/rake. The single lazy init on every notify might be enough. Simplifying code a bit. But at the cost of checking the flags on every notify, which is potentially expensive only because it needs to be concurrency-safe so involve a mutex.

But okay, enough for tonight, I’ll let you and team think on this!

Quick update: I am working on a fix which makes Honeybadger.init idempotent and adds a custom hidden rake task honeybadger:init as a dependency to all the rake tasks giving us a hook where we can test if the rails environment was actually loaded. I just need to clean up the code and add a few tests.

I still think looking at the code that the most straightforward solution is some kind of concurrency-safe flag for “has been initialized”. If running in an app with Rails::Railstie (ie honeybadger decides it’s rails), and a rake task is executed, and the flag is still false, that’s how you know rails hasn’t booted (which would trigger the usual rails honeybadger init), and you need to do the honeybadger init now.

For purpose of “it’s not installing the at_exit handler cause it thinks it’s sinatra when it’s not” bug – I wonder if you just gotta make the client explicitly configure framework in config, to tell honeybadger what framework. That’d be unfortunate. Or maybe at least allow this as a force when the auto-detect code gets confused. I guess the trick there is at present the framework-specific-initialization code runs before any config is read, and config won’t be read if the framework-specific-initialization isn’t done right, which is part of the problem I was running into.