Breadcrumbs

Learn more about what Sentry uses to create a trail of events (breadcrumbs) that happened prior to an issue.

Sentry uses breadcrumbs to create a trail of events that happened prior to an issue. These events are very similar to traditional logs, but can record more rich structured data.

This page provides an overview of manual breadcrumb recording and customization. Learn more about the information that displays on the Issue Details page and how you can filter breadcrumbs to quickly resolve issues in Using Breadcrumbs.

You can manually add breadcrumbs whenever something interesting happens. For example, you might manually record a breadcrumb if the user authenticates or another state change occurs.

Manually record a breadcrumb:

Copied
import sentry_sdk

sentry_sdk.add_breadcrumb(
    category='auth',
    message='Authenticated user %s' % user.email,
    level='info',
)

The available breadcrumb keys are type, category, message, level, timestamp (which defaults to the system's wall-clock time), and data, which is the place to put any additional information you'd like the breadcrumb to include. Using keys other than these six won't cause an error, but will result in the data being dropped when the event is processed by Sentry.

The Python SDK captures breadcrumbs automatically for:

  • Incoming HTTP requests are included in breadcrumbs by default in supported web frameworks.

  • Logs at or above a configured level are captured as breadcrumbs when using Python’s built-in logging module or other supported logging integrations.

  • Database queries and Redis commands are added as breadcrumbs for supported libraries.

  • Breadcrumbs are created for Popen subprocesses.

  • Breadcrumbs are made for the start and end of Spark jobs and submission and completion of Spark stages.

SDKs allow you to customize breadcrumbs through the before_breadcrumb hook.

This hook is passed an already assembled breadcrumb and a hint object containing extra metadata. The function can modify the breadcrumb or decide to discard it entirely by returning None:

Copied
import sentry_sdk
from sentry_sdk.types import Breadcrumb, BreadcrumbHint

def before_breadcrumb(crumb: Breadcrumb, hint: BreadcrumbHint) -> Breadcrumb | None:
    if crumb['category'] == 'a.spammy.Logger':
        return None

    return crumb

sentry_sdk.init(
    # ...
    before_breadcrumb=before_breadcrumb,
)

For information about what can be done with the hint, see Filtering Events.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").