Homepage

Tutorial - how to skip Sentry Slack notifications

Sentry is a popular app for tracking software’s errors and inaccuracies. To better monitor the code, crashes or API calls you can set up a chain of incoming Slack notifications. Yet, Sentry and Slack sometimes aren’t that cooperative as they could be. Read on to see how to turn them off.

We’ve been working on integrations of many different warehouse systems with the Shopify platform. All data exchange between them utilizes Sidekiq workers’ background jobs. Generally, we want to be notified about the first occurrence of an error. So most exceptions are caught by Raven and sent to Sentry. However, we faced some exceptions at remote systems, for example, connection issues. Luckily, after some worker retries the problems were solved without any additional actions. In such cases we wanted Sidekiq workers to have silent retries without spamming our Slack channel with Sentry messages.

The bad news is that Sidekiq doesn’t offer access to retry_count param from a worker. Fortunately, Sidekiq offers us developers middleware that allows us to add a functionality which has access to job attributes including retry_count. Raven allows to specify in config should_capture where we will add Proc, where we exclude custom error Sidekiq::SilentRetryError.

Let’s start with registering our retry middleware.

tsx

We want to delay some specific network/api errors, so let’s define an array that contains some of them.

tsx

In the next step we define a module which we include in our worker. This module will help us identify monitored worker and also allow us to define retry_count _for_sentry there.

tsx

Our custom error class:

tsx

In our middleware, we rescue errors for which we want to delay Sentry notifications. If worker’s retry_count is lower than our retry_count_for_sentry then we have to replace the original exception with a custom one and raise Sidekiq::SilentRetryError – otherwise we would have to reraise the original one. Without that, Sidekiq treats the job as completed.

tsx

The last thing to do is to define should_capture for Raven. We can define Proc which checks if the exception contains Sidekiq::SilentRetryError.

tsx

Summary

Sentry will be notified after ninth retry of some errors. We wanted to avoid overflooding Sentry/Slack with notifications. Some jobs after some retries are successful and there’s no need to get notifications from the very beginning.

Let’s Create a Great Website Together

We'll shape your web platform the way you win it!

More posts in this category