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

Clear stale breadcrumbs and context after Sidekiq jobs #637

Merged
merged 1 commit into from Mar 3, 2017

Conversation

drewish
Copy link
Contributor

@drewish drewish commented Feb 21, 2017

The error handler is only called after an exception so the context (specifically breadcrumbs) will carry over from previous jobs that completed successfully.

Fixes #636

I'm not sure this is exactly the way the maintainers would want to approach it but it seems to be working better for me. I'm not using ActiveJob so I don't know if that would play into this.

You can test this out by creating two new sidekiq jobs:

bundle exec rails g sidekiq:worker Happy
bundle exec rails g sidekiq:worker Sad

Then fill in some basic processing logic to have one record breadcrumbs and context:

class HappyWorker
  include Sidekiq::Worker

  def perform(*args)
    Raven.breadcrumbs.record do |crumb|
      crumb.message = "I'm happy!"
    end
    Raven.tags_context mood: 'happy'
  end
end

And the other just raise an exception and doesn't retry

class SadWorker
  include Sidekiq::Worker

  sidekiq_options :retry => false

  def perform(*args)
    raise "I'm sad!"
  end
end

Start up sidekiq with a single thread (so we don't have to worry about which worker picks up which job):

bundle exec sidekiq -c 1

Open up a rails console in another terminal window and queue up a couple of jobs:

HappyWorker.perform_async
HappyWorker.perform_async
SadWorker.perform_async

On the master branch the Sentry issue from SadWorker will include the breadcrumbs and tags from the HappyWorker:
runtimeerror__i_m_sad_

The error handler is only called after an exception so the context will
carry over from previous jobs that completed successfully.

Fixes getsentry#636
Copy link
Contributor Author

@drewish drewish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've provided some pretty detailed testing instructions so hopefully this will be easy enough to review.

@@ -2,7 +2,16 @@
require 'sidekiq'

module Raven
class Sidekiq
class SidekiqCleanupMiddleware
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you think of these names. I was going for explicit to to make the role clear.

@drewish drewish changed the title Add middleware to clear context after every Sidekiq job Clear stale breadcrumbs and context after Sidekiq jobs Feb 24, 2017
@nateberkopec nateberkopec merged commit 1aa9f1f into getsentry:master Mar 3, 2017
@drewish drewish deleted the sidekiq-cleanup branch March 3, 2017 17:41
@tarmo
Copy link

tarmo commented Mar 30, 2017

Hi @drewish, did you explicitly also test the case that tags added in SadWorker actually end up recorded in Sentry with the exception raised by it (ie. not leaked tags from previous jobs but those that we intentionally want to report for the current job)?

From some limited testing it seems that the ensure block for SidekiqCleanupMiddleware actually gets called before Sidekiq calls any error handlers so all the context will be gone by the time the exception is reported to Sentry. This is confirmed by both adding debugger calls to the ensure block and exception reporting code and observing the former being called first and also having lost all tags attached to job exception reports after updating to a raven-ruby version containing this change.

@tarmo
Copy link

tarmo commented Mar 30, 2017

I think it might work if the middleware only cleared context when no exception was raised and the error handler also cleared context. I'll try if that works.

@drewish
Copy link
Contributor Author

drewish commented Mar 30, 2017

@tarmo Can you say more about how the tags would be provided? I was definitely testing that tags and breadcrumbs from the failed job were being reported. Now specifically it was picking up breadcrumbs from the failed jobs but the extra and tags were being passed in when capturing, e.g. Raven.capture_exception(exception, extra: extra, tags: tags, level: level).

@tarmo
Copy link

tarmo commented Mar 30, 2017

@drewish if you call Raven.capture_exception from within the job it would certainly work fine because the SidekiqCleanupMiddleware hasn't yet run and cleared the context. The problem only shows if you rely on the SidekiqErrorHandler (https://github.com/getsentry/raven-ruby/blob/master/lib/raven/integrations/sidekiq.rb#L19) to catch raised exceptions and report them with any tags added to Raven.tags_context within the job.

@drewish
Copy link
Contributor Author

drewish commented Mar 30, 2017

@tarmo Ah okay yeah I wasn't relying on the default capture so I understand the case you're highlighting now. Definitely a shortcoming in my testing.

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

Successfully merging this pull request may close these issues.

None yet

3 participants